AWS cfn-init是否需要DescribeStackResource的配置文件/角色?

问题描述:

this页:AWS cfn-init是否需要DescribeStackResource的配置文件/角色?

要使用AWS CloudFormation引导功能,您需要提供AWS凭据 引导脚本。我们强烈建议您在实例启动时在EC2实例 上分配一个IAM角色。

这看起来很直截了当,但是当我查看来自AWS文档各处的任何示例时,他们从不为此设置角色或配置文件。例如,here

我错过了什么?有没有cfn-init需要额外权限的情况,而不是其他情况?

不,您不再需要将cloudformation:DescribeStackResource添加到与实例配置文件关联的角色的任何策略中,以便访问CloudFormation元数据。诸如cfn-get-metadata和cfn-init之类的脚本使用特殊的CFN标头而不是标准的AWS Authorization header进行授权。从CFN脚本的请求看起来像这样:

# This command succeeds regardless of your instance profile 
cfn-get-metadata --region us-west-1 --stack cftest --resource LaunchConfig --key AWS::CloudFormation::Init 

GET /?Action=DescribeStackResource&StackName=cftest&Version=2010-05-15&ContentType=JSON&LogicalResourceId=LaunchConfig HTTP/1.1 
Host: cloudformation.us-west-1.amazonaws.com 
Connection: keep-alive 
Accept: application/json 
Accept-Encoding: gzip, deflate 
Authorization: CFN_V1 ewogICJwcml2YXRlSX(truncated)==:b9ZM3/EnzeX(truncated)= 
User-Agent: CloudFormation Tools 

的CFN授权报头是http://169.254.169.254/latest/dynamic/instance-identity/documenthttp://169.254.169.254/latest/dynamic/instance-identity/signature一个级联和只允许该实例,以查看从它自己的堆栈的CloudFormation元数据。

相反,使用实例简档的请求看起来像这样:如IAM Roles for EC2描述

# This command fails if you don’t have cloudformation:DescribeStackResource permission! 
aws cloudformation --region us-west-1 describe-stack-resource --stack-name cftest --logical-resource-id LaunchConfig 

POST/HTTP/1.1 
Host: cloudformation.us-west-1.amazonaws.com 
Accept-Encoding: identity 
Content-Length: 95 
X-Amz-Date: 20160630T010040Z 
User-Agent: aws-cli/1.10.43 Python/2.7.11+ Linux/4.4.0-28-generic botocore/1.4.33 
X-Amz-Security-Token: FQoDY(truncated-token)= 
Content-Type: application/x-www-form-urlencoded 
Authorization: AWS4-HMAC-SHA256 Credential=ASIA(truncated)/20160630/us-west-1/cloudformation/aws4_request, SignedHeaders=host;x-amz-date;x-amz-security-token, Signature=fbad7aeef75186cb18bbd44810c4d0379d7d1cf1b8a80be14ea1e3192d2ec531 

Action=DescribeStackResource&StackName=cftest&Version=2010-05-15&LogicalResourceId=LaunchConfig 

实例轮廓临时凭证与http://169.254.169.254/latest/meta-data/iam/security-credentials/取出。

(注:收集这些要求,我跑nc -l 80 &cfn-get-metadata --url http://localhostaws --endpoint-url http://localhost

这CFNSigner功能被添加到客户端之间的AWS-CFN的自举-1.1(2012-03)和aws- cfn-bootstrap-1.3.6(2012-09)。在2012年之前,您确实需要使用cloudformation的角色:DescribeStackResource权限,如2011年文档Boostrapping Applications With AWS CloudFormation中所述。请注意,只有cfn- *脚本使用CFNSigner;如果你想使用aws cloudformation,你需要确保你的角色允许它。

clou-init脚本需要连接到AWS服务以检索您在CFN模板中提供的元数据。

为了连接到AWS服务,您可以直接将访问/密钥传递给cfn-init脚本,或者将具有必要权限的IAM角色附加到EC2实例,以便cfn-init脚本将使用IAM角色连接到AWS服务。

进一步阅读:http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-roles-for-amazon-ec2.html

全部由CFN-INIT提供的样本假定您有连接到该实例,使访问/分泌键不直接传递到CFN-init脚本IAM角色。

+0

这似乎并非如此。可以通过IAM角色或显式凭证使用'cfn-get-metadata'而不需要任何权限。但是如果你用'aws cloudformation describe-stack-resource'做同样的事情,它就会失败。 – 2017-05-16 09:45:34