Skip to content

EC2: LaunchTemplate does not create a UserData when machineImage is provided used with AsgCapacityProvider #20870

@tochny

Description

@tochny

Describe the bug

As documented, Launch Template should creates a UserData based on the type of provided machineImage, but no UserData generated when machineImage is provided.
When uses with AsgCapacityProvider will throw an error The provided launch template does not expose its user data

Expected Behavior

Should create UserData based on machineImage

Current Behavior

No UserData generated.

Reproduction Steps

If provided machineImage only:

const asgLaunchTemplate = new ec2.LaunchTemplate(this, 'ASG-LaunchTemplate', {
    instanceType: new ec2.InstanceType('t3.medium'),
    machineImage: ecs.EcsOptimizedImage.amazonLinux2(),
});

Which will produce

LaunchTemplate:
    Type: AWS::EC2::LaunchTemplate
    Properties:
      LaunchTemplateData:
        ImageId:
          Ref: SsmParameterValueawsserviceecsoptimizedamiamazonlinux2recommendedimageidC96584B6F00A464EAD1953AFF4B05118Parameter
        InstanceType: t3.medium
        TagSpecifications:
          - ResourceType: instance
            Tags:
              - Key: Name
                Value: Test-Dev/LaunchTemplate
          - ResourceType: volume
            Tags:
              - Key: Name
                Value: Test-Dev/LaunchTemplate
    Metadata:
      aws:cdk:path: Test-Dev/LaunchTemplate/Resource

And if UserData provided:

const asgLaunchTemplate = new ec2.LaunchTemplate(this, 'ASG-LaunchTemplate', {
    instanceType: new ec2.InstanceType('t3.medium'),
    machineImage: ecs.EcsOptimizedImage.amazonLinux2(),
    userData: ec2.UserData.forLinux(),
});

const autoScalingGroup = new autoscaling.AutoScalingGroup(this, 'ASG', {
    vpc,
    mixedInstancesPolicy: {
    instancesDistribution: {
      onDemandPercentageAboveBaseCapacity: 50,
    },
    launchTemplate: asgLaunchTemplate,
  },
});

const capacityProvider = new ecs.AsgCapacityProvider(this, 'AsgCapacityProvider', {autoScalingGroup});
cluster.addAsgCapacityProvider(capacityProvider);

Which will add the UserData

ASGLaunchTemplate:
    Type: AWS::EC2::LaunchTemplate
    Properties:
      LaunchTemplateData:
        IamInstanceProfile:
          Arn:
            Fn::GetAtt:
              - ASGLaunchTemplateProfile
              - Arn
        ImageId:
          Ref: SsmParameterValueawsserviceecsoptimizedamiamazonlinux2recommendedimageidC96584B6F00A464EAD1953AFF4B05118Parameter
        InstanceType: t3.medium
        TagSpecifications:
          - ResourceType: instance
            Tags:
              - Key: Name
                Value: Test-Dev/ASG-LaunchTemplate
          - ResourceType: volume
            Tags:
              - Key: Name
                Value: Test-Dev/ASG-LaunchTemplate
        UserData:
          Fn::Base64:
            Fn::Join:
              - ""
              - - |-
                  #!/bin/bash
                  echo ECS_CLUSTER=
                - Ref: Cluster
                - |-2
                   >> /etc/ecs/ecs.config
                  sudo iptables --insert FORWARD 1 --in-interface docker+ --destination 169.254.169.254/32 --jump DROP
                  sudo service iptables save
                  echo ECS_AWSVPC_BLOCK_IMDS=true >> /etc/ecs/ecs.config
    Metadata:
      aws:cdk:path: Test-Dev/ASG-LaunchTemplate/Resource

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.29.1

Framework Version

No response

Node.js Version

16

OS

Amazon Linux 2 aarch64

Language

Typescript

Language Version

No response

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    @aws-cdk/aws-ec2Related to Amazon Elastic Compute CloudbugThis issue is a bug.effort/smallSmall work item – less than a day of effortfeature-requestA feature should be added or improved.p2

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions