簡體   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