简体   繁体   English

在引导新 EC2 实例时使用 UserData 中的内部函数

[英]Using an intrinsic function within UserData when bootstrapping a new EC2 instance

I have written cloudformation which consists of an EC2 instance and a RDS database.我编写了由 EC2 实例和 RDS 数据库组成的 cloudformation。 I wish to get the endpoint address from the RDS, which i can via the intrinsic function GetAtt, and pass it into the EC2 instance in a text file.我希望从 RDS 获取端点地址,我可以通过内部函数 GetAtt 获取该地址,并将其以文本文件的形式传递到 EC2 实例中。 Below is the code i've tried however its not working and giving me what i want.下面是我尝试过的代码,但它不起作用并给了我我想要的。

Resources:
MyEC2Instance:
Type: AWS::EC2::Instance
Properties:
    ImageId: ami-74e6b80d
    InstanceType: t2.nano
    SecurityGroups:
      - Ref: MySecurityGroup
    KeyName:
      - Ref: MyKeyPair
    UserData:
      Fn::Base64:
        Fn::Sub:
          - |
            #!/bin/bash
            touch /home/ubuntu/touch1.txt
          - DatabaseAddress: !GetAtt MyDatabase.Endpoint.Address
            echo "My address is ${DatabaseAddress}" >> touch1.txt
    DependsOn: MyDatabase

You can fetch attributes from a resource without the use of GetAtt while in a Sub function.Sub函数中,您可以在不使用GetAtt情况下从资源中获取属性。

Example:示例:

UserData:
  Fn::Base64:
    Fn::Sub:
      - |
        #!/bin/bash
        touch /home/ubuntu/touch1.txt
        echo "My address is ${MyDatabase.Endpoint.Address}" >> /home/ubuntu/touch1.txt

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM