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: require_imdsv2 causes TypeError: Cannot read properties of undefined (reading 'metadataOptions') #27070

Closed
ghost opened this issue Sep 8, 2023 · 2 comments
Labels
@aws-cdk/aws-autoscaling Related to Amazon EC2 Auto Scaling bug This issue is a bug. closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. p1 response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days.

Comments

@ghost
Copy link

ghost commented Sep 8, 2023

Describe the bug

When applying require_imdsv2=True to autoscaling.AutoScalingGroup(), cdk synth fails. cdk synth succeeds when this is require_imdsv2=False.

Expected Behavior

cdk synth succeeds and enables imdsv2 on the launch template.

Current Behavior

[12:13:47] env: {
  CDK_DEFAULT_REGION: 'us-east-1',
  CDK_DEFAULT_ACCOUNT: '323739469746',
  CDK_DEBUG: 'true',
  CDK_OUTDIR: 'cdk.out',
  CDK_CLI_ASM_VERSION: '34.0.0',
  CDK_CLI_VERSION: '2.95.0'
}
[12:13:47] Notices refreshed
jsii.errors.JavaScriptError: 
  @jsii/kernel.RuntimeError: TypeError: Cannot read properties of undefined (reading 'metadataOptions')
      at Kernel._Kernel_ensureSync (/private/var/folders/f0/c9zqjb9s55n2gr3248m7f1pw0000gq/T/tmp8i9qvyr6/lib/program.js:10454:23)
      at Kernel.invoke (/private/var/folders/f0/c9zqjb9s55n2gr3248m7f1pw0000gq/T/tmp8i9qvyr6/lib/program.js:9818:102)
      at KernelHost.processRequest (/private/var/folders/f0/c9zqjb9s55n2gr3248m7f1pw0000gq/T/tmp8i9qvyr6/lib/program.js:11658:36)
      at KernelHost.run (/private/var/folders/f0/c9zqjb9s55n2gr3248m7f1pw0000gq/T/tmp8i9qvyr6/lib/program.js:11618:22)
      at Immediate._onImmediate (/private/var/folders/f0/c9zqjb9s55n2gr3248m7f1pw0000gq/T/tmp8i9qvyr6/lib/program.js:11619:46)
      at process.processImmediate (node:internal/timers:476:21)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/j8g8/p/ecs/cdk/cdk-ecs_ecs/app.py", line 27, in <module>
    app.synth()
  File "/Users/j8g8/p/ecs/cdk/cdk-ecs_ecs/.venv/lib/python3.10/site-packages/aws_cdk/__init__.py", line 21201, in synth
    return typing.cast(_CloudAssembly_c693643e, jsii.invoke(self, "synth", [options]))
  File "/Users/j8g8/p/ecs/cdk/cdk-ecs_ecs/.venv/lib/python3.10/site-packages/jsii/_kernel/__init__.py", line 149, in wrapped
    return _recursize_dereference(kernel, fn(kernel, *args, **kwargs))
  File "/Users/j8g8/p/ecs/cdk/cdk-ecs_ecs/.venv/lib/python3.10/site-packages/jsii/_kernel/__init__.py", line 399, in invoke
    response = self.provider.invoke(
  File "/Users/j8g8/p/ecs/cdk/cdk-ecs_ecs/.venv/lib/python3.10/site-packages/jsii/_kernel/providers/process.py", line 380, in invoke
    return self._process.send(request, InvokeResponse)
  File "/Users/j8g8/p/ecs/cdk/cdk-ecs_ecs/.venv/lib/python3.10/site-packages/jsii/_kernel/providers/process.py", line 342, in send
    raise RuntimeError(resp.error) from JavaScriptError(resp.stack)
RuntimeError: TypeError: Cannot read properties of undefined (reading 'metadataOptions')
[12:13:54] Reading cached notices from /Users/j8g8/.cdk/cache/notices.json

Subprocess exited with error 1
[12:13:57] Error: Subprocess exited with error 1
    at ChildProcess.<anonymous> (/usr/local/lib/node_modules/aws-cdk/lib/api/cxapp/exec.ts:121:23)
    at ChildProcess.emit (node:events:514:28)
    at ChildProcess.emit (node:domain:489:12)
    at Process.ChildProcess._handle.onexit (node:internal/child_process:291:12)

Reproduction Steps

class CdkEcsStack(Stack):
    def __init__(
        self, scope: Construct, construct_id: str, config: Dict, **kwargs
    ) -> None:
        super().__init__(scope, construct_id, **kwargs)
        my_vpc = ec2.Vpc.from_lookup(self, f"VPC-myvpc", vpc_name='my-vpc')
        auto_scaling_group = autoscaling.AutoScalingGroup(self, "ASG",
            vpc=my_vpc,
            instance_type=ec2.InstanceType.of(ec2.InstanceClass.T4G, ec2.InstanceSize.MICRO),
            machine_image=ecs.EcsOptimizedImage.amazon_linux2(ecs.AmiHardwareType.ARM),
            min_capacity=1,
            max_capacity=2,
            auto_scaling_group_name=f"asg",
            vpc_subnets=ec2.SubnetSelection(one_per_az=True, subnet_type=ec2.SubnetType.PRIVATE_WITH_EGRESS),
            require_imdsv2=True,
        )

Possible Solution

        # workaround
        # require_imdsv2 https://github.com/aws/aws-cdk/issues/27070
        lt = auto_scaling_group.node.find_child("LaunchTemplate")
        clt = lt.node.default_child
        metadata_prop = ec2.CfnLaunchTemplate.MetadataOptionsProperty(http_tokens='required')
        lt_data = clt.launch_template_data
        lt_data_imdsv2 = ec2.CfnLaunchTemplate.LaunchTemplateDataProperty(**lt_data.__dict__["_values"], metadata_options=metadata_prop)
        clt.launch_template_data = lt_data_imdsv2

Additional Information/Context

No response

CDK CLI Version

2.95.0 (build cfa7e88)

Framework Version

No response

Node.js Version

v18.17.0

OS

macOS 11.7.9

Language

Python

Language Version

No response

Other information

No response

@ghost ghost added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Sep 8, 2023
@github-actions github-actions bot added the @aws-cdk/aws-autoscaling Related to Amazon EC2 Auto Scaling label Sep 8, 2023
@indrora
Copy link
Contributor

indrora commented Sep 11, 2023

Does this work when you include a launch template in the Autoscaling group configuration? https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_autoscaling.AutoScalingGroup.html#launchtemplate

If so, then there should be a check to make sure that there is a Launch Template with the appropriate fields set if that is the case.

@indrora indrora added p1 response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. and removed needs-triage This issue or PR still needs to be triaged. labels Sep 11, 2023
@github-actions
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 closing-soon This issue will automatically close in 4 days unless further comments are made. closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. and removed closing-soon This issue will automatically close in 4 days unless further comments are made. labels Sep 13, 2023
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. closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. p1 response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days.
Projects
None yet
Development

No branches or pull requests

1 participant