Skip to content

feat(ec2): expose EC2 instance MetadataOptions#35369

Merged
mergify[bot] merged 19 commits intoaws:mainfrom
pahud:feature/ec2-instance-metadata-options-35357
Nov 28, 2025
Merged

feat(ec2): expose EC2 instance MetadataOptions#35369
mergify[bot] merged 19 commits intoaws:mainfrom
pahud:feature/ec2-instance-metadata-options-35357

Conversation

@pahud
Copy link
Copy Markdown
Contributor

@pahud pahud commented Aug 29, 2025

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:

  1. Instance construct enhancements - Added individual metadata option properties directly to InstanceProps:

    • httpEndpoint?: boolean - Enables or disables the HTTP metadata endpoint
    • httpProtocolIpv6?: boolean - Enables or disables the IPv6 endpoint for IMDS
    • httpPutResponseHopLimit?: 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 API
  2. Separate enum for cleaner API - Defined HttpTokens enum in instance.ts:

    • Instance uses HttpTokens enum (cleaner API for new construct)
    • LaunchTemplate keeps LaunchTemplateHttpTokens enum (backward compatible, no JSII breaking change)
    • Both enums have identical members (OPTIONAL, REQUIRED) for consistency
  3. Validation and rendering - Added validation to prevent conflicting use of metadata options with requireImdsv2 and implemented smart rendering that only includes MetadataOptions when explicitly specified

  4. Testing - Added 22 comprehensive unit tests covering all metadata options scenarios

Before (using escape hatch):

const instance = new ec2.Instance(this, 'Instance', {
  vpc,
  instanceType,
  machineImage,
});

const cfnInstance = instance.node.defaultChild as ec2.CfnInstance;
cfnInstance.addPropertyOverride('MetadataOptions', {
  HttpTokens: 'required',
  HttpPutResponseHopLimit: 2,
});

After (idiomatic L2 API):

const instance = new ec2.Instance(this, 'Instance', {
  vpc,
  instanceType,
  machineImage,
  httpTokens: HttpTokens.REQUIRED,
  httpPutResponseHopLimit: 2,
  instanceMetadataTags: true,
});

CloudFormation impact:

  • When metadata options are specified, generates MetadataOptions object with only the specified properties
  • When not specified, CloudFormation template remains unchanged (backward compatible)
  • No breaking changes to existing behavior

Accurate CloudFormation defaults

All properties include evidence-based @default tags 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 CloudFormation
  • httpTokens: @default - Conditional default based on AMI and account settings
  • instanceMetadataTags: @default false - CloudFormation default is "disabled"

Separate enum approach

Instance defines its own HttpTokens enum with identical members:

export enum HttpTokens {
  OPTIONAL = 'optional',
  REQUIRED = 'required',
}

This provides a cleaner API for Instance users while maintaining full backward compatibility:

  • Instance API uses HttpTokens enum (cleaner for users)
  • LaunchTemplate API uses LaunchTemplateHttpTokens enum (no JSII breaking change)
  • Both enums have identical members for consistency
  • Follows conventional CDK patterns (similar to ELBv2 shared enums)

Description of how you validated changes

  • Unit tests: Added 22 comprehensive tests covering:
    • All metadata options with various values
    • Boolean to enabled/disabled string mapping
    • Hop limit validation (1-64 range)
    • Partial configuration (only specified properties rendered)
    • Conflict detection with requireImdsv2
  • Build verification: TypeScript compilation, linting, and JSII build all pass successfully
  • Integration tests: Updated integ.instance-metadata-options.ts to use the new flat API
  • CloudFormation validation: Unit tests verify correct template generation with proper property structure
  • API consistency: Verified the API matches LaunchTemplate's flat property design
  • Backward compatibility: Verified LaunchTemplateHttpTokens alias works with existing code

Checklist


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@aws-cdk-automation aws-cdk-automation requested a review from a team August 29, 2025 20:30
@github-actions github-actions bot added effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2 labels Aug 29, 2025
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Aug 29, 2025
Copy link
Copy Markdown
Collaborator

@aws-cdk-automation aws-cdk-automation left a 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)

@pahud pahud changed the title feat(aws-ec2): expose EC2 instance MetadataOptions feat(ec2): expose EC2 instance MetadataOptions Aug 29, 2025
@pahud pahud marked this pull request as ready for review August 29, 2025 20:37
@aws-cdk-automation aws-cdk-automation dismissed their stale review August 29, 2025 20:37

✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.

@alvazjor alvazjor self-assigned this Sep 1, 2025
… 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 pahud marked this pull request as draft September 2, 2025 14:33
alvazjor
alvazjor previously approved these changes Oct 8, 2025
@alvazjor alvazjor had a problem deploying to deployment-integ-test October 8, 2025 11:54 — with GitHub Actions Error
@alvazjor
Copy link
Copy Markdown
Contributor

@pahud if this is no longer a draft, can you please update it. I have approved the current version

@pahud
Copy link
Copy Markdown
Contributor Author

pahud commented Oct 24, 2025

@alvazjor OK I will do the final check really quick today and convert it to ready.

@pahud pahud marked this pull request as ready for review October 24, 2025 14:16
@pahud
Copy link
Copy Markdown
Contributor Author

pahud commented Oct 24, 2025

fixing CI

@alvazjor
Copy link
Copy Markdown
Contributor

@pahud there seems to be an issue with this PR, not building. Can you check and rebase please?

@pahud
Copy link
Copy Markdown
Contributor Author

pahud commented Nov 24, 2025

@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
@pahud pahud marked this pull request as draft November 27, 2025 16:51
- 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
@pahud pahud marked this pull request as ready for review November 27, 2025 19:28
@pahud
Copy link
Copy Markdown
Contributor Author

pahud commented Nov 27, 2025

Hi @alvazjor I've refactored the implementation(see PR desc). Please take a look when you get a chance. Thank you.

@alvazjor
Copy link
Copy Markdown
Contributor

Thanks @pahud

@mergify
Copy link
Copy Markdown
Contributor

mergify bot commented Nov 28, 2025

Merge Queue Status Beta

✅ The pull request has been merged

This pull request spent 41 minutes 57 seconds in the queue, including 41 minutes 45 seconds waiting for CI.
The checks were run in-place.

Required conditions to merge

@mergify
Copy link
Copy Markdown
Contributor

mergify bot commented Nov 28, 2025

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).

@mergify
Copy link
Copy Markdown
Contributor

mergify bot commented Nov 28, 2025

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).

@mergify mergify bot merged commit 4056e14 into aws:main Nov 28, 2025
17 of 18 checks passed
@github-actions
Copy link
Copy Markdown
Contributor

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 28, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

contribution/core This is a PR that came from AWS. effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

aws-ec2: Expose EC2 instance MetadataOptions

3 participants