

# CloudFormation 基于 IAM 身份的策略示例
<a name="security_iam_id-based-policy-examples"></a>

默认情况下，用户和角色没有创建或修改 CloudFormation 资源的权限。他们也无法使用 Amazon Web Services 管理控制台、Amazon Command Line Interface (Amazon CLI) 或 Amazon API 执行任务。要授予用户对所需资源执行操作的权限，IAM 管理员可以创建 IAM 策略。管理员随后可以向角色添加 IAM 策略，用户可以代入角色。有关更多信息，请参阅 [为 CloudFormation 定义基于 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` 参数（仅适用于 Amazon 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` 参数（仅适用于 Amazon 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 在堆栈或堆栈集上添加或删除标签。用于添加或删除标签的操作应作为条件键的值进行添加。以下策略会授予对变更操作 `CreateStack` 的 `TagResource` 权限和 `UntagResource` 权限。

------
#### [ 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` 登录到组织管理账户，则用户还可以仅对以 OU（ID 为 `ou-1fsfsrsdsfrewr`）为目标的堆栈集执行操作，并可以仅对以 Amazon Web Services 账户（ID 为 `987654321012`）为目标的堆栈集（ID 为 `stack-set-id`）执行操作。

如果堆栈集模板包含除策略中指定的资源类型以外的其他资源类型，或者如果部署的目标 OU 或账户 ID 不是在对应管理账户和堆栈集的策略中指定的内容，则堆栈集操作将失败。

这些策略限制仅在堆栈集操作以 `us-east-1`、`us-west-2` 或 `eu-west-2` Amazon Web Services 区域 为目标时适用。

------
#### [ 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 生成器能够检索有关资源和可用资源类型的信息。第二条语句授予创建、删除、描述、列出和更新所生成模板的全部权限。

您还必须向使用 IaC 生成器扫描资源的任何人授予目标 Amazon 服务的读取权限。有关更多信息，请参阅 [扫描资源所需的 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":"*"
        }
    ]
}
```

------