

这是新的《CloudFormation 模板参考指南》**。请更新您的书签和链接。有关开始使用 CloudFormation 的帮助，请参阅《AWS CloudFormation 用户指南》[https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html)。

# `AWS::CloudFormation::Authentication`
<a name="aws-resource-authentication"></a>

使用 `AWS::CloudFormation::Authentication` 元数据类型为通过 [`AWS::CloudFormation::Init`](aws-resource-init.md) 元数据类型指定的文件或源指定身份验证凭证。

要包含使用 `AWS::CloudFormation::Init` 指定的文件或源的身份验证信息，请在源是 URI 时使用 `uris` 属性，或者在源是 Amazon S3 存储桶时使用 `buckets` 属性。更多有关文件信息，请参阅 [文件](aws-resource-init.md#aws-resource-init-files)。有关来源的更多信息，请参阅[源](aws-resource-init.md#aws-resource-init-sources)。

您也可以直接在 `AWS::CloudFormation::Init` 元数据类型中指定文件的身份验证信息。资源的文件密钥包含名为 `authentication` 属性。您可以直接使用 `authentication` 属性将 `AWS::CloudFormation::Authentication` 元数据类型中定义的身份验证信息直接与文件关联。

对于文件，CloudFormation 将以下列顺序查找验证信息：

1. `authentication` `AWS::CloudFormation::Init` 密钥的 `files` 属性。

1. `AWS::CloudFormation::Authentication` 元数据的 `uris` 或 `buckets` 属性。

对于源，CloudFormation 会在 `AWS::CloudFormation::Authentication` 元数据的 `uris` 或 `buckets` 属性中查找身份验证信息。

**Topics**
+ [语法](#aws-resource-cloudformation-authentication-syntax)
+ [属性](#w2aac19c23c15c19)
+ [示例](#aws-resource-authentication-examples)

## 语法
<a name="aws-resource-cloudformation-authentication-syntax"></a>

要在 CloudFormation 模板中声明此实体，请使用以下语法：

在使用 `AWS::CloudFormation::Authentication` 元数据类型时应了解以下注意事项：
+ 与大多数 CloudFormation 资源不同，`AWS::CloudFormation::Authentication` 元数据不包含名为 *Properties* 的块，而是包含一组用户命名的块，每个块包含自己的身份验证属性。

  并非所有属性都与每个身份验证类型相关联。有关更多信息，请参阅 [type](#cfn-cloudformation-authentication-type) 属性。
+ 与大多数 CloudFormation 资源不同，`AWS::CloudFormation::Authentication` 属性名称使用的是小驼峰式命名法。

### JSON
<a name="aws-resource-cloudformation-authentication-syntax.json"></a>

```
{
  "AWS::CloudFormation::Authentication" {
    "String" : {
      "accessKeyId" : String,
      "buckets" : [ String, ... ],
      "password" : String,
      "secretKey" : String,
      "type" : String,
      "uris" : [ String, ... ],
      "username" : String,
      "roleName" : String
    }
  }
}
```

### YAML
<a name="aws-resource-cloudformation-authentication-syntax.yaml"></a>

```
AWS::CloudFormation::Authentication
String:
  accessKeyId: String
  buckets:
    - String
  password: String
  secretKey: String
  type: String
  uris:
    - String
  username: String
  roleName: String
```

## 属性
<a name="w2aac19c23c15c19"></a>

`accessKeyId`  <a name="cfn-cloudformation-authentication-accesskeyid"></a>
指定 S3 验证的访问密钥 ID。  
*必填*：条件性。只有在将 `type` 属性设置为 `S3` 时才能指定。  
*类型*：字符串

`buckets`  <a name="cfn-cloudformation-authentication-buckets"></a>
要与 S3 验证凭证关联的 Amazon S3 存储桶列表（以逗号分隔）。  
*必填*：条件性。只有在将 `type` 属性设置为 `S3` 时才能指定。  
*类型*：字符串值列表

`password`  <a name="cfn-cloudformation-authentication-password"></a>
指定基础验证密码。  
*必填*：条件性。只有在将 type 属性设置为 `basic` 时才能指定。  
*类型*：字符串

`secretKey`  <a name="cfn-cloudformation-authentication-secretkey"></a>
指定 S3 验证的访问密钥 ID。  
*必填*：条件性。只有在将 `type` 属性设置为 `S3` 时才能指定。  
*类型*：字符串

`type`  <a name="cfn-cloudformation-authentication-type"></a>
指定身份验证方案是使用用户名和密码（基本），还是使用访问密钥 ID 和私有密钥（S3）。  
如果您指定 `basic`，则指定 `username`、`password` 和 `uris` 属性。  
如果您指定 `S3`，则指定 `accessKeyId`、`secretKey` 和 `buckets` (可选) 属性。  
*必需*：是  
*有效值*：`basic` \$1`S3` 

`uris`  <a name="cfn-cloudformation-authentication-uris"></a>
要与基础级验证凭证关联的 URI 列表 (以逗号分隔)。该授权适用于指定的 URI 和任何更多特定 URI。例如，如果您指定 `http://www.example.com`，则授权也将应用于 `http://www.example.com/test`。  
*必填*：条件性。只有在将 `type` 属性设置为 `basic` 时才能指定。  
*类型*：字符串值列表

`username`  <a name="cfn-cloudformation-authentication-username"></a>
指定用于基本身份验证的用户名。  
*必填*：条件性。只有在将 type 属性设置为 `basic` 时才能指定。  
*类型*：字符串

`roleName`  <a name="cfn-cloudformation-authentication-rolename"></a>
描述基于角色的身份验证的角色。  
此角色必须包含在附加到 EC2 实例的实例配置文件中。一个实例配置文件只能包含一个 IAM 角色。
*必填*：条件性。只有在将 `type` 属性设置为 `S3` 时才能指定。  
*类型*：字符串。

## 示例
<a name="aws-resource-authentication-examples"></a>

**Topics**
+ [EC2 Web 服务器身份验证](#aws-resource-cloudformation-authentication-example1)
+ [指定基本和 S3 身份验证](#aws-resource-cloudformation-authentication-example2)
+ [IAM 角色](#aws-resource-cloudformation-authentication-example3)

### EC2 Web 服务器身份验证
<a name="aws-resource-cloudformation-authentication-example1"></a>

本模板片段显示了如何从 EC2 实例内的私有 S3 存储桶获取文件。用于身份验证的凭证是在 `AWS::CloudFormation::Authentication` 元数据中定义的，并由 *files* 部分中的 `AWS::CloudFormation::Init` 元数据引用。

#### JSON
<a name="aws-resource-cloudformation-authentication-example1.json"></a>

```
"WebServer": {
   "Type": "AWS::EC2::Instance",
   "DependsOn" : "BucketPolicy",
   "Metadata" : {
      "AWS::CloudFormation::Init" : {
         "config" : {
            "packages" : { "yum" : { "httpd" : [] } },
            "files" : {
               "/var/www/html/index.html" : {
                  "source" : {
                     "Fn::Join" : [
                        "", [ "http://s3.amazonaws.com/", { "Ref" : "BucketName" }, "/index.html" ]
                     ]
                  },
                  "mode"   : "000400",
                  "owner"  : "apache",
                  "group"  : "apache",
                  "authentication" : "S3AccessCreds"
               }
            },
            "services" : {
               "sysvinit" : {
                  "httpd" : { "enabled" : "true", "ensureRunning" : "true" }
               }
            }
         }
      },
      "AWS::CloudFormation::Authentication" : {
         "S3AccessCreds" : {
            "type" : "S3",
            "accessKeyId" : { "Ref" : "AccessKeyID" },
            "secretKey" : { "Ref" : "SecretAccessKey" }
         }
      }
   },
   "Properties": {
   EC2 Resource Properties ...
   }
}
```

#### YAML
<a name="aws-resource-cloudformation-authentication-example1.yaml"></a>

```
WebServer: 
  Type: AWS::EC2::Instance
  DependsOn: BucketPolicy
  Metadata: 
    AWS::CloudFormation::Init: 
      config: 
        packages: 
          yum: 
            httpd: []
        files: 
          /var/www/html/index.html: 
            source: !Join
              - ''
              - - 'http://s3.amazonaws.com/'
                - !Ref BucketName
                - '/index.html'
            mode: '000400'
            owner: apache
            group: apache
            authentication: S3AccessCreds
        services: 
          sysvinit: 
            httpd: 
              enabled: 'true'
              ensureRunning: 'true'
    AWS::CloudFormation::Authentication: 
      S3AccessCreds: 
        type: S3
        accessKeyId: !Ref AccessKeyID
        secretKey: !Ref SecretAccessKey
  Properties: 
  EC2 Resource Properties ...
```

### 指定基本和 S3 身份验证
<a name="aws-resource-cloudformation-authentication-example2"></a>

以下示例模板片段包含*基本*和 *S3* 身份验证类型。

#### JSON
<a name="aws-resource-cloudformation-authentication-example2.json"></a>

```
"AWS::CloudFormation::Authentication" : {
   "testBasic" : {
      "type" : "basic",
      "username" : { "Ref" : "UserName" },
      "password" : { "Ref" : "Password" },
      "uris" : [ "example.com/test" ]
   },
   "testS3" : {
      "type" : "S3",
      "accessKeyId" : { "Ref" : "AccessKeyID" },
      "secretKey" : { "Ref" : "SecretAccessKey" },
      "buckets" : [{ "Fn::Sub": "${BucketName}" }]
   }
}
```

#### YAML
<a name="aws-resource-cloudformation-authentication-example2.yaml"></a>

```
AWS::CloudFormation::Authentication: 
  testBasic: 
    type: basic
    username: !Ref UserName
    password: !Ref Password
    uris: 
      - 'example.com/test'
  testS3: 
    type: S3
    accessKeyId: !Ref AccessKeyID
    secretKey: !Ref SecretAccessKey
    buckets: 
      - !Sub ${BucketName}
```

### IAM 角色
<a name="aws-resource-cloudformation-authentication-example3"></a>

以下示例显示如何使用 IAM 角色：
+ `myRole` 是一个 [AWS::IAM::Role](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-iam-role.html) 资源。
+ 运行 `cfn-init` 的 Amazon EC2 实例通过实例配置文件与 `myRole` 关联。
+ 与 Amazon S3 身份验证中一样，该示例使用 `buckets` 属性指定身份验证。也可通过名称来指定身份验证。

#### JSON
<a name="aws-resource-cloudformation-authentication-example3.json"></a>

```
"AWS::CloudFormation::Authentication": {
    "rolebased" : {
        "type": "S3",
        "buckets": [{ "Fn::Sub": "${BucketName}" }],
        "roleName": { "Ref": "myRole" }
    }
}
```

#### YAML
<a name="aws-resource-cloudformation-authentication-example3.yaml"></a>

```
AWS::CloudFormation::Authentication:
  rolebased:
    type: S3
    buckets:
      - !Sub ${BucketName}
    roleName: !Ref myRole
```