-
Notifications
You must be signed in to change notification settings - Fork 4.2k
feat(ec2): expose EC2 instance MetadataOptions #35369
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(This review is outdated)
✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.
metadataOptions.httpTokens === undefined && | ||
metadataOptions.instanceMetadataTags === undefined | ||
)) { | ||
return undefined; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we want to do this, then we need to remove the documentation that states the default values for the properties in InstanceMetadataOptions
, as we are not respecting that here. In my opinion, if you define the metadataOptions
, then the properties that have a default value should use that (even if they are undefined by the user)
* | ||
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-instance-metadataoptions.html#cfn-ec2-instance-metadataoptions-httpputresponsehoplimit | ||
* | ||
* @default 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we defaulting to 1
? CFN does not provides any default, so I wonder where this value is coming from
… in tests - Updated import statement to use HttpTokens instead of LaunchTemplateHttpTokens - Replaced all usage references in test cases - Aligns with deprecation notice in favor of HttpTokens enum - Fixed trailing spaces in instance.ts
Reason for this change
The EC2 Instance construct lacked support for metadata options configuration, while the LaunchTemplate construct already had this capability. Users needed a way to configure instance metadata options (like IMDSv2 requirements, hop limits, etc.) directly on Instance constructs.
Additionally, we wanted to provide a better developer experience than the existing LaunchTemplate implementation, where users must specify at least one property to opt into metadata options.
Description of changes
1. Added new
metadataOptions
property to Instance constructInstanceMetadataOptions
interface with all metadata configuration options2. Improved developer experience with clean opt-in
metadataOptions: undefined
(omitted) → Uses legacy EC2 behaviormetadataOptions: {}
→ Clean way to opt into CloudFormation defaults for all propertiesmetadataOptions: { httpEndpoint: false }
→ Uses CloudFormation defaults for unspecified properties3. Added comprehensive
@default
documentation tagsAdded evidence-based
@default
tags to all properties in theInstanceMetadataOptions
interface based on official AWS CloudFormation documentation:Properties updated:
httpEndpoint
:@default true
- CloudFormation default is "enabled"httpProtocolIpv6
:@default false
- CloudFormation default is "disabled"httpPutResponseHopLimit
:@default
- No default specified in CloudFormation docshttpTokens
:@default
- Complex default based on AMI and account settingsinstanceMetadataTags
:@default false
- CloudFormation default is "disabled"Key improvements:
Better developer experience: Users can write
metadataOptions: {}
to cleanly opt into modern metadata options with CloudFormation defaults.No breaking changes: This is a new property on Instance construct, so no existing code is affected.
Consistent with LaunchTemplate: Uses the same property names and types for familiarity.
Conflict prevention: Validates that
metadataOptions
andrequireImdsv2
are not used together to prevent configuration conflicts:requireImdsv2: true
creates a LaunchTemplate withhttpTokens: 'required'
metadataOptions
sets metadata options directly on the InstanceClear documentation: All
@default
tags clarify "(only applies when metadataOptions is specified)" and are backed by CloudFormation documentation.Describe any new or updated permissions being added
N/A - No IAM permissions changes. This adds a new optional property that controls CloudFormation template generation but doesn't require additional permissions.
Description of how you validated changes
Documentation verification: Cross-referenced all default values against official AWS CloudFormation documentation:
Code analysis: Implemented clean opt-in logic in
renderMetadataOptions()
:metadataOptions: {}
cleanly opts into CloudFormation defaultsBreaking change analysis: Confirmed this is safe because
metadataOptions
is a new property on Instance construct - no existing code could be affected.Conflict validation: Added validation in
renderMetadataOptions()
to prevent usingrequireImdsv2
andmetadataOptions
together:Updated Behavior:
requireImdsv2: undefined, metadataOptions: undefined
→ Legacy EC2 behavior ✅requireImdsv2: true, metadataOptions: undefined
→ Uses LaunchTemplate with IMDSv2 required ✅requireImdsv2: undefined, metadataOptions: {}
→ Uses CloudFormation defaults ✅requireImdsv2: true, metadataOptions: {}
→ Throws validation error ✅Why this approach is best:
requireImdsv2
for simple cases,metadataOptions
for advanced casesTypeScript compilation: Verified all changes compile without errors and maintain existing type safety.
Validation testing: Confirmed the validation error is thrown when both
requireImdsv2: true
andmetadataOptions
are specified together.Checklist
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license.