Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aws-autoscaling: LaunchTemplate resource should be created for AutoScalingGroup instead of LaunchConfiguration when running unit tests #28750

Open
carlosahs opened this issue Jan 18, 2024 · 3 comments
Labels
@aws-cdk/aws-autoscaling Related to Amazon EC2 Auto Scaling bug This issue is a bug. effort/medium Medium work item – several days of effort p2

Comments

@carlosahs
Copy link

carlosahs commented Jan 18, 2024

Describe the bug

Hi, template generated from stack in unit tests creates a LaunchConfiguration resource instead of the recommended LaunchTemplate resource when an AutoScalingGroup resource is created. So when a test like this is added:

test('Auto Scaling Group Created', () => {
    const app = new App();
    const stack = new DummyStack(app, 'DummyStack');
    const template = Template.fromStack(stack);
    console.log(template);
    template.hasResourceProperties('AWS::AutoScaling::AutoScalingGroup', {
        AutoScalingGroupName: 'MyAutoScalingGroup',
        LaunchTemplate: Match.anyValue(),
        MaxSize: '1',
        MinSize: '0'
    });
});

It will fail with:

 FAIL  test/dummy.test.ts
  ✕ Auto Scaling Group Created (213 ms)

  ● Auto Scaling Group Created

    Template has 1 resources with type AWS::AutoScaling::AutoScalingGroup, but none match as expected.
    The 1 closest matches:
    MyAutoScalingGroupASG1686ADA7 :: {
      "Properties": {
        "LaunchConfigurationName": { "Ref": "MyAutoScalingGroupLaunchConfig15D8856B" },
    !!   Missing key 'LaunchTemplate'
        "LaunchTemplate": undefined,
        "MaxSize": "1",
        "MinSize": "0",
        "Tags": [ { ... } ],
        "VPCZoneIdentifier": [ ... ]
      },
      "Type": "AWS::AutoScaling::AutoScalingGroup",
      "UpdatePolicy": { "AutoScalingScheduledAction": { "IgnoreUnmodifiedGroupSizeProperties": true } }
    }

Please note that this only happens for unit tests, when cdk synth is executed AutoScalingGroup is correctly created with a LaunchTemplate:

"MyAutoScalingGroupASG1686ADA7": {
 "Type": "AWS::AutoScaling::AutoScalingGroup",
 "Properties": {
  "AutoScalingGroupName": "MyAutoScalingGroup",
  "LaunchTemplate": {
   "LaunchTemplateId": {
    "Ref": "MyAutoScalingGroupLaunchTemplate9D9A040D"
   },
   "Version": {
    "Fn::GetAtt": [
     "MyAutoScalingGroupLaunchTemplate9D9A040D",
     "LatestVersionNumber"
    ]
   }
  },
  ...
}

Expected Behavior

LaunchTemplate should be created instead of LaunchConfiguration for AutoScalingGroup when template is created during unit test execution. This as per recommendation of Template reference docs.

Current Behavior

LaunchConfiguration is created for AutoScalingGroup instead of LaunchTemplate when template is created during unit test execution. This is against recommendation of Template reference docs.

Reproduction Steps

  1. Create a new CDK app with cdk init app --language typescript using latest CDK CLI (version 2.121.1 (build d86bb1a)).
  2. Update stack in lib/ with this code:
export class DummyStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);
    const vpc = new ec2.Vpc(this, 'CoreVpc', {
      availabilityZones: ['us-east-1a', 'us-east-1b'],
      enableDnsHostnames: false,
      enableDnsSupport: false,
      ipAddresses: ec2.IpAddresses.cidr('10.0.0.0/16'),
      subnetConfiguration: [
          {
              cidrMask: 20,
              name: 'private',
              subnetType: ec2.SubnetType.PRIVATE_ISOLATED
          }
      ],
      vpcName: 'MyVpc'
  });
    const autoScalingGroup = new autoscaling.AutoScalingGroup(this, 'MyAutoScalingGroup', {
      autoScalingGroupName: 'MyAutoScalingGroup',
      instanceType: new ec2.InstanceType('t2.micro'),
      machineImage: ecs.EcsOptimizedImage.amazonLinux2(),
      maxCapacity: 1,
      minCapacity: 0,
      vpc: vpc,
    });
  }
}
  1. Update test file in test/ with this code:
