简体   繁体   English

cloudformation- 如何在 AWS::CloudFormation::Init 部分的来源中使用 Ref?

[英]cloudformation- how to use Ref in sources of AWS::CloudFormation::Init section?

I'm trying to write sources inside AWS::CloudFormation::Init to take a parameter like below:我正在尝试在AWS::CloudFormation::Init中编写源代码以获取如下参数:

"Parameters" : {
    "Environment" : {
            "Description" : "The Environment",
            "Type" : "String",
            "AllowedValues" : [
                "DEV",
                "QA",
                "UAT",
                "PROD"
            ]
        }
}

Below is my Metadata section:下面是我的元数据部分:

"LaunchTemplate": {
    "Type": "AWS::EC2::LaunchTemplate",
        "Metadata": {
            "Comment" : "Install application",
            "AWS::CloudFormation::Init" : {
                    "configSets" : {
                        "full_install" : [ "prepare", "app-deployment" ]
                    },
            "app-deployment" : {
                    "sources": {
                        "Fn::Join":["",["/app/",{"Ref": "Environment"},"/appname"]] : "https://xyz.s3.amazonaws.com/appname.tar"
                    },
                    "files": {
                        "Fn::Join":["",["/opt/",{"Ref": "Environment"},"/index.html"]] : {
                    "content" : "<html><title>HelloTitle</title><body><h1>header H1</h1></body></html>"
                }

This is not working.这是行不通的。 Can I user Ref function to use parameter this way inside sources and files section??我可以使用Ref function 在sourcesfiles部分中以这种方式使用参数吗?

I don't think you can do this.我不认为你能做到这一点。

However, the workaround would be to use to use commands section.但是,解决方法是使用 to use commands部分。

I can demonstrate the idea behind the workaround using yaml (its what I personal use) including multiline files:我可以使用yaml (我个人使用的)演示解决方法背后的想法,包括多行文件:

commands: 

  04_setup_index_html:
    command: !Sub |
      mkdir -p /opt/${Environment}

      echo "<html><title>HelloTitle</title><body><h1>header H1</h1></body></html>" \
        > /opt/${Environment}/index.html

  05_setup_index2_html_multiline:
    command: !Sub |
      mkdir -p /opt/${Environment}

      cat >/opt/${Environment}/index2.html << EOL
      <html>
          <title>HelloTitle</title>
          <body>
            <h1>header H1</h1>
          </body>
      </html>
      EOL

This way you can create your files and folders dynamically based on the template Parameters .这样您就可以根据模板Parameters动态创建文件和文件夹。

Same of course would be possible in json , but I don't have a tested working example right now.当然json也可以,但我现在没有经过测试的工作示例。 The snipped in yaml provided works for sure. yaml中的片段肯定可以正常工作。

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

相关问题 AWS Cloudformation-如何在 json/yaml 模板中处理字符串大写或小写 - AWS Cloudformation- How to do string Uppercase or lowercase in json/yaml template 如何在 cloudformation 的参考参数中使用 AWS principal - How to use AWS principal in reference parameter in cloudformation AWS CloudFormation - 在 !Sub 中使用 !Ref - AWS CloudFormation - using !Ref inside !Sub CloudFormation 模板文件——如何在字符串中间使用 ref 函数来引用参数 - CloudFormation template file -- how to use ref function in the middle of a string to reference a parameter AWS CloudFormation链接功能 - AWS CloudFormation chaining functions CloudFormation-如何使用Sub内部函数? - CloudFormation - How to use Sub intrinsic function? 我可以在 AWS Cloudformation json 模板的“参数”中使用“Fn::Join”吗 - Can I use "Fn::Join" in "Parameters" of AWS Cloudformation json template AWS CloudFormation:“参数[subnetIds]无效” - AWS CloudFormation: “Parameter [subnetIds] is invalid” 如何在AWS中使用CloudFormation模板包含&#39;mysql_secure_installation&#39; - How to include 'mysql_secure_installation' using CloudFormation template in AWS 如何在cloudformation中更改默认的根EBS大小? [AWS] - How to change default root EBS size in cloudformation? [AWS]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM