如何將 IAM 管理政策連接至 AWS CloudFormation 中的 IAM 角色?

4 分的閱讀內容
0

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

簡短說明

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

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

**重要事項:**您最多可以將 10 項管理政策連接至 IAM 角色或使用者。每個管理政策的大小不得超過 6,144 個字元。如需相關資訊,請參閱 IAM 和 STS Quotas

根據您的範例,完成下列其中一個區段中的各項步驟:

  • 將現有的 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.    在範本的「資源」區段中,針對 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 年前