[英]How to create an 'AWS::SSM::Document' with DocumentType of Package using CloudFormation
此 AWS CloudFormation 文档建议可以管理 DocumentType 为“Package”的“AWS::SSM::Document”资源。 然而,实现这一目标所需的“内容”仍然是个谜。
是否可以通过 CloudFormation 创建一个“包”类型的文档,如果可以,这个有效的 CLI 命令相当于一个 CloudFormation 模板(最好使用 YAML 格式)?
ssm create-document --name my-package --content "file://manifest.json" --attachments Key="SourceUrl",Values="s3://my-s3-bucket" --document-type Package
尝试失败。 使用的内容是使用 CLI 选项时提供的 manifest.json 的内联版本。 使用 CloudFormation 时似乎没有指定 AttachmentSource 的选项:
AWSTemplateFormatVersion: 2010-09-09
Resources:
Document:
Type: AWS::SSM::Document
Properties:
Name: 'my-package'
Content: !Sub |
{
"schemaVersion": "2.0",
"version": "Auto-Generated-1579701261956",
"packages": {
"windows": {
"_any": {
"x86_64": {
"file": "my-file.zip"
}
}
}
},
"files": {
"my-file.zip": {
"checksums": {
"sha256": "sha...."
}
}
}
}
DocumentType: Package
CloudFormation 错误
AttachmentSource not provided in the input request. (Service: AmazonSSM; Status Code: 400; Error Code: InvalidParameterValueException;
是的,这是可能的! 我已经使用DocumentType: Package
成功创建了一个资源,并且在堆栈成功后,该包显示在 Distributor Packages 下的 SSM 控制台中。
您的 YAML 已准备就绪,但您还需要包含现在可用的Attachments
属性。
这是一个工作示例:
AWSTemplateFormatVersion: "2010-09-09"
Description: Sample to create a Package type Document
Parameters:
S3BucketName:
Type: "String"
Default: "my-sample-bucket-for-package-files"
Description: "The name of the S3 bucket."
Resources:
CrowdStrikePackage:
Type: AWS::SSM::Document
Properties:
Attachments:
- Key: "SourceUrl"
Values:
- !Sub "s3://${S3BucketName}"
Content:
!Sub |
{
"schemaVersion": "2.0",
"version": "1.0",
"packages": {
"windows": {
"_any": {
"_any": {
"file": "YourZipFileName.zip"
}
}
}
},
"files": {
"YourZipFileName.zip": {
"checksums": {
"sha256": "7981B430E8E7C45FA1404FE6FDAB8C3A21BBCF60E8860E5668395FC427CE7070"
}
}
}
}
DocumentFormat: "JSON"
DocumentType: "Package"
Name: "YourPackageNameGoesHere"
TargetType: "/AWS::EC2::Instance"
注意:对于Attachments
属性,在使用DocumentType: Package
时必须使用SourceUrl
键。 创建过程将在此 S3 存储桶 URL 后附加一个“/”,并将其与您在清单中列出的每个文件名连接起来,该文件名是创建包时的Content
属性。
似乎没有直接的方法可以通过 CloudFormation (CFN) 创建带有附件的 SSM 文档。 您可以使用一种变通方法,例如使用支持的 Lambda CFN,您将使用 Lambda 调用 API SDK 来创建 SSM 文档,然后使用 CFN 中的自定义资源来调用该 Lambda。
关于如何实施此解决方案有一些注意事项,如下所示:
sam deploy
而不是 CFN create stack
。data
json 中来从 lambda 获取新创建资源的信息到 CFN,CFN 可以将其与 !GetAtt CustomResrc.Attribute 一起使用,您可以在此处找到更多详细信息。此解决方案有一些缺点:
PS:如果您不必严格在CFN上工作,那么您可以尝试使用Terraform:https ://www.terraform.io/docs/providers/aws/r/ssm_document.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.