説明
EC2 のアプリケーションが AWS API を呼び出す場合は、IamInstanceProfile でロールを関連付け、一時的な認証情報を使います。API 権限は必要なインスタンスにだけ付与してください。
想定される影響
適切な認証情報がなければ API 呼び出しは失敗します。サーバーに長期キーを保存すると、漏えいした後もその権限を使われるおそれがあります。
対処方法
EC2 を信頼するロールには、アプリケーションに必要な操作とリソースだけを許可します。ロールの一時的な認証情報で API を呼び出せることを確認し、不要な長期キーを失効させて削除してください。
例
対象リージョンで承認済みの AMI を指定し、AppRole を定義します。
変更前
yaml
Resources:
AppServer:
Type: AWS::EC2::Instance
Properties:
InstanceType: t3.micro
ImageId: ami-1234567890abcdef0
変更後
yaml
Resources:
AppServer:
Type: AWS::EC2::Instance
Properties:
InstanceType: t3.micro
ImageId: ami-1234567890abcdef0
IamInstanceProfile: !Ref AppInstanceProfile
AppInstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Roles:
- !Ref AppRole
2 つ目の例は AppRole を AppInstanceProfile に含め、そのプロファイルを AppServer に関連付けます。