AWS::CloudFormation::Authentication
使用 AWS::CloudFormation::Authentication 资源可为通过 AWS::CloudFormation::Init 资源指定的文件或源指定身份验证凭证。
要包含使用 AWS::CloudFormation::Init 指定的文件或源的身份验证信息,请在源是 URI 时使用 uris 属性,或者在源是 Amazon S3 存储桶时使用 buckets 属性。更多有关文件信息,请参阅 文件。更多有关来源的信息,请参阅 源。
您也可以直接在 AWS::CloudFormation::Init 资源中指定文件的身份验证信息。资源的文件密钥包含名为 authentication 属性。您可以直接使用 authentication 属性来关联在 资源中指定的验证信息和文件。AWS::CloudFormation::Authentication
对于文件,Amazon CloudFormation 将以下列顺序查找验证信息:
-
authenticationAWS::CloudFormation::Init密钥的files属性。 -
uris资源的buckets或AWS::CloudFormation::Authentication属性。
对于源,CloudFormation 在 AWS::CloudFormation::Authentication 资源的 uris 或 buckets 属性中查找身份验证信息。
语法
要在 CloudFormation 模板中声明此实体,请使用以下语法:
您在使用 AWS::CloudFormation::Authentication 类型时应了解以下注意事项:
-
与大多数 CloudFormation 资源不同,
AWS::CloudFormation::Authentication类型不包含名为 Properties 的块,而是包含一组用户命名的块,每个块包含自己的身份验证属性。并非所有属性都与每个身份验证类型相关;请参阅 type 属性以了解更多详细信息。
-
与大多数 CloudFormation 资源不同,属性名称使用的是小驼峰式命名法。
JSON
{ "Type" : "AWS::CloudFormation::Authentication" { "String" : { "accessKeyId" :String, "buckets" : [String, ...], "password" :String, "secretKey" :String, "type" :String, "uris" : [String, ...], "username" :String, "roleName" :String} } }
YAML
Type: AWS::CloudFormation::AuthenticationString: accessKeyId:Stringbuckets: -Stringpassword:StringsecretKey:Stringtype:Stringuris: -Stringusername:StringroleName:String
属性
accessKeyId-
指定 S3 验证的访问密钥 ID。
必填:条件性。只有在将
type属性设置为"S3"时才能指定。类型:字符串
buckets-
要与 S3 验证凭证关联的 Amazon S3 存储桶列表(以逗号分隔)。
必填:条件性。只有在将
type属性设置为"S3"时才能指定。类型:字符串值列表
password-
指定基础验证密码。
必填:条件性。只有在将 type 属性设置为
"basic"时才能指定。类型:字符串
secretKey-
指定 S3 验证的访问密钥 ID。
必填:条件性。只有在将
type属性设置为"S3"时才能指定。类型:字符串
type-
指定身份验证方案是使用用户名和密码 (“basic”),还是使用访问密钥 ID 和私有密钥 (“S3”)。
如果您指定
"basic",则指定username、password和uris属性。如果您指定
"S3",则指定accessKeyId、secretKey和buckets(可选) 属性。必需:是
有效值:
basic|S3 uris-
要与基础级验证凭证关联的 URI 列表 (以逗号分隔)。该授权适用于指定的 URI 和任何更多特定 URI。例如,如果您指定
http://www.example.com,则授权也将应用于http://www.example.com/test。必填:条件性。只有在将
type属性设置为"basic"时才能指定。类型:字符串值列表
username-
指定用于基本身份验证的用户名。
必填:条件性。只有在将 type 属性设置为
"basic"时才能指定。类型:字符串
roleName-
描述基于角色的身份验证的角色。
重要
此角色必须包含在附加到 EC2 实例的实例配置文件中。一个实例配置文件只能包含一个 IAM 角色。
必填:条件性。只有在将
type属性设置为"S3"时才能指定。类型:字符串。
示例
注意
与大多数资源不同,AWS::CloudFormation::Authentication 类型定义了用户命名的块的列表,其中每个块均包含使用小驼峰式命名法的验证信息。
EC2 Web 服务器身份验证
本模板片段显示了如何从 EC2 实例内的私有 S3 存储桶获取文件。用于身份验证的凭证是在 AWS::CloudFormation::Authentication 资源中定义的,并由 files 节中的 AWS::CloudFormation::Init 资源引用。
JSON
"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" : "CfnKeys" }, "secretKey" : { "Fn::GetAtt": [ "CfnKeys", "SecretAccessKey" ] } } } }, "Properties": {EC2 Resource Properties ...} }
YAML
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: "CfnKeys" secretKey: Fn::GetAtt: - "CfnKeys" - "SecretAccessKey" Properties:EC2 Resource Properties ...
指定基本和 S3 身份验证
以下示例模板片段包含基本和 S3 身份验证类型。
JSON
"AWS::CloudFormation::Authentication" : { "testBasic" : { "type" : "basic", "username" : { "Ref" : "UserName" }, "password" : { "Ref" : "Password" }, "uris" : [ "example.com/test" ] }, "testS3" : { "type" : "S3", "accessKeyId" : { "Ref" : "AccessKeyID" }, "secretKey" : { "Ref" : "SecretAccessKeyID" }, "buckets" : [ "DOC-EXAMPLE-BUCKET1" ] } }
YAML
AWS::CloudFormation::Authentication: testBasic: type: "basic" username: Ref: "UserName" password: Ref: "Password" uris: - "example.com/test" testS3: type: "S3" accessKeyId: Ref: "AccessKeyID" secretKey: Ref: "SecretAccessKeyID" buckets: - "myawsbucket"
IAM 角色
以下示例显示如何使用 IAM 角色:
-
myRole是一个 AWS::IAM::Role 资源。 -
运行
cfn-init的 Amazon EC2 实例通过实例配置文件与myRole关联。 -
与 Amazon S3 身份验证中一样,该示例使用
buckets属性指定身份验证。也可通过名称来指定身份验证。
JSON
"AWS::CloudFormation::Authentication": { "rolebased" : { "type": "S3", "buckets": [ "myBucket" ], "roleName": { "Ref": "myRole" } } }
YAML
AWS::CloudFormation::Authentication: rolebased: type: "S3" buckets: - "myBucket" roleName: Ref: "myRole"