AWSCLI v2のblobタイプの引数についてハマったお話

2020.07.14

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

サーモン大好き横山です。

AWSCLI v2 インストールが楽でいいですよね。
そんな中、breaking-changeな変更にハマったお話です。

動作確認環境

$ sw_vers
ProductName:	Mac OS X
ProductVersion:	10.15.5
BuildVersion:	19F101

$ aws --version
aws-cli/2.0.26 Python/3.7.4 Darwin/19.5.0 botocore/2.0.0dev30

ACM に SSL証明書をimport しようとした時のお話

AWSCLI v2 で 以下のファイルから証明書をimportしようとしていました。
AWS マネジメントコンソールからのインポートできることは確認済です。

  • cert.pem … 証明書
  • chain.pem … 中間証明書
  • privkey.pem … プライベートキー

acm import-certificate のhelpを見ると、以下のようになってます。

$ aws acm import-certificate help
:
SYNOPSIS
            import-certificate
          [--certificate-arn <value>]
          --certificate <value>
          --private-key <value>
          [--certificate-chain <value>]
          [--tags <value>]
          [--cli-input-json | --cli-input-yaml]
          [--generate-cli-skeleton <value>]
          [--cli-auto-prompt <value>]

OPTIONS
       --certificate-arn (string)
          The Amazon  Resource  Name  (ARN)  of  an  imported  certificate  to
          replace. To import a new certificate, omit this field.

       --certificate (blob)
          The certificate to import.

       --private-key (blob)
          The private key that matches the public key in the certificate.

       --certificate-chain (blob)
          The PEM encoded certificate chain.

       --tags (list)
          One  or  more  resource tags to associate with the imported certifi-
          cate.

          Note: You cannot apply tags when reimporting a certificate.
:

各pemファイルが作業ディレクトリ直下にいる場面で下記コマンドを叩きました。すると Invalid base64 というエラーが出ました。

$ aws acm import-certificate \
  --certificate file://./cert.pem \
  --private-key file://./privkey.pem \
  --certificate-chain file://./chain.pem

Invalid base64: "-----BEGIN PRIVATE KEY-----
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
: (省略)
xxxxxxxxxxxxxxxxxxxxxxx=
-----END PRIVATE KEY-----

同様のコマンドを AWSCLI v1で叩くと成功します。

$ venv/bin/aws --version
aws-cli/1.18.97 Python/3.7.3 Darwin/19.5.0 botocore/1.17.20

$ venv/bin/aws acm import-certificate \
  --certificate file://./cert.pem \
  --private-key file://./privkey.pem \
  --certificate-chain file://./chain.pem
{
    "CertificateArn": "arn:aws:acm:ap-northeast-1:xxxxxxxx0399:certificate/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}

原因

AWS CLIバージョン2では、デフォルトでバイナリパラメータをbase64エンコードされた文字列として渡すようになったことが原因でした。

AWS CLI version 2 now passes binary parameters as base64-encoded strings by default

  • file://〜 - ファイルの内容をbase64エンコードされたテキストとして扱う
  • fileb://〜 - ファイルの内容をエンコードされていないバイナリとして扱う

ですので、fileb://〜 で指定してあげれば動作します。

$ aws acm import-certificate \
  --certificate fileb://./cert.pem \
  --private-key fileb://./privkey.pem \
  --certificate-chain fileb://./chain.pem
{
    "CertificateArn": "arn:aws:acm:ap-northeast-1:xxxxxxxx0399:certificate/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}

また、v1でもfileb://〜 で指定しても同様に動作します。

$ venv/bin/aws acm import-certificate \
  --certificate fileb://./cert.pem \
  --private-key fileb://./privkey.pem \
  --certificate-chain fileb://./chain.pem
{
    "CertificateArn": "arn:aws:acm:ap-northeast-1:xxxxxxxx0399:certificate/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}

まとめ

v1 -> v2にバージョンが上がりbreaking-changeな部分で戸惑うこともありますが、新機能もたくさんあるので、便利なものは便利に使って行きたいですね。