繁体   English   中英

如何使用 CloudFormation Init 创建没有任何内容的文件?

[英]How to create a file without any content using CloudFormation Init?

我想要一个由 CloudFormation 创建的空文件。

我尝试在我的 CloudFormation 模板 yaml 文件的“内容”字段中提供一个空字符串。

Resources:
  Instance:
    Type: AWS::EC2::Instance
    Metadata:
      AWS::CloudFormation::Init:
        config:
          files:
            /var/log/app.log:
              content: ""
              mode: '000646'

我收到以下错误: 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

如果我省略“内容”字段,我会得到同样的错误。

你能告诉我指示 CloudFormation 创建空文件的正确方法吗?

您需要指定一个内容。 如果你真的想创建一个空文件,那么我建议使用commands

正如brushtakopo指出的那样,CloudFormation Init 不支持创建空文件,因此唯一的选择似乎是使用命令。

我只是想包含相应的命令,因为他没有在他的回答中包含它。

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"

注意:以a_为前缀的命令创建文件,以b_为前缀的命令更改其访问权限。 前缀是为了确保第一个命令在第二个命令之前执行,因为 CloudFormation 按字母顺序执行命令。

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-init.html

命令按名称的字母顺序处理。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM