

這是新的 *CloudFormation 範本參考指南*。請更新您的書籤和連結。如需 CloudFormation 入門的說明，請參閱 [AWS CloudFormation 使用者指南](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` 指定之檔案或來源的身分驗證資訊，請使用 `uris` 屬性 (若來源為 URI) 或 `buckets` 屬性 (若來源位於 Amazon S3 儲存貯體)。如需檔案的詳細資訊，請參閱[檔案](aws-resource-init.md#aws-resource-init-files)。如需來源的詳細資訊，請參閱 [來源](aws-resource-init.md#aws-resource-init-sources)。

也可以在 `AWS::CloudFormation::Init` 中繼資料類型中直接指定檔案的身分驗證資訊。資源的 files 金鑰包含名為 `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)
+ [Properties](#w2aac19c23c15c19)
+ [範例](#aws-resource-authentication-examples)

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

若要在您的 CloudFormation 範本中宣告此實體，請使用下列語法：

使用 `AWS::CloudFormation::Authentication` 中繼資料類型時，應該清楚下列事項：
+ 與大多數的 CloudFormation 資源不同，`AWS::CloudFormation::Authentication` 中繼資料類型不包含名為*屬性*的區塊，而是包含一個使用者命名的區塊清單，其中每一個都包含其自身的身分驗證屬性。

  並非所有屬性都與每個身分驗證類型相關。如需詳細資訊，請參閱 [type](#cfn-cloudformation-authentication-type) 屬性。
+ 與大多數的 CloudFormation 資源不同，`AWS::CloudFormation::Authentication` 屬性名稱使用小寫連字 (camel case)。

### 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}}
```

## Properties
<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 身分驗證的秘密金鑰。  
*必要*：有條件限制。只有在 `type` 屬性設為 `S3` 時，才能指定此項目。  
*類型：*字串

`type`  <a name="cfn-cloudformation-authentication-type"></a>
指定身分驗證機制是否使用使用者名稱及密碼 (basic) 或存取金鑰 ID 及秘密金鑰 (S3)。  
若您指定 `basic`，請指定 `username`、`password` 和 `uris` 屬性。  
若您指定 `S3`，請指定 `accessKeyId`、`secretKey` 和 `buckets` (選擇性) 屬性。  
*必要*：是  
*有效值*：`basic` \| `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` 中繼資料中定義，而且 `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` 建立關聯。
+ 範例會使用 `buckets` 屬性指定身分驗證，與 Amazon S3 身分驗證中的情況相同。您也可以使用名稱指定身分驗證。

#### 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
```