EC2インスタンスにアタッチされたIAM Roleから別のIAM Roleにassumeできるようにしてあって ~/.aws/config に以下のように書いてある場合、
[profile admin] role_arn = arn:aws:iam::123456789012:role/sample-admin credential_source = Ec2InstanceMetadata
awscliでは aws --profile admin ... とすれば sample-admin というIAM Roleでawscliを実行できますが、 serverless deploy --aws-profile admin で動かす方法がわかりませんでした。。。という話。
やったこと
IAM Role 2つ準備
sample-ec2-role
sample-ec2-role という名前の、なにも権限のないIAM Role。EC2インスタンスにアタッチしておきます。
sample-admin
sample-admin という名前のIAM Role。AdministratorAccess のPolicyを付与。
Trust Relationshipには以下のように sample-ec2-role からassumeできるようにします。
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "AWS": "arn:aws:iam::123456789012:role/sample-ec2-role" }, "Action": "sts:AssumeRole" } ] }
~/.aws/config 作成
EC2インスタンスの ~/.aws/config に以下の内容を記述します。 ~/.aws/credentials は作成しません。
[profile admin] role_arn = arn:aws:iam::123456789012:role/sample-admin credential_source = Ec2InstanceMetadata
awscliで確認
profileになにも指定しないと、EC2インスタンスにアタッチされているなにも権限のないIAM Roleになるのが確認できます。
$ aws configure list
Name Value Type Location
---- ----- ---- --------
profile <not set> None None
access_key ****************RLXK iam-role
secret_key ****************g+L7 iam-role
region <not set> None None
$ aws s3 ls
An error occurred (AccessDenied) when calling the ListBuckets operation: Access Denied
--profile admin を付ければ、強い権限のIAM Roleになります。
$ aws --profile admin configure list
Name Value Type Location
---- ----- ---- --------
profile admin manual --profile
access_key ****************HZ7K assume-role
secret_key ****************Lr2q assume-role
region ap-northeast-1 config-file ~/.aws/config
$ aws --profile admin s3 ls
2020-11-03 15:45:51 xxxx
2020-07-23 18:09:47 xxxx
....
serverlessで確認
まずはprofileなしで実行。EC2インスタンスにアタッチされているなにも権限のないIAM Roleになるので、権限が足りないというエラーになります。
$ serverless deploy -v
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless Error ----------------------------------------
Could not locate deployment bucket. Error: Access Denied
Get Support --------------------------------------------
Docs: docs.serverless.com
Bugs: github.com/serverless/serverless/issues
Issues: forum.serverless.com
Your Environment Information ---------------------------
Operating System: linux
Node Version: 10.19.0
Framework Version: 2.38.0
Plugin Version: 4.5.3
SDK Version: 4.2.2
Components Version: 3.9.0
--aws-profile admin を指定してみると、 Could not locate deployment bucket. Error: AWS profile "admin" doesn't seem to be configured というよくわからないエラーになりました。
$ serverless deploy --aws-profile admin -v
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless Error ----------------------------------------
Could not locate deployment bucket. Error: AWS profile "admin" doesn't seem to be configured
Get Support --------------------------------------------
Docs: docs.serverless.com
Bugs: github.com/serverless/serverless/issues
Issues: forum.serverless.com
Your Environment Information ---------------------------
Operating System: linux
Node Version: 10.19.0
Framework Version: 2.38.0
Plugin Version: 4.5.3
SDK Version: 4.2.2
Components Version: 3.9.0
結論
このエラーメッセージから参考記事を探してみると、 ~/.aws/credentials を作ればいいとか、 AWS_SDK_LOAD_CONFIG を指定すればいいとかいう記事も見つかりますけど、いろいろな組み合わせを試しましたが、方法を見つけられませんでした。
最終的に credential_source がJavaScriptで実装されているServerless Frameworkでは対応していない、というコメントが見つかりました。
Assumed role not found when defined in ~/.aws/config · Issue #5048 · serverless/serverless
残念。
2021/04/30 追記
一時クレデンシャルを取得して環境変数に設定すれば、できました。