首页 > 解决方案 > 如何在 cloudformation 模板中获取 docker 主机的实例 ID

问题描述

我有一个使用模板创建的简单 CloudFormation 堆栈。托管一个 Docker 容器的服务器。

这是创建主机和容器的模板的一部分:

  ContainerHostInstances:
    Type: "AWS::AutoScaling::LaunchConfiguration"
    DependsOn: "AttachGateway"
    Properties:
      AssociatePublicIpAddress: true
      ImageId: "ami-0302f3ec240b9d23c"
      SecurityGroups:
        - Ref: "NginxSecurityGroup"
      InstanceType: "t3.nano"
      IamInstanceProfile: !Ref "ECSHostEC2InstanceProfile"
      KeyName: "test-key-pair"
      UserData:
        Fn::Base64: !Sub |
          #!/bin/bash -xe
          echo ECS_CLUSTER=${MyCluster} >> /etc/ecs/ecs.config
          yum install -y aws-cfn-bootstrap
          /opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource ECSAutoScalingGroup --region ${AWS::Region}

...

  MyTask:
    Type: "AWS::ECS::TaskDefinition"
    Properties:
      Family: "my-task-family"
      ContainerDefinitions:
        - Name: "nginx"
          Essential: true
          Image: "image_url/nginx:latest"
          MemoryReservation: 300
          LogConfiguration:
            LogDriver: "awslogs"
            Options:
              awslogs-group: "nginx"
              awslogs-region: !Ref "AWS::Region"
              awslogs-stream-prefix: "prefix"
              awslogs-datetime-format: "%Y-%m-%d %H:%M:%S.%L"
          PortMappings:
            - ContainerPort: 80
              HostPort: 80

我的问题是我无法在模板中关联弹性 IP 地址。我可以使用控制台来完成它,但如果可能的话,我希望将它放在模板中。

我试过这样:

  ElasticIPAssoc:
    Type: AWS::EC2::EIPAssociation
    Properties:
      AllocationId: "some_id"
      InstanceId: "HOW CAN I GET THIS VALUE"
      NetworkInterfaceId: "OR THAT VALUE"

我看不到获取 instanceId 或其对应的 NetworkInterfaceId 的方法。

标签: amazon-web-servicesamazon-cloudformation

解决方案


推荐阅读