0
votes

I'm quite new to using CDK, and I'm trying to generate user_data for my launch config. Having problems however... Took the userdata from here and trying to adapt that to my CDK template: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/quickref-ecs.html#quickref-ecs-example-1.yaml

This is my code

     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}
`
        )

This generates the following user data

      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

This is very confusing to me. The cluster.clusterName does not return the cluster name at all, but a Ref to the underlying cluster resource.

For the ASG the following is generated, and I guess here instead of CdkClusterStack/ASG which I'm currently getting I would need ASG46ED3070

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

Please advise!

1

1 Answers

0
votes

The Ref of the logical ID on ECS cluster returns the resource name which in this case is the cluster name.When you pass the logical ID of this resource to the intrinsic Ref function, Ref returns the resource name. This is from the Cloudformation pages.

To check this, I would add the cluster.clusterName as a cfnOutput and check if it is giving you the actual cluster name infact.