首页 > 解决方案 > 获取通过 LaunchConfiguration 创建的 EC2 的属性

问题描述

我想获取我通过 LaunchConfiguration 创建的 EC2 的 PrivateIP 属性。

我需要该属性,以便我可以将 A 类型的 dns 记录分配给实例以用于其他目的。

这是我的代码:

Resources:
    webLaunchConfig:
      Type: 'AWS::AutoScaling::LaunchConfiguration'
      Properties:
        ImageId: !Ref webEc2AMI
        InstanceType: !Ref ec2WebInstanceType
        SecurityGroups: !Ref webEc2SG
        UserData:
          'Fn::Base64': !Sub >
            #!/bin/bash -xe

            apt update -y
    dnsWebServerName:
      Type: 'AWS::Route53::RecordSet'
      Properties:
        HostedZoneId: !Ref hostedZoneId
        Comment: DNS name for my db server.
        Name: !Ref dnsWebServerNamePar
        Type: A
        TTL: '900'
        ResourceRecords:
          - !GetAtt webLaunchConfig.PrivateIp

...当我尝试启动它时,我收到此错误:

模板包含错误。:模板错误:资源 webLaunchConfig 不支持 Fn::GetAtt 中的属性类型 PrivateIp

...表明我不支持我正在尝试做的事情。虽然必须有一种方法来实现这一点。

你知道怎么做吗?或者解决方法?

标签: amazon-cloudformation

解决方案


可悲的是你不能这样做AWS::AutoScaling::LaunchConfiguration只是要启动的实例的蓝图。因此它不提供有关 instance的信息PrivateIp。获取PrivateIp您必须实际启动实例。

为此,您必须使用AWS::EC2::Instance。但是AWS::EC2::Instance不支持从 ``AWS::AutoScaling::LaunchConfiguration . So either you have to change your LaunchConfiguration intoLaunchTemplate or just create instance directly usingAWS::EC2::Instance` 而不是任何模板启动。


推荐阅读