Reference Parameter Value in UserData in AWS Cloudformation

前提是你 提交于 2019-12-11 05:57:38

问题


I have this under parameter section ,

Parameters:
  PlatformSelect:
    Description: Cockpit platform Select.
    Type: String
    Default: qa-1
    AllowedValues: [qa-1, qa-2, staging, production]

I need to reference this value in my UserData. I’m using Mappings in between.

Mappings:
  bootstrap:
    ubuntu:
      print: echo ${PlatformSelect} >>test.txt

Resources:
  EC2Instance:
    Type: AWS::EC2::Instance
    Properties:
      InstanceType: !Ref ‘InstanceType’
      KeyName: !Ref ‘KeyName’
      Tags:
      - Key: Name
        Value: Test
      UserData:
        Fn::Base64:
          Fn::Join:
          - ‘’
          - - |
              #!/bin/bash
            - Fn::FindInMap:
              - bootstrap
              - ubuntu
              - print
            - |2+

This is not working. Not sure the way I refer it is wrong in first place!!

Should I use something before it like, ‘${AWS::Parameters:PlatformSelect}’ ?


回答1:


Is there a reason why you are using Mapping in between?

You could easily use !Sub instead

Resources:
  EC2Instance:
    Type: AWS::EC2::Instance
    Properties:
      InstanceType: !Ref InstanceType
      KeyName: !Ref KeyName
      Tags:
        - Key: Name
          Value: Test
      UserData:
        Fn::Base64:
          !Sub |
            #!/bin/bash
            ${PlatformSelect}



回答2:


What about a combination of Fn::Join and Ref

UserData:
        Fn::Base64:
          Fn::Join:
            - ''
            - - '#!/bin/bash\n'
              - 'print: echo'
              - !Ref 'PlatformSelect' 
              - '>>test.txt\n'


来源:https://stackoverflow.com/questions/44775534/reference-parameter-value-in-userdata-in-aws-cloudformation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!