ユーザフェデレーションを行うために python で lambda によるサービスを開発しようとしています。
この lambda はまず GetOpenIdTokenForDeveloperIdentity を呼び出して AWS 上の ID プールからトークンを取得し、それから AssumeRoleWithWebIdentity を呼び出します。しかしながら、lambda が AssumeRoleWithWebIdentity を呼び出そうと試みるとエラーになります。
"An error occurred (AccessDenied) when calling the AssumeRoleWithWebIdentity operation: Not authorized to perform sts:AssumeRoleWithWebIdentity
lambda のロールに付けられている信頼関係とポリシーは以下の通りです。
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com",
"Federated": "cognito-identity.amazonaws.com"
},
"Action": [
"sts:AssumeRole",
"sts:AssumeRoleWithWebIdentity"
]
}
]
}
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"lambda:InvokeFunction",
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents",
"cognito-identity:GetOpenIdTokenForDeveloperIdentity",
"sts:AssumeRoleWithWebIdentity"
],
"Resource": "*",
"Effect": "Allow"
}
]
}
これで AssumeRoleWithWebIdentity を呼び出すのに十分なポリシーと信頼関係が付けられているのか分かりますでしょうか。些細なことでもご教授いただけましたら幸甚です。
念の為、以下が lambda のコードの断片になります。
# 'provider_name' is a custom provider name set in an identity pool in AWS
cog_cli = boto3.client('cognito-identity')
cog_id_res = cog_cli.get_open_id_token_for_developer_identity(
IdentityPoolId=os.environ['IDENTITY_POOL_ID'],
Logins={
provider_name: user_id
}
)
sts_cli = boto3.client("sts")
sts_res = sts_cli.assume_role_with_web_identity(
RoleArn=os.environ['TARGET_ROLE_ARN'],
RoleSessionName=user_id,
WebIdentityToken=cog_id_res['Token']
)