19

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

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

4

5 回答 5

29

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

如果您使用 AWS 管理控制台为 Amazon EC2 创建角色,控制台会自动创建一个实例配置文件并为其提供与该角色相同的名称。

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

控制台不会为未与 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",
      ...
    }
  }
}
于 2014-06-10T18:55:52.040 回答
28

You need an instance profile, a role, and the instance info (or launch configuration) itself.

Your instance profile would look like this:

"Resources" : {
  "InstanceProfile" : {
    "Type" : "AWS::IAM::InstanceProfile",
    "Properties" : {
      "Path" : "/",
      "Roles" : ["MyExistingRole"]
    }
  },

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

In particular - note that the reference in the Instance profile is to an existing RoleName

Also - I've written about bootstrapping instances which uses instance profiles and roles to ensure we're not persisting security.

The key thing is rather than using the {"Ref" : RoleName} etc, to use the actual name of the role.

于 2013-12-03T03:59:04.483 回答
0

你想用 IAM 角色做什么?

我有一个需要访问受限 S3 存储桶的 cfn 脚本。我的实例块看起来像这样 - bucketName 和 RoleName 都是参数,具有默认值:

"Resources" : {
    "myInstance" : {
        "Type" : "AWS::EC2::Instance",

        "Metadata" : {
            "Comment1" : "My Instance stuff here",     

            "AWS::CloudFormation::Authentication": {
                "default" : {
                    "type": "s3",
                    "buckets": [ { "Ref" : "bucketName" } ],
                    "roleName": { "Ref" : "RoleName" }
                }
            },
...snip...

编辑:我在创建实例时将角色作为属性的一部分:

        "Properties" : {
            "ImageId"             : { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "64"] },
            "InstanceType"        : { "Ref" : "InstanceType" },
            "SecurityGroups"      : [ {"Ref" : "SecurityGroup"} ],
            "IamInstanceProfile"  : { "Ref" : "RoleName" },
            "KeyName"             : { "Ref" : "KeyName" },

            "BlockDeviceMappings" : [
                {
                    "DeviceName" : "/dev/sda1",
                    "Ebs" : { "VolumeSize" : "10" } 
                }
            ],

            "UserData"            : { "Fn::Base64" : { "Fn::Join" : ["", [
                "#!/bin/bash -v\n",
...snip...
            ] ] } }

RoleName 在我的参数部分中定义:

"Parameters" : {

    "RoleName" : {
        "Description" : "Role description",
        "Type" : "String",
        "Default" : "my-default-role",
        "ConstraintDescription" : "Must be a valid IAM Role"
    }
  }
于 2013-12-02T14:41:57.473 回答
0

对于那些使用启动模板的人来说,与 ec2instance 或启动配置相比,语法略有不同。

下面是使用启动模板的 yaml 示例。

LaunchTemplate:
  Properties:
    LaunchTemplateData:
      IamInstanceProfile:
        Name: !Ref ExistingInstanceProfileName
于 2021-06-16T01:56:21.927 回答
0

只需将在 Amazon 控制台中创建的现有角色名称输入到 EC2 资源 IamInstanceProfile 属性中。

Resources:
  TestEC2Instace:
     Type: AWS::EC2::Instance
     InstanceType: t2.micro
     IamInstanceProfile: ExistingRoleName
     Tags:
       - Key: Name
         Value: Public Instance

        
于 2021-03-23T09:10:07.787 回答