gpt4 book ai didi

amazon-web-services - 将现有 IAM 角色与 CloudFormation 中的 EC2 实例关联

转载 作者:行者123 更新时间:2023-12-03 14:31:19 24 4
gpt4 key购买 nike

如何为 EC2 实例使用现有 IAM 角色,而不是在 CloudFormation 模板中创建新角色?

例如,我在 AWS 控制台中创建了一个角色,并且只想使用它。

最佳答案

您可以使用现有的 InstanceProfile,而不是从堆栈中创建新的 InstanceProfile。事实上,可能已经为您创建了一个 - 来自 the docs :

If you use the AWS Management Console to create a role for Amazon EC2, the console automatically creates an instance profile and gives it the same name as the role.

这意味着您可能不必在堆栈中创建 AWS::IAM::InstanceProfile 资源。但还要注意:

The console does not create an instance profile for a role that is not associated with Amazon EC2.

在这种情况下,您可以使用以下 2 个命令从 AWS CLI 手动执行此操作:

aws iam create-instance-profile --instance-profile-name MyExistingRole
aws iam add-role-to-instance-profile --instance-profile-name MyExistingRole --role-name MyExistingRole

然后,只要您在 UI 中定义了一个名为 MyExistingRole 的角色,就足够了:

"Resources" : {

"Instance" : {
"Type" : "AWS::EC2::Instance",
...
"Properties" : {
"IamInstanceProfile" : "MyExistingRole",
...
}
}
}

关于amazon-web-services - 将现有 IAM 角色与 CloudFormation 中的 EC2 实例关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20329067/

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