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

ec2: userData in launchTemplate is not created automatically even when machineImege is provided #23592

Closed
tmokmss opened this issue Jan 6, 2023 · 2 comments · Fixed by #23593
Assignees
Labels
@aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud bug This issue is a bug. effort/small Small work item – less than a day of effort p2

Comments

@tmokmss
Copy link
Contributor

tmokmss commented Jan 6, 2023

Describe the bug

Reading this doc, userData is expected to be automatically created when machineImage is provided.

(optional, default: This Launch Template creates a UserData based on the type of provided machineImage; no UserData is created if a machineImage is not provided)

However, it is not working as expected; userData is not automatically created at all. See reproduction steps for the actual code.

Expected Behavior

userData is automatically created when machineImage is provided.

Current Behavior

userData is not created even when machineImage is provided.

Reproduction Steps

Synthesize the below stack:

export class Ec2UserdataStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const launchTemplate = new ec2.LaunchTemplate(this, 'LaunchTemplate', {
      instanceType: new ec2.InstanceType('t3.small'),
      machineImage: new ec2.AmazonLinuxImage({
        generation: ec2.AmazonLinuxGeneration.AMAZON_LINUX_2,
      }),
    });

    // this will cause an error
    launchTemplate.userData!.addCommands("yum -y update")
  }
}

and we get the following error:

/../ec2-userdata/lib/ec2-userdata-stack.ts:26
    launchTemplate.userData!.addCommands("yum -y update")
                             ^
TypeError: Cannot read properties of undefined (reading 'addCommands')

Possible Solution

Current implementation is here. We need to add code for creating userData depending on machineImage.

I guess we can follow the below implementation in ec2.Instance:

// use delayed evaluation
const imageConfig = props.machineImage.getImage(this);
this.userData = props.userData ?? imageConfig.userData;

Additional Information/Context

Adding this feature will not be a breaking change because we are just adding empty userData to launchTemplates whose userData was not specified explicitly, and it will not have any effect on the existing behavior.

CDK CLI Version

2.59.0

Framework Version

2.59.0

Node.js Version

v16.18.1

OS

macOS

Language

Typescript

Language Version

No response

Other information

No response

@tmokmss tmokmss added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jan 6, 2023
@github-actions github-actions bot added the @aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud label Jan 6, 2023
@peterwoodworth peterwoodworth added p2 effort/small Small work item – less than a day of effort and removed needs-triage This issue or PR still needs to be triaged. labels Jan 7, 2023
@peterwoodworth
Copy link
Contributor

Thanks for the quick PR on this!

@mergify mergify bot closed this as completed in #23593 Feb 27, 2023
mergify bot pushed a commit that referenced this issue Feb 27, 2023
…chineImege is provided (#23593)

closes #23592

Reading through the discussion in PR #12385, which introduced the original code, I could not find any reason not to create userData when machineImage is provided. They also agreed with the design [here](#12385 (comment)), but it seems it accidentally became out of scope at the time.

This change should not be considered as a breaking change because we are just adding empty userData to launchTemplates whose userData is not specified explicitly, and it will not have any effect on the existing behavior.

* Users who already sets a userData explicitly:
   * will not see any change to their synthesized template, as this PR only modifies userData when it is not set explicitly.
* Users who is not using userData:
   * will see a userData is added to their template. But the userData is empty and does nothing. It should not have any effect on the previous behavior.

----

### All Submissions:

* [X] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md)

### Adding new Construct Runtime Dependencies:

* [ ] This PR adds new construct runtime dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-construct-runtime-dependencies)

### New Features

* [X] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)?
	* [X] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)?

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@github-actions
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

westhouseK pushed a commit to westhouseK/aws-cdk that referenced this issue Feb 28, 2023
…chineImege is provided (aws#23593)

closes aws#23592

Reading through the discussion in PR aws#12385, which introduced the original code, I could not find any reason not to create userData when machineImage is provided. They also agreed with the design [here](aws#12385 (comment)), but it seems it accidentally became out of scope at the time.

This change should not be considered as a breaking change because we are just adding empty userData to launchTemplates whose userData is not specified explicitly, and it will not have any effect on the existing behavior.

* Users who already sets a userData explicitly:
   * will not see any change to their synthesized template, as this PR only modifies userData when it is not set explicitly.
* Users who is not using userData:
   * will see a userData is added to their template. But the userData is empty and does nothing. It should not have any effect on the previous behavior.

----

### All Submissions:

* [X] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md)

### Adding new Construct Runtime Dependencies:

* [ ] This PR adds new construct runtime dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-construct-runtime-dependencies)

### New Features

* [X] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)?
	* [X] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)?

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
beck3905 pushed a commit to beck3905/aws-cdk that referenced this issue Feb 28, 2023
…chineImege is provided (aws#23593)

closes aws#23592

Reading through the discussion in PR aws#12385, which introduced the original code, I could not find any reason not to create userData when machineImage is provided. They also agreed with the design [here](aws#12385 (comment)), but it seems it accidentally became out of scope at the time.

This change should not be considered as a breaking change because we are just adding empty userData to launchTemplates whose userData is not specified explicitly, and it will not have any effect on the existing behavior.

* Users who already sets a userData explicitly:
   * will not see any change to their synthesized template, as this PR only modifies userData when it is not set explicitly.
* Users who is not using userData:
   * will see a userData is added to their template. But the userData is empty and does nothing. It should not have any effect on the previous behavior.

----

### All Submissions:

* [X] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md)

### Adding new Construct Runtime Dependencies:

* [ ] This PR adds new construct runtime dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-construct-runtime-dependencies)

### New Features

* [X] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)?
	* [X] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)?

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
homakk pushed a commit to homakk/aws-cdk that referenced this issue Mar 28, 2023
…chineImege is provided (aws#23593)

closes aws#23592

Reading through the discussion in PR aws#12385, which introduced the original code, I could not find any reason not to create userData when machineImage is provided. They also agreed with the design [here](aws#12385 (comment)), but it seems it accidentally became out of scope at the time.

This change should not be considered as a breaking change because we are just adding empty userData to launchTemplates whose userData is not specified explicitly, and it will not have any effect on the existing behavior.

* Users who already sets a userData explicitly:
   * will not see any change to their synthesized template, as this PR only modifies userData when it is not set explicitly.
* Users who is not using userData:
   * will see a userData is added to their template. But the userData is empty and does nothing. It should not have any effect on the previous behavior.

----

### All Submissions:

* [X] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md)

### Adding new Construct Runtime Dependencies:

* [ ] This PR adds new construct runtime dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-construct-runtime-dependencies)

### New Features

* [X] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)?
	* [X] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)?

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud bug This issue is a bug. effort/small Small work item – less than a day of effort p2
Projects
None yet
3 participants