feat(ec2): expose EC2 instance MetadataOptions#35369
Conversation
✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.
… 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
|
@pahud if this is no longer a draft, can you please update it. I have approved the current version |
|
@alvazjor OK I will do the final check really quick today and convert it to ready. |
|
fixing CI |
|
@pahud there seems to be an issue with this PR, not building. Can you check and rebase please? |
|
@alvazjor Yes I will get it fixed hopefully tonight and get back to you. Thank you! |
- Changed from nested metadataOptions object to flat properties - Added httpEndpoint, httpProtocolIpv6, httpPutResponseHopLimit, httpTokens, instanceMetadataTags directly to InstanceProps - Removed InstanceMetadataOptions interface (was only in PR, never released) - Updated all documentation with accurate CloudFormation defaults - Updated README examples to use flat API - Updated integration tests to use flat API - All 22 metadata options unit tests passing This matches the LaunchTemplate API design for consistency across the EC2 module.
- Remove extra blank lines between IInstance interface and JSDoc comment - Remove trailing blank line at end of instance.test.ts file - Improve code formatting consistency across EC2 instance module
- Remove `@deprecated` annotation from LaunchTemplateHttpTokens export - Simplify documentation by removing redundant deprecation notice - Users should reference HttpTokens directly for metadata token configuration - Maintains backward compatibility while encouraging use of primary export
- Define HttpTokens enum in instance.ts with OPTIONAL and REQUIRED values - Provides cleaner API for Instance construct - LaunchTemplate continues to use LaunchTemplateHttpTokens (no breaking change) - Both enums have identical members for consistency
|
Hi @alvazjor I've refactored the implementation(see PR desc). Please take a look when you get a chance. Thank you. |
|
Thanks @pahud |
|
Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork). |
|
Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork). |
|
Comments on closed issues and PRs are hard for our team to see. |
Issue # (if applicable)
Closes #35357.
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 without resorting to escape hatches.
Description of changes
Added metadata options properties to Instance construct with flat properties matching LaunchTemplate API design for consistency across the EC2 module.
Key changes:
Instance construct enhancements - Added individual metadata option properties directly to InstanceProps:
httpEndpoint?: boolean- Enables or disables the HTTP metadata endpointhttpProtocolIpv6?: boolean- Enables or disables the IPv6 endpoint for IMDShttpPutResponseHopLimit?: number- Sets the hop limit for metadata requests (1-64)httpTokens?: HttpTokens- Controls IMDSv2 requirement (OPTIONAL or REQUIRED)instanceMetadataTags?: boolean- Enables or disables instance tag access from metadata APISeparate enum for cleaner API - Defined
HttpTokensenum in instance.ts:HttpTokensenum (cleaner API for new construct)LaunchTemplateHttpTokensenum (backward compatible, no JSII breaking change)Validation and rendering - Added validation to prevent conflicting use of metadata options with
requireImdsv2and implemented smart rendering that only includes MetadataOptions when explicitly specifiedTesting - Added 22 comprehensive unit tests covering all metadata options scenarios
Before (using escape hatch):
After (idiomatic L2 API):
CloudFormation impact:
Accurate CloudFormation defaults
All properties include evidence-based
@defaulttags based on official AWS CloudFormation documentation:httpEndpoint: @default true - CloudFormation default is "enabled"httpProtocolIpv6: @default false - CloudFormation default is "disabled"httpPutResponseHopLimit: @default - No default specified in CloudFormationhttpTokens: @default - Conditional default based on AMI and account settingsinstanceMetadataTags: @default false - CloudFormation default is "disabled"Separate enum approach
Instance defines its own
HttpTokensenum with identical members:This provides a cleaner API for Instance users while maintaining full backward compatibility:
HttpTokensenum (cleaner for users)LaunchTemplateHttpTokensenum (no JSII breaking change)Description of how you validated changes
Checklist
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license