この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
困っていた内容
ECS on Fargate でコンテナの環境変数を SecretsManager から取得する際、ResourceInitializationError が発生しました。どうしたら解決できますか?
どう対応すればいいの?
ResourceInitializationError には複数のエラーパターンがありますが、このブログでは以下の3つのエラーパターンを取り上げます。
1. 実行 IAM ロールの権限不足
ResourceInitializationError: unable to pull secrets or registry auth: execution resource retrieval failed: unable to retrieve secret from asm: service call has been retried 1 time(s): failed to fetch secret arn:aws:secretsmanager:ap-northeast-1:111111...
タスクの実行 IAM ロールに SecretsManager からデータを参照する権限があるかご確認ください。
必要なアクセス許可は以下のとおりです( KMS は SecretsManager でデフォルト以外の暗号化キーを使用する場合に必要)。
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"secretsmanager:GetSecretValue",
"kms:Decrypt"
],
"Resource": [
"arn:aws:secretsmanager:<region>:<aws_account_id>:secret:<secret_name>",
"arn:aws:kms:<region>:<aws_account_id>:key/<key_id>"
]
}
]
}
2. 誤った形式での SecretsManager への参照
ResourceInitializationError: unable to pull secrets or registry auth: execution resource retrieval failed: unable to retrieve secret from asm: service call has been retried 1 time(s): secrets manager: failed to retrieve secret from arn:aws:secretsmanag...
コンテナの環境変数で SecretsManager の特定のキーを参照する際に、適切な形式で参照しているかご確認ください。
例えば、上記エラーは以下の形式で SecretsManager の特定のキーを参照する際に発生します。
{
"valueFrom": "arn:aws:secretsmanager:ap-northeast-1:<aws_account_id>:secret:<secret_name>:<key>",
"name": "key"
}
以下の正しい形式で参照するとエラーは発生しません。 特定のキーを参照する場合、末尾に「::」とコロンを付けるのが正しい形式です。
{
"valueFrom": "arn:aws:secretsmanager:ap-northeast-1:<aws_account_id>:secret:<secret_name>:<key>::",
"name": "key"
}
3. SecretsManager エンドポイントへの不到達
ResourceInitializationError: unable to pull secrets or registry auth: execution resource retrieval failed: unable to retrieve secret from asm: service call has been retried 5 time(s): failed to fetch secret arn:aws:secretsmanager:ap-northeast-1:111111...
SecretsManager のエンドポイントと通信可能かご確認ください。
プライベートサブネットで実行する際、ルートテーブルの NAT Gateway の設定や、 SecretsManager の VPC エンドポイントのセキュリティグループなどは適切に設定されていますか?