目的はタイトルの通りです。Cloudfrontでの配信をSSLでも実施します。方式は最近追加されたらしい「SNI 独自 SSL」方式を利用します。なお今回は検証にお手軽だった、AWS Amazonlinux を利用しました。
※AWS CLIツールでの手順を以下に書いています。こちらの方がシンプルです。
AWSにSSL証明書を、AWS CLIツールを利用してアップロード・削除の作業や管理をする方法 | 田舎に住みたいエンジニアの日記
【コマンドの有無チェック】
# whereis iam-servercertupload iam-servercertupload: /opt/aws/bin/iam-servercertupload
Amazonlinux だと最初からコマンドは入っています。ちなみに、AWSアカウントの設定をしていないとツールは利用出来ないので、まずは証明書の一覧を取得してみます。
【AWSの連携確認】
# iam-servercertlistbypath Could not load AWS Credential file. Make sure that the environment variable AWS_CREDENTIAL_FILE or command line argument --aws-credential-file is set to a file containing AWS Credentials in the following format. Enter the AWS keys without the < or > AWSAccessKeyId=<Write your AWS access key ID> AWSSecretKey=<Write your AWS secret key>
AWSアカウントと紐づいていませんでした。まずは、アカウントと紐づけてIAMのコマンドツールを利用可能にします。
・確認
# aws --version aws-cli/0.9.3 Python/2.6.8 Linux/3.4.57-48.42.amzn1.i686
・環境変数設定
# export AWS_DEFAULT_REGION=ap-northeast-1 # export AWS_CREDENTIAL_FILE=/opt/aws/credential-file
(※ 静的に追加したい場合は、環境変数としてbashとかで設定しておきます。)
# cp -rp /opt/aws/credential-file-path.template /opt/aws/credential-file
# vi /opt/aws/credential-file 以下のキーを記述します。(このキーは、AWSコンソール上で取得しておきます。) AWSAccessKeyId=xxxxxxxxxxxxxxxx AWSSecretKey=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
・結果確認
例 何か、ツールコマンドで確認します。
# aws ec2 describe-instances
ec2を立ち上げていればインスタンス情報が返ってきます。
返ってこればうまくAWSアカウントと連携しています。
【SSL証明書のアップロード】
・登録している鍵の確認
# iam-servercertlistbypath
何も登録していないので、ないですね。
・SSL証明書を登録
事前に証明書の鍵をアップロードしておきます。必要なのは、秘密鍵、公開鍵、証明書チェーンの3点です。アップロード後、以下のオプション設定をして、コマンド実行。
iam-servercertupload -s <証明書の名前> -k <秘密鍵のファイル(pem形式でOK)> -c <証明書チェーンのファイル(pem形式でOK)> -b <公開鍵のファイル(pem形式でOK)> -p/cloudfront/www.xxx.net -v ※ www.xxx.net がCloudfrontで設定した「Alternate Domain Names(CNAMEs)」であること。
・実行
# iam-servercertupload -s xxx.net -k /usr/local/key/test_key.pem -c /usr/local/key/test_ica.pem -b /usr/local/key/test_crt.pem -p /cloudfront/www.xxx.net -v arn:aws:iam::1908322323605:server-certificate/cloudfront/xxx.net/www.xxx.net ASCAIGFQSxxxxxxxxxxxx
上記のように、鍵が登録されれば成功。駄目な場合はエラーメッセージが出力されるのでそれに従って解決。
・確認
# iam-servercertlistbypath
登録した鍵が表示されれば、OK。
ちなみに、オプションは以下になります。
# iam-servercertupload --help "--help" is not a valid option Uploads a new server certificate under your account. iam-servercertupload [options...] arguments... --aws-credential-file CREDENTIALFILE : path to the file containing your AWS cr edentials --client-config-file CLIENTCONFIGFILE : path to the file containing client conf igurations --url SERVICEENDPOINT : endpoint URL to be used when making the service call -b CERTBODYFILE : the pem encoded public key certificate file -c CERTCHAINFILE : the pem encoded certificate chain file -d (--debug) : enable debug logging -h : print out this message -k PRIVATEKEYFILE : the pem encoded private key file -p PATH : the path of the server certificate, def aults to / -s SERVERCERTNAME : the name of the server certificate -v VERBOSE : print out the newly created server cert ificate's arn and guid
[amazonjs asin=”B00UWCYRZK” locale=”JP” title=”Amazon Web Services パターン別構築・運用ガイド 一番大切な知識と技術が身につく”]
【参考】
Using an HTTPS Connection to Access Your Objects – Amazon CloudFront
Amazon Web Services ブログ: 【AWS発表】 Amazon CloudFrontが独自SSL証明書とルートドメインホスティングをサポート
AWS CLIを使ってTagの値のみを取得する – funasaki memo
CentOS6にIAMのコマンドラインツールキットをセットアップする « サーバーワークス エンジニアブログ
CloudFrontの独自ドメインSSL証明書対応を試してみた | Developers.IO