[英]How to create a file without any content using CloudFormation Init?
I want an empty file to be created by CloudFormation.我想要一个由 CloudFormation 创建的空文件。
I tried providing an empty string in the 'content' field in my CloudFormation template yaml file.我尝试在我的 CloudFormation 模板 yaml 文件的“内容”字段中提供一个空字符串。
Resources:
Instance:
Type: AWS::EC2::Instance
Metadata:
AWS::CloudFormation::Init:
config:
files:
/var/log/app.log:
content: ""
mode: '000646'
I get the following error: File specified without source or content
我收到以下错误: File specified without source or content
2023-01-10 14:32:55,132 [DEBUG] Writing content to /var/log/app.log
2023-01-10 14:32:55,132 [ERROR] Error encountered during build of config: File specified without source or content
Traceback (most recent call last):
File "/usr/lib/python3.7/site-packages/cfnbootstrap/construction.py", line 579, in run_config
CloudFormationCarpenter(config, self._auth_config, self.strict_mode).build(worklog)
File "/usr/lib/python3.7/site-packages/cfnbootstrap/construction.py", line 267, in build
self._config.files, self._auth_config)
File "/usr/lib/python3.7/site-packages/cfnbootstrap/file_tool.py", line 131, in apply
self._write_file(f, attribs, auth_config)
File "/usr/lib/python3.7/site-packages/cfnbootstrap/file_tool.py", line 221, in _write_file
raise ToolError("File specified without source or content")
cfnbootstrap.construction_errors.ToolError: File specified without source or content
2023-01-10 14:32:55,137 [ERROR] -----------------------BUILD FAILED!------------------------
2023-01-10 14:32:55,137 [ERROR] Unhandled exception during build: File specified without source or content
Traceback (most recent call last):
File "/opt/aws/bin/cfn-init", line 181, in <module>
worklog.build(metadata, configSets, strict_mode)
File "/usr/lib/python3.7/site-packages/cfnbootstrap/construction.py", line 137, in build
Contractor(metadata, strict_mode).build(configSets, self)
File "/usr/lib/python3.7/site-packages/cfnbootstrap/construction.py", line 567, in build
self.run_config(config, worklog)
File "/usr/lib/python3.7/site-packages/cfnbootstrap/construction.py", line 579, in run_config
CloudFormationCarpenter(config, self._auth_config, self.strict_mode).build(worklog)
File "/usr/lib/python3.7/site-packages/cfnbootstrap/construction.py", line 267, in build
self._config.files, self._auth_config)
File "/usr/lib/python3.7/site-packages/cfnbootstrap/file_tool.py", line 131, in apply
self._write_file(f, attribs, auth_config)
File "/usr/lib/python3.7/site-packages/cfnbootstrap/file_tool.py", line 221, in _write_file
raise ToolError("File specified without source or content")
cfnbootstrap.construction_errors.ToolError: File specified without source or content
I get the same error if I omit the 'content' field.如果我省略“内容”字段,我会得到同样的错误。
Can you tell me the proper way to instruct CloudFormation to create an empty file?你能告诉我指示 CloudFormation 创建空文件的正确方法吗?
As brushtakopo pointed out, CloudFormation Init doesn't support creating empty files so the only option appears to be to use a command.正如brushtakopo指出的那样,CloudFormation Init 不支持创建空文件,因此唯一的选择似乎是使用命令。
I just wanted to include the corresponding command since he didn't include it in his answer.我只是想包含相应的命令,因为他没有在他的回答中包含它。
Resources:
Instance:
Type: AWS::EC2::Instance
Metadata:
AWS::CloudFormation::Init:
config:
commands:
a_createLogFile:
command: "touch /var/log/app.log"
test: "test ! -e /var/log/app.log"
b_chmodLogFile:
command: "chmod 646 /var/log/app.log"
test: "test -e /var/log/app.log"
Note: The command prefixed with a_
creates the file and the command prefixed with b_
changes its access permissions.注意:以a_
为前缀的命令创建文件,以b_
为前缀的命令更改其访问权限。 The prefixes are to ensure the first command is executed before the second command since CloudFormation executes commands in alphabetical order.前缀是为了确保第一个命令在第二个命令之前执行,因为 CloudFormation 按字母顺序执行命令。
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-init.html https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-init.html
The commands are processed in alphabetical order by name.命令按名称的字母顺序处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.