Home > front end >  How to create a file without any content using CloudFormation Init?
How to create a file without any content using CloudFormation Init?

Time:01-11

I want an empty file to be created by CloudFormation.

I tried providing an empty string in the 'content' field in my CloudFormation template yaml file.

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

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?

CodePudding user response:

You need to specify a content. If you really want to create an empty file, then I suggest to use commands.

CodePudding user response:

As brushtakopo pointed out, CloudFormation Init doesn't support creating empty files so the only option appears to be to use a command.

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. The prefixes are to ensure the first command is executed before the second command since CloudFormation executes commands in alphabetical order.

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

The commands are processed in alphabetical order by name.

  • Related