跳至内容

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

4 分钟阅读
0

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

简短描述

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

IAM 托管策略可以是 AWS 托管式策略或客户管理型策略。

**重要事项:**您最多可以将 20 个托管策略附加到 IAM 角色或用户。每个托管策略的大小不得超过 6,144 个字符。在依据此限制计算策略大小时,IAM 不会将空格计入在内。有关更多信息,请参阅 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 角色,请使用修改后的 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 角色,请使用修改后的 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. 在模板的 Resources(资源)部分,将 Ref 设置为您在步骤 1 中创建的 AWS::IAM::ManagedPolicy 资源类型的参数 (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 角色,请使用修改后的 CloudFormation 模板创建更新堆栈。

AWS 官方已更新 1 年前