test('Auto Scaling Group Created', () => {
  const app = new App();
  const stack = new DummyStack(app, 'DummyStack');
  const template = Template.fromStack(stack);
  console.log(template);
  template.hasResourceProperties('AWS::AutoScaling::AutoScalingGroup', {
    LaunchTemplate: Match.anyValue(),
    MaxSize: '1',
    MinSize: '0'
  });
});
  1. Run npm test.

Possible Solution

Possibly solution from #23165 could be extended.

Additional Information/Context

Contents from package.json:

{
  "name": "dummy",
  "version": "0.1.0",
  "bin": {
    "dummy": "bin/dummy.js"
  },
  "scripts": {
    "build": "tsc",
    "watch": "tsc -w",
    "test": "jest",
    "cdk": "cdk"
  },
  "devDependencies": {
    "@types/jest": "^29.5.11",
    "@types/node": "20.10.8",
    "jest": "^29.7.0",
    "ts-jest": "^29.1.1",
    "aws-cdk": "2.121.1",
    "ts-node": "^10.9.2",
    "typescript": "~5.3.3"
  },
  "dependencies": {
    "aws-cdk-lib": "2.121.1",
    "constructs": "^10.0.0",
    "source-map-support": "^0.5.21"
  }
}

CDK CLI Version

2.121.1 (build d86bb1a)

Framework Version

2.121.1

Node.js Version

v18.18.0

OS

macOS Sonoma 14.2.1

Language

TypeScript

Language Version

Version 5.3.3

Other information

@carlosahs carlosahs added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jan 18, 2024
@github-actions github-actions bot added the @aws-cdk/aws-autoscaling Related to Amazon EC2 Auto Scaling label Jan 18, 2024
@pahud
Copy link
Contributor

pahud commented Jan 19, 2024

I guess it's to avoid breaking changes. The LT will not be created by default only when prop.launchTemplate is defauled

if (props.launchTemplate || props.mixedInstancesPolicy) {

or AUTOSCALING_GENERATE_LAUNCH_TEMPLATE is enabled

https://github.com/aws/aws-cdk/blob/20ad55e7aec7d387550db865257dc6f8ebcab067/packages/aws-cdk-lib/aws-autoscaling/lib/auto-scaling-group.ts#L1365C43-L1365C79

@pahud pahud added p2 response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. effort/medium Medium work item – several days of effort and removed needs-triage This issue or PR still needs to be triaged. labels Jan 19, 2024
Copy link

This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

@github-actions github-actions bot added the closing-soon This issue will automatically close in 4 days unless further comments are made. label Jan 21, 2024
@carlosahs
Copy link
Author

carlosahs commented Jan 22, 2024

But how is it that with cdk synth LT is created and not when unit tests are executed with npm test? If I use launchTemplate prop I need to make sure I specify ImageId with an ECS optimized AMI and make sure UserData contains this script (which is automatically generated when I run cdk synth without needing to specify launchTemplate prop with reproduction steps code above):

#!/bin/bash
echo ECS_CLUSTER=<ecs-cluster-logical-id> >> /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

Concern here is that what cdk synth generates is not consistent with what is generated when npm test is run, which makes unit tests less reliable.

@github-actions github-actions bot removed closing-soon This issue will automatically close in 4 days unless further comments are made. response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. labels Jan 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-autoscaling Related to Amazon EC2 Auto Scaling bug This issue is a bug. effort/medium Medium work item – several days of effort p2
Projects
None yet
Development

No branches or pull requests

2 participants