在 AWS CloudFormation 中,如何将 IAM 托管策略附加到 IAM 角色?

4 分钟阅读
0

在 AWS CloudFormation 中,我要将现有或新的 AWS Identity and Access Management (IAM) 托管策略添加到新的或现有的 IAM 角色。

简短描述

要将现有或新的 IAM 托管策略添加到新的 IAM 角色资源,请使用资源类型 AWS::IAM::RoleManagedPolicyArns 属性。要将新的 IAM 管理策略添加到现有 IAM 角色资源,请使用资源类型 AWS::IAM::ManagedPolicy角色属性。

您的 IAM 管理策略可以是 AWS 管理策略或客户管理策略。

重要提示:您最多可以向 IAM 角色或用户附加 10 个管理策略。每个托管策略的大小不能超过 6144 个字符。有关更多信息,请参阅 IAM 和 STS 配额

根据您的情况,完成下面其中一个部分中的步骤:

  • 将现有的 IAM 托管策略添加到新的 IAM 角色
  • 将新的 IAM 托管策略添加到新的 IAM 角色
  • 将新的 IAM 托管策略添加到现有的 IAM 角色

解决方法

将现有的 IAM 托管策略添加到新的 IAM 角色

1.    在 AWS CloudFormation 模板中,创建一个或多个参数,您可以使用这些参数传递 IAM 管理策略的 Amazon 资源名称 (ARN)。请参阅以下 JSON 和 YAML 示例。

JSON:

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Parameters": {
        "awsExampleManagedPolicyParameterOne": {
            "Type": "String",
            "Description": "ARN of the first IAM Managed Policy to add to the role"
        },
        "awsExampleManagedPolicyParameterTwo": {
            "Type": "String",
            "Description": "ARN of the second IAM Managed Policy to add to the role"
        }
    }
}

YAML:

Parameters:
  awsExampleManagedPolicyParameterOne:
    Type: String
    Description: 'ARN of the first IAM Managed Policy to add to the role'
  awsExampleManagedPolicyParameterTwo:
    Type: String
    Description: 'ARN of the second IAM Managed Policy to add to the role'

2.    在模板的 **Resources(资源)**部分,对于资源类型 AWS::IAM::Role,将 Ref 设为您在步骤 1 中创建的参数。本例中为参数 awsExampleManagedPolicyParameterOneawsExampleManagedPolicyParameterTwo。请参阅以下 JSON 和 YAML 示例。

JSON:

{
    "Resources": {
        "RootRole": {
            "Type": "AWS::IAM::Role",
            "Properties": {
                "AssumeRolePolicyDocument": {
                    "Version": "2012-10-17",
                    "Statement": [
                        {
                            "Effect": "Allow",
                            "Principal": {
                                "Service": [
                                    "ec2.amazonaws.com"
                                ]
                            },
                            "Action": [
                                "sts:AssumeRole"
                            ]
                        }
                    ]
                },
                "Path": "/",
                "ManagedPolicyArns": [
                    {
                        "Ref": "awsExampleManagedPolicyParameterOne"
                    },
                    {
                        "Ref": "awsExampleManagedPolicyParameterTwo"
                    }
                ]
            }
        }
    }
}

YAML:

Resources:
  RootRole:
    Type: 'AWS::IAM::Role'
    Properties:
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - ec2.amazonaws.com
            Action:
              - 'sts:AssumeRole'
      Path: /
      ManagedPolicyArns:
        - !Ref awsExampleManagedPolicyParameterOne
        - !Ref awsExampleManagedPolicyParameterTwo

3.    要将现有的 IAM 托管策略应用到新的 IAM 角色,请根据您修改后的 AWS CloudFormation 模板创建堆栈更新现有堆栈

将新的 IAM 托管策略添加到新的 IAM 角色

1.    在 AWS CloudFormation 模板中,使用 AWS::IAM::ManagedPolicy 资源创建新策略。请参阅以下 JSON 和 YAML 示例。

JSON:

{
    "SampleManagedPolicy": {
        "Type": "AWS::IAM::ManagedPolicy",
        "Properties": {
            "PolicyDocument": {
                "Version": "2012-10-17",
                "Statement": [
                    {
                        "Sid": "AllowAllUsersToListAccounts",
                        "Effect": "Allow",
                        "Action": [
                            "iam:ListAccountAliases",
                            "iam:ListUsers",
                            "iam:GetAccountSummary"
                        ],
                        "Resource": "*"
                    }
                ]
            }
        }
    }
}

YAML:

SampleManagedPolicy:
    Type: AWS::IAM::ManagedPolicy
    Properties:
      PolicyDocument:
        Version: '2012-10-17'
        Statement:
          -
            Sid: AllowAllUsersToListAccounts
            Effect: Allow
            Action:
              - iam:ListAccountAliases
              - iam:ListUsers
              - iam:GetAccountSummary
            Resource: "*"

2.    使用 !Ref 逻辑 ID 语法将 IAM 管理策略资源附加到 AWS::IAM::Role 资源。

例如,将 Ref 设置为您在第 1 步中创建的资源逻辑 ID (SampleManagedPolicy)。请参阅以下 JSON 和 YAML 示例。

JSON:

{
    "RootRole": {
        "Type": "AWS::IAM::Role",
        "Properties": {
            "AssumeRolePolicyDocument": {
                "Version": "2012-10-17",
                "Statement": [
                    {
                        "Effect": "Allow",
                        "Principal": {
                            "Service": [
                                "ec2.amazonaws.com"
                            ]
                        },
                        "Action": [
                            "sts:AssumeRole"
                        ]
                    }
                ]
            },
            "Path": "/",
            "ManagedPolicyArns": [
                {
                    "Ref": "SampleManagedPolicy"
                }
            ]
        }
    }
}

YAML:

RootRole:
    Type: 'AWS::IAM::Role'
    Properties:
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - ec2.amazonaws.com
            Action:
              - 'sts:AssumeRole'
      Path: /
      ManagedPolicyArns:
        - !Ref SampleManagedPolicy

3.    要将新的 IAM 托管策略应用到新的 IAM 角色,请根据您修改后的 AWS CloudFormation 模板创建堆栈更新现有堆栈

将新的 IAM 托管策略添加到现有的 IAM 角色

1.    在 AWS CloudFormation 模板中,创建一个可用于在现有角色名称中传递的参数。请参阅以下 JSON 和 YAML 示例。

JSON:

{
    "Parameters": {
        "awsExampleRolesParameter": {
            "Type": "CommaDelimitedList",
            "Description": "Names of existing Roles you want to add to the newly created Managed Policy"
        }
    }
}

YAML:

Parameters:
  awsExampleRolesParameter:
    Type: CommaDelimitedList
    Description: Names of existing Roles you want to add to the newly created Managed Policy

2.    在模板的资源部分中,对于类型为 AWS::IAM::ManagedPolicy 的资源,请将 Ref 设置为您在第 1 步中创建的参数 (awsExampleRolesParameter)。请参阅以下 JSON 和 YAML 示例。

JSON:

{
    "Resources": {
        "SampleManagedPolicy": {
            "Type": "AWS::IAM::ManagedPolicy",
            "Properties": {
                "PolicyDocument": {
                    "Version": "2012-10-17",
                    "Statement": [
                        {
                            "Sid": "AllowAllUsersToListAccounts",
                            "Effect": "Allow",
                            "Action": [
                                "iam:ListAccountAliases",
                                "iam:ListUsers",
                                "iam:GetAccountSummary"
                            ],
                            "Resource": "*"
                        }
                    ]
                },
                "Roles": {
                    "Ref": "awsExampleRolesParameter"
                }
            }
        }
    }
}

YAML:

Resources:
  SampleManagedPolicy:
    Type: 'AWS::IAM::ManagedPolicy'
    Properties:
      PolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Sid: AllowAllUsersToListAccounts
            Effect: Allow
            Action:
              - 'iam:ListAccountAliases'
              - 'iam:ListUsers'
              - 'iam:GetAccountSummary'
            Resource: '*'
      Roles: !Ref awsExampleRolesParameter

3.    要将新的 IAM 托管策略应用到现有的 IAM 角色,请根据您修改后的 AWS CloudFormation 模板创建堆栈更新现有堆栈


相关视频

AWS 官方
AWS 官方已更新 2 年前