AMI のパブリック共有をブロックする設定を AWS CLI で設定してみる

2023.09.15

AMI のパブリック共有をブロックする機能が追加されました。早速、AWS CLI を用いて全てのリージョンでブロックを有効化したため紹介します。

AWS CLI で AMI のパブリック共有をブロックする

全てのリージョンで AMI のパブリック共有をブロックする設定に変更する AWS CLI コマンドです。image-block-public-access-stateオプションは執筆時点ではblock-new-sharingのみ指定可能です。

aws ec2 describe-regions --query "Regions[].[RegionName]" --output text \
| while read region; do
  echo "### Enable image block public access in ${region}"
  aws ec2 enable-image-block-public-access \
  --region ${region} \
  --image-block-public-access-state block-new-sharing
done

AWS CloudShell からも実行できますが、執筆時点では AWS CLI のバージョンを上げてから実行する必要があります。バージョンアップ例は下記となります。

$ curl -s "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
$ unzip -q awscliv2.zip
$ sudo ./aws/install --update
You can now run: /usr/local/bin/aws --version
$ aws --version
aws-cli/2.13.18 Python/3.11.5 Linux/4.14.255-314-253.539.amzn2.x86_64 exec-env/CloudShell exe/x86_64.amzn.2 prompt/off


ブロック設定の設定値を確認するコマンドは下記です。

aws ec2 describe-regions --query "Regions[].[RegionName]" --output text \
| while read region; do
  echo "### Get image block public access state in ${region}"
  aws ec2 get-image-block-public-access-state --region ${region}
done


有効化に利用しているコマンドは下記です。

設定確認に利用しているコマンドは下記です。

今回は利用していませんが、無効化したい場合は次のコマンドを利用します。


AMI のパブリック共有をブロック設定にするコマンドの実行結果例を掲載します。

$ aws ec2 describe-regions --query "Regions[].[RegionName]" --output text \
> | while read region; do
>   echo "### Enable image block public access in ${region}"
>   aws ec2 enable-image-block-public-access \
>   --region ${region} \
>   --image-block-public-access-state block-new-sharing
> done
### Enable image block public access in ap-south-1
{
    "ImageBlockPublicAccessState": "block-new-sharing"
}
### Enable image block public access in eu-north-1
{
    "ImageBlockPublicAccessState": "block-new-sharing"
}
### Enable image block public access in eu-west-3
{
    "ImageBlockPublicAccessState": "block-new-sharing"
}
### Enable image block public access in eu-west-2
{
    "ImageBlockPublicAccessState": "block-new-sharing"
}
### Enable image block public access in eu-west-1
{
    "ImageBlockPublicAccessState": "block-new-sharing"
}
### Enable image block public access in ap-northeast-3
{
    "ImageBlockPublicAccessState": "block-new-sharing"
}
### Enable image block public access in ap-northeast-2
{
    "ImageBlockPublicAccessState": "block-new-sharing"
}
### Enable image block public access in ap-northeast-1
{
    "ImageBlockPublicAccessState": "block-new-sharing"
}
### Enable image block public access in ca-central-1
{
    "ImageBlockPublicAccessState": "block-new-sharing"
}
### Enable image block public access in sa-east-1
{
    "ImageBlockPublicAccessState": "block-new-sharing"
}
### Enable image block public access in ap-southeast-1
{
    "ImageBlockPublicAccessState": "block-new-sharing"
}
### Enable image block public access in ap-southeast-2
{
    "ImageBlockPublicAccessState": "block-new-sharing"
}
### Enable image block public access in eu-central-1
{
    "ImageBlockPublicAccessState": "block-new-sharing"
}
### Enable image block public access in us-east-1
{
    "ImageBlockPublicAccessState": "block-new-sharing"
}
### Enable image block public access in us-east-2
{
    "ImageBlockPublicAccessState": "block-new-sharing"
}
### Enable image block public access in us-west-1
{
    "ImageBlockPublicAccessState": "block-new-sharing"
}
### Enable image block public access in us-west-2
{
    "ImageBlockPublicAccessState": "block-new-sharing"
}

設定確認コマンドの実行結果例です。image-block-public-access-stateblock-new-sharingであれば、新しいパブリック共有をブロックする設定となっています。

$ aws ec2 describe-regions --query "Regions[].[RegionName]" --output text \
> | while read region; do
>   echo "### Get image block public access state in ${region}"
>   aws ec2 get-image-block-public-access-state --region ${region}
> done
### Get image block public access state in ap-south-1
{
    "ImageBlockPublicAccessState": "block-new-sharing"
}
### Get image block public access state in eu-north-1
{
    "ImageBlockPublicAccessState": "block-new-sharing"
}
### Get image block public access state in eu-west-3
{
    "ImageBlockPublicAccessState": "block-new-sharing"
}
### Get image block public access state in eu-west-2
{
    "ImageBlockPublicAccessState": "block-new-sharing"
}
### Get image block public access state in eu-west-1
{
    "ImageBlockPublicAccessState": "block-new-sharing"
}
### Get image block public access state in ap-northeast-3
{
    "ImageBlockPublicAccessState": "block-new-sharing"
}
### Get image block public access state in ap-northeast-2
{
    "ImageBlockPublicAccessState": "block-new-sharing"
}
### Get image block public access state in ap-northeast-1
{
    "ImageBlockPublicAccessState": "block-new-sharing"
}
### Get image block public access state in ca-central-1
{
    "ImageBlockPublicAccessState": "block-new-sharing"
}
### Get image block public access state in sa-east-1
{
    "ImageBlockPublicAccessState": "block-new-sharing"
}
### Get image block public access state in ap-southeast-1
{
    "ImageBlockPublicAccessState": "block-new-sharing"
}
### Get image block public access state in ap-southeast-2
{
    "ImageBlockPublicAccessState": "block-new-sharing"
}
### Get image block public access state in eu-central-1
{
    "ImageBlockPublicAccessState": "block-new-sharing"
}
### Get image block public access state in us-east-1
{
    "ImageBlockPublicAccessState": "block-new-sharing"
}
### Get image block public access state in us-east-2
{
    "ImageBlockPublicAccessState": "block-new-sharing"
}
### Get image block public access state in us-west-1
{
    "ImageBlockPublicAccessState": "block-new-sharing"
}
### Get image block public access state in us-west-2
{
    "ImageBlockPublicAccessState": "block-new-sharing"
}

有効化後すぐに設定を確認した場合において、いくつかのリージョンでblock-new-sharingステータスではありませんでした。そのため、1 分程度待ってから確認したほうがよさそうです。なお、デフォルトのステータスはunblockedでした。


設定後のマネジメントコンソールの設定画面です。ブロックする設定になっていることが分かります。

SCP でブロック設定の変更を禁止する

AMI のパブリック共有のブロック設定の設定変更(無効化)を SCP で制限したい場合は、次のブログで紹介されているポリシーで実現できます。

アカウント発行時は AMI のパブリック共有のブロック設定の変更を禁止していない OU で設定した後に禁止している OU に移動させるか、設定変更用の IAM ロール/IAM ユーザーを Condition 区で除外しておくなどの運用が考えられます。

合わせて EBS のデフォルト暗号化設定を検討する

EC2 の設定には EBS をデフォルトで暗号化する設定もあります。AMI のパブリック共有のブロック設定と合わせて検討したい設定です。

AWS CLI を用いて全てのリージョンで設定する方法は次のブログで紹介しています。

さいごに

AMI のパブリック共有をブロックする機能が追加されたため、早速設定してみました。

以上、このブログがどなたかのご参考になれば幸いです。