Lambda 関数で boto3 を利用時、run-instances を使用して EC2 マシンを作成する際にEBSボリュームを暗号化する設定を入れると、インスタンス起動してすぐに Terminate されてしまいました。
...(snip)...
response = client.run_instances(
BlockDeviceMappings=[
{
'DeviceName': '/dev/xvda',
'Ebs': {
'VolumeSize': 20,
'VolumeType': 'gp3',
'KmsKeyId': 'xxxx-xxxx-xxxx-xxxx-xxxx',
'Encrypted': True
}
}
],
ImageId='ami-xxxx',
...(snip)...
KMS のキーポリシに当該 Lambda 関数の実行ロールを追加したら、Terminate されなくなりました。
...(snip)...
"Sid": "Allow use of the key",
"Effect": "Allow",
"Principal": {
"AWS": [
"arn:aws:iam::<ACCOUNT-ID>:role/service-role/<ROLE>",
"arn:aws:iam::<ACCOUNT-ID>:user/<USER>"
]
},
"Sid": "Allow attachment of persistent resources",
"Effect": "Allow",
"Principal": {
"AWS": [
"arn:aws:iam::<ACCOUNT-ID>:role/service-role/<ROLE>",
"arn:aws:iam::<ACCOUNT-ID>:user/<USER>"
]
},
...(snip)...