【问题标题】:Unable to fetch cluster name and ASG resource name using CDK无法使用 CDK 获取集群名称和 ASG 资源名称
【发布时间】:2021-07-15 11:02:54
【问题描述】:

我对使用 CDK 很陌生,我正在尝试为我的启动配置生成 user_data。但是遇到问题...从这里获取用户数据并尝试将其调整为我的 CDK 模板:https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/quickref-ecs.html#quickref-ecs-example-1.yaml

这是我的代码

     const asg = new autoscaling.AutoScalingGroup(this, 'ASG', {
            vpc: existingVpc,
            instanceType: ec2.InstanceType.of(ec2.InstanceClass.T3, ec2.InstanceSize.XLARGE),
            machineImage: ecs.EcsOptimizedImage.amazonLinux2(),
            minCapacity: 3,
            desiredCapacity: 3,
            maxCapacity: 10,
            instanceMonitoring: autoscaling.Monitoring.DETAILED,
        });

        // Create an ECS cluster
        const cluster = new ecs.Cluster(this, 'Cluster', {
            vpc: existingVpc,
            clusterName: envName,
        });

        asg.addUserData(`
echo ECS_CLUSTER=${cluster.clusterName} >> /etc/ecs/ecs.config
yum install -y aws-cfn-bootstrap
/opt/aws/bin/cfn-signal -e $? --stack ${this.stackName} --resource ${asg} --region ${this.region}
`
        )

这会生成以下用户数据

      UserData:
        Fn::Base64:
          Fn::Join:
            - ""
            - - |-
                #!/bin/bash

                echo ECS_CLUSTER=
              - Ref: ClusterEB0386A7
              - |2
                 >> /etc/ecs/ecs.config
                yum install -y aws-cfn-bootstrap
                /opt/aws/bin/cfn-signal -e $? --stack CdkClusterStack --resource CdkClusterStack/ASG --region eu-west-1

这让我很困惑。 cluster.clusterName 根本不返回集群名称,而是返回底层集群资源的 Ref。

对于 ASG,生成了以下内容,我想这里而不是我目前得到的 CdkClusterStack/ASG 我需要 ASG46ED3070

 ASG46ED3070:
    Type: AWS::AutoScaling::AutoScalingGroup
    Properties:
      MaxSize: "10"
      MinSize: "3"
      DesiredCapacity: "3"
      .... more stuff ....

请指教!

【问题讨论】:

    标签: amazon-web-services aws-cdk


    【解决方案1】:

    ECS 集群上逻辑 ID 的 Ref 返回资源名称,在本例中为集群名称。When you pass the logical ID of this resource to the intrinsic Ref function, Ref returns the resource name. 这是来自Cloudformation 页面。

    要检查这一点,我会将 cluster.clusterName 添加为 cfnOutput 并检查它是否为您提供了实际的集群名称。

    【讨论】:

      猜你喜欢
      • 2021-01-30
      • 1970-01-01
      • 1970-01-01
      • 2019-07-08
      • 2023-03-06
      • 1970-01-01
      • 2015-01-30
      • 2015-05-21
      • 1970-01-01
      相关资源
      最近更新 更多