我在帐户 A 中有一个 lambda 函数,尝试从帐户 B 访问资源。创建了一个新的 lambda 角色,具有基本执行权限,可以将日志上传到云监视。
这是我在 Python 3.7 中的函数代码:
import boto3
allProfiles = ['account2']
def lambda_handler(event, context):
sts_connection = boto3.client('sts')
acct_b = sts_connection.assume_role(
RoleArn="arn:aws:iam::2222222222:role/role-on-source-account",
RoleSessionName="account2"
)
for profiles in allProfiles:
print("\nusing profile %s" % profiles)
newMethod..
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
Run Code Online (Sandbox Code Playgroud)
还修改了账户 B 中假定角色的信任策略,如文档中所述:https://aws.amazon.com/premiumsupport/knowledge-center/lambda-function-assume-iam-role/
错误:调用 AssumeRole 操作时发生错误 (AccessDenied):用户:arn:aws:sts::11111111:assumed-role/lambda-role/lambda-function 无权对资源执行:sts:AssumeRole:arn: aws:iam::2222222222:角色/源账户上的角色"
注意:我可以在本地成功运行它,但不能在 lambda 中运行。