跳至內容

如何在 CloudFormation 中將 IAM 受管政策附加至 IAM 角色?

4 分的閱讀內容
0

我想在 AWS CloudFormation 中,將現有或新的 AWS Identity and Access Management (IAM) 受管政策新增至新的或現有的 IAM 角色。

簡短說明

若要將現有或新的 IAM 受管政策新增至新的 IAM 角色資源,請使用資源類型為 AWS::IAM::RoleManagedPolicyArns 屬性。若要將新的 IAM 受管政策新增至現有的 IAM 角色資源,請使用資源類型為 AWS::IAM::ManagedPolicyRoles 屬性。

您的 IAM 受管政策可以是 AWS 受管政策或客戶管理政策。

**重要:**您最多可以將 20 個受管政策附加至一個 IAM 角色或使用者。每個受管政策的大小不得超過 6,144 個字符。IAM 在計算政策大小是否超過此限制時,不會計入空白字元。如需詳細資訊,請參閱 IAM 和 STS 配額

請根據您的情境,完成下列其中一個章節的步驟:

  • 將現有的 IAM 受管政策新增至新的 IAM 角色
  • 將新的 IAM 受管政策新增至新的 IAM 角色
  • 將新的 IAM 受管政策新增至現有的 IAM 角色

解決方法

將現有的 IAM 受管政策新增至新的 IAM 角色

請完成以下步驟:

  1. 在您的 AWS CloudFormation 範本中,建立一或多個參數,以便傳入 IAM 受管政策的 Amazon Resource Name (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 (資源) 區段中,於資源類型 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 角色,請使用修改後的 CloudFormation 範本建立更新堆疊。

AWS 官方已更新 2 年前