简体   繁体   English

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

[英]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 创建空文件的正确方法吗?

You need to specify a content.您需要指定一个内容。 If you really want to create an empty file, then I suggest to use commands .如果你真的想创建一个空文件,那么我建议使用commands

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.

相关问题 使用方法:Sub 或:Join in "AWS::CloudFormation,:Init", file key - How to use !Sub or !Join in "AWS::CloudFormation::Init", file key 如何使用 AWS CloudFormation 创建 Amazon VPC? - How to create an Amazon VPC using AWS CloudFormation? 我如何使用 cloudformation 创建和更新 WAF IPset - how can i create and update WAF IPset using cloudformation 当使用 cloudformation 模板对 dynamodb 表进行任何更改时,如何触发 Function? - How trigger Function when any changes in dynamodb table using cloudformation template? 使用一个 cloudFormation 模板从 zip 文件和 S3 存储桶创建一个 lambda - Create a lambda from zip file and and an S3 bucket using one cloudFormation Template 如何通过 cloudformation 在 sagemaker 中创建无服务器端点? - how to create a serverless endpoint in sagemaker via cloudformation? 如何使用 CloudFormation 模板连接到现有 EC2 实例并执行 shell 文件 - How to connect to an existing EC2 Instance and execute a shell file using CloudFormation Template 使用 CloudFormation 创建 Secrets Manager 但保持值不可见 - Create Secrets Manager using CloudFormation but keeping values not visible 使用 CloudFormation 创建策略 - Create policy with CloudFormation cloudformation时如何在s3中动态创建文件夹 package - How to dynamically create a folder in s3 when doing cloudformation package
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM