gpt4 book ai didi

amazon-web-services - 具有 EC2 实例 IAM 角色的 AWS 承担角色不起作用

转载 作者:行者123 更新时间:2023-12-05 03:10:52 24 4
gpt4 key购买 nike

在我们的应用程序中,我们使用自定义角色访问 aws API。在开发环境中,我们在 app.config 中提供了 access Key 和 secret key,效果很好。

在 prod 环境中,我们设置了一个 IAM 角色,该角色对自定义角色具有必要的权限,EC2 实例使用该 IAM 角色启动。当我们尝试使用代码切换角色时,出现以下错误

消息:用户:arn:aws:sts::XXXXXXXXX:assumed-role//i-0490fbbb5ea7df6a8 无权在资源上执行:sts:AssumeRole:arn:aws:iam::XXXXXXXXXX:role/

代码:

AmazonSecurityTokenServiceClient stsClient = new AmazonSecurityTokenServiceClient();
AssumeRoleResponse assumeRoleResponse = await stsClient.AssumeRoleAsync(new AssumeRoleRequest
{
RoleArn = roleArn,
RoleSessionName = sessionName
});

var sessionCredentials = new SessionAWSCredentials(assumeRoleResponse.Credentials.AccessKeyId, assumeRoleResponse.Credentials.SecretAccessKey, assumeRoleResponse.Credentials.SessionToken);

AmazonS3Client s3Client = new AmazonS3Client(sessionCredentials);

政策详情:

"Effect": "Allow",
"Action": "sts:AssumeRole",
"Resource": "arn:aws:iam::account_id:role/role-name"

在这方面的任何帮助都会很棒。提前致谢。

最佳答案

我们通过在自定义角色的信任关系中添加以下策略解决了这个问题。

{
"Effect": "Allow",
"Principal": {
"AWS": "<ARN of role that has to assume the custom role>"
},
"Action": "sts:AssumeRole"
}

关于amazon-web-services - 具有 EC2 实例 IAM 角色的 AWS 承担角色不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38449069/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com