

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# CloudFormation 的 IAM 基於身分識別的政策範例
<a name="security_iam_id-based-policy-examples"></a>

依預設，使用者和角色不具備建立或修改 CloudFormation 資源的許可。他們也無法使用 AWS 管理主控台、 AWS Command Line Interface (AWS CLI) 或 AWS API 來執行任務。若要授予使用者對其所需資源執行動作的許可，IAM 管理員可以建立 IAM 政策。然後，管理員可以將 IAM 政策新增至角色，使用者便能擔任這些角色。如需詳細資訊，請參閱[定義 IAM 基於身分識別的政策](control-access-with-iam.md#iam-id-based-policies)。

下列範例顯示政策陳述式，可用於允許或拒絕使用一項或多項 CloudFormation 動作的許可。

**Topics**
+ [需要特定的範本 URL](#w2aac43c23c17b9)
+ [拒絕所有 CloudFormation 匯入操作](#w2aac43c23c17c11)
+ [允許特定資源類型的匯入操作](#w2aac43c23c17c13)
+ [拒絕堆疊範本中的 IAM 資源](#w2aac43c23c17c15)
+ [允許使用特定資源類型建立堆疊](#w2aac43c23c17c17)
+ [根據資源變動 API 動作來控制存取權](#w2aac43c23c17c19)
+ [根據區域和資源類型限制堆疊集操作](#resource-level-permissions-service-managed-stack-set)
+ [允許所有 IaC 產生器操作](#iam-policy-example-for-iac-generator)

## 需要特定的範本 URL
<a name="w2aac43c23c17b9"></a>

以下政策授予許可只使用 `{{https://s3.amazonaws.com/amzn-s3-demo-bucket/test.template}}` 範本 URL 建立或更新堆疊。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "cloudformation:CreateStack",
                "cloudformation:UpdateStack"
            ],
            "Resource": "*",
            "Condition": {
                "StringEquals": {
                    "cloudformation:TemplateUrl": [
                        "{{https://s3.amazonaws.com/amzn-s3-demo-bucket/test.template}}"
                    ]
                }
            }
        }
    ]
}
```

------

## 拒絕所有 CloudFormation 匯入操作
<a name="w2aac43c23c17c11"></a>

以下政策會授予使用者許可完成所有 CloudFormation 操作，但匯入操作「除外」。

------
#### [ JSON ]

****  

```
{ 
  "Version":"2012-10-17",		 	 	  
  "Statement": [ 
    { 
      "Sid": "AllowAllStackOperations",
      "Effect": "Allow", 
      "Action": "cloudformation:*", 
      "Resource": "*" 
    }, 
    { 
      "Sid": "DenyImport", 
      "Effect": "Deny", 
      "Action": "cloudformation:*", 
      "Resource": "*",
      "Condition": { 
        "ForAnyValue:StringLike": {
          "cloudformation:ImportResourceTypes": [ 
            "*" 
          ] 
        } 
      } 
    } 
  ] 
}
```

------

## 允許特定資源類型的匯入操作
<a name="w2aac43c23c17c13"></a>

以下政策授予所有堆疊操作的許可，以及僅限指定資源上的匯入操作 (在此範例中為 `AWS::S3::Bucket`)。

------
#### [ JSON ]

****  

```
{ 
  "Version":"2012-10-17",		 	 	  
  "Statement": [ 
    { 
      "Sid": "AllowImport",
      "Effect": "Allow", 
      "Action": "cloudformation:*", 
      "Resource": "*",
      "Condition": { 
        "ForAllValues:StringEqualsIgnoreCase": {
          "cloudformation:ImportResourceTypes": [ 
            "AWS::S3::Bucket" 
          ] 
        } 
      } 
    } 
  ] 
}
```

------

## 拒絕堆疊範本中的 IAM 資源
<a name="w2aac43c23c17c15"></a>

以下政策授予許可以建立堆疊，但如果堆疊範本包含任何來自 IAM 服務的資源，則拒絕請求。該政策也需要使用者指定 `ResourceTypes` 參數，僅供 AWS CLI 和 API 請求使用。此政策使用明確拒絕陳述式，因此，如有其他政策授予額外許可，此政策一律有效 (明確拒絕陳述式一律覆寫明確允許陳述式)。

------
#### [ JSON ]

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement":[
    {
      "Effect" : "Allow",
      "Action" : [ "cloudformation:CreateStack" ],
      "Resource" : "*"
    },
    {
      "Effect" : "Deny",
      "Action" : [ "cloudformation:CreateStack" ],
      "Resource" : "*",
      "Condition" : {
        "ForAnyValue:StringLikeIfExists" : {
          "cloudformation:ResourceTypes" : [ "AWS::IAM::*" ]
        }
      }
    },
    {
      "Effect": "Deny",
      "Action" : [ "cloudformation:CreateStack" ],
      "Resource": "*",
      "Condition": {
        "Null": {
          "cloudformation:ResourceTypes": "true"
        }
      }
    }
  ]
}
```

------

## 允許使用特定資源類型建立堆疊
<a name="w2aac43c23c17c17"></a>

以下政策類似前述範例。此政策授予許可以建立堆疊，除非堆疊範本包含任何來自 IAM 服務的資源。它還要求使用者指定 參數，該`ResourceTypes`參數僅適用於 AWS CLI 和 API 請求。此政策更為簡單，但不使用明確拒絕陳述式。授予額外許可的其他政策，可以覆寫此政策。

------
#### [ JSON ]

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement":[
    {
      "Effect" : "Allow",
      "Action" : [ "cloudformation:CreateStack" ],
      "Resource" : "*",
      "Condition" : {
        "ForAllValues:StringNotLikeIfExists" : {
          "cloudformation:ResourceTypes" : [ "AWS::IAM::*" ]
        },
        "Null":{
          "cloudformation:ResourceTypes": "false"
        }
      }
    }
  ]
}
```

------

## 根據資源變動 API 動作來控制存取權
<a name="w2aac43c23c17c19"></a>

下列政策授予依據資源變更 API 動作名稱篩選存取權的許可。這用於控制 IAM 使用者可使用哪些 API 來在堆疊或堆疊集上新增或移除標籤。用於新增或移除標籤的操作應做為條件索引鍵的值新增。下列政策會授予 `TagResource` 和 `UntagResource` 變更操作 `CreateStack` 的許可。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [{
        "Sid": "CreateActionConditionPolicyForTagUntagResources",
        "Effect": "Allow",
        "Action": [
            "cloudformation:TagResource",
            "cloudformation:UntagResource"
        ],
        "Resource": "*",
        "Condition": {
            "StringEquals": {
                "cloudformation:CreateAction": [
                    "CreateStack"
                ]
            }
        }
    }]
}
```

------

## 根據區域和資源類型限制堆疊集操作
<a name="resource-level-permissions-service-managed-stack-set"></a>

下列政策授與服務受管堆疊集許可。只有當堆疊集的範本包含 Amazon S3 資源類型 (`AWS::S3::*`) 或 `AWS::SES::ConfigurationSet` 資源類型時，具有此政策的使用者才能在堆疊集上執行操作。如果使用 ID 登入組織管理帳戶`123456789012`，使用者也只能對以 ID 為 的 OU 為目標的堆疊集執行操作`{{ou-1fsfsrsdsfrewr}}`，而且只能對以 ID AWS 帳戶 為 為目標`stack-set-id`的堆疊集執行操作`{{987654321012}}`。

如果堆疊集範本包含的資源類型不是政策中指定的資源類型，或部署目標不是政策中為對應的管理帳戶和堆疊集所指定的 OU 或帳戶 ID，則堆疊集操作會失敗。

只有在堆疊集操作以 `us-east-1`、`us-west-2` 或 `eu-west-2` AWS 區域為目標時，才會套用這些政策限制。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "cloudformation:*"
            ],
            "Resource": [
                "arn:aws:cloudformation:*:*:stackset/*",
                "arn:aws:cloudformation:*:*:type/resource/{{AWS-S3}}-*",
                "arn:aws:cloudformation:us-west-2:{{111122223333}}:type/resource/{{AWS-SES-ConfigurationSet}}",
                "arn:aws:cloudformation:*:{{111122223333}}:stackset-target/*/{{ou-1fsfsrsdsfrewr}}",
                "arn:aws:cloudformation:*:{{111122223333}}:stackset-target/{{stack-set-id}}/{{444455556666}}"
            ],
            "Condition": {
                "ForAllValues:StringEqualsIgnoreCase": {
                    "cloudformation:TargetRegion": [
                        "{{us-east-1}}",
                        "{{us-west-2}}",
                        "{{eu-west-1}}"
                    ]
                }
            }
        }
    ]
}
```

------

## 允許所有 IaC 產生器操作
<a name="iam-policy-example-for-iac-generator"></a>

下列政策允許存取與 IaC 產生器資源掃描和範本管理相關的 CloudFormation 動作。第一個陳述式會授予描述、列出和啟動資源掃描的許可。它也允許存取其他必要的許可 (`cloudformation:GetResource`、`cloudformation:ListResources` 和 `cloudformation:ListTypes`)，這些許可可讓 IaC 產生器擷取有關資源和可用資源類型的資訊。第二個陳述式會授予建立、刪除、描述、列出和更新產生範本的完整許可。

您也必須將目標 AWS 服務的讀取許可授予使用 IaC 產生器掃描資源的任何人。如需詳細資訊，請參閱[掃描資源所需的 IAM 許可](generate-IaC.md#iac-generator-permissions)。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement":[
        {
            "Sid":"ResourceScanningOperations",
            "Effect":"Allow",
            "Action":[
                "cloudformation:DescribeResourceScan",
                "cloudformation:GetResource",
                "cloudformation:ListResources",
                "cloudformation:ListResourceScanRelatedResources",
                "cloudformation:ListResourceScanResources",
                "cloudformation:ListResourceScans",
                "cloudformation:ListTypes",
                "cloudformation:StartResourceScan"
            ],
            "Resource":"*"
        },
        {
            "Sid":"TemplateGeneration",
            "Effect":"Allow",
            "Action":[
                "cloudformation:CreateGeneratedTemplate",
                "cloudformation:DeleteGeneratedTemplate",
                "cloudformation:DescribeGeneratedTemplate",
                "cloudformation:GetResource",
                "cloudformation:GetGeneratedTemplate",
                "cloudformation:ListGeneratedTemplates",
                "cloudformation:UpdateGeneratedTemplate"
            ],
            "Resource":"*"
        }
    ]
}
```

------