Lambda Metadata
Lambda Metadata provides idiomatic access to the Lambda Metadata Endpoint (LMDS), eliminating boilerplate code for retrieving execution environment metadata like Availability Zone ID.
Key features¶
- Retrieve Lambda execution environment metadata with a single method call
- Automatic caching for the sandbox lifetime, avoiding repeated HTTP calls
- Thread-safe access for concurrent executions (compatible with Lambda Managed Instances)
- Automatic SnapStart cache invalidation via CRaC integration
- Lightweight with minimal external dependencies, using built-in
HttpURLConnection - GraalVM support
Getting started¶
Installation¶
1 2 3 4 5 6 7 8 9 | |
1 2 3 4 5 6 7 8 9 10 | |
IAM Permissions¶
No additional IAM permissions are required. The Lambda Metadata Endpoint is available within the Lambda execution environment and uses a Bearer token provided automatically via environment variables.
Basic usage¶
Retrieve metadata using LambdaMetadataClient.get():
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
At launch, only availabilityZoneId is available. The API is designed to support additional metadata fields as LMDS evolves.
Caching behavior¶
Metadata is cached automatically after the first call. Subsequent calls return the cached value without making HTTP requests.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
This is safe because metadata (like Availability Zone) never changes during a sandbox's lifetime.
Advanced¶
Eager loading at module level¶
For predictable latency, fetch metadata at class initialization:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
SnapStart considerations¶
When using SnapStart, the function may restore in a different Availability Zone. The utility automatically handles this by registering with CRaC to invalidate the cache after restore.
Using the same eager loading pattern above, the cache is automatically invalidated on SnapStart restore, ensuring subsequent calls to LambdaMetadataClient.get() return refreshed metadata.
For module-level usage with SnapStart, ensure LambdaMetadataClient is referenced during initialization so the CRaC hook registers before the snapshot is taken.
Lambda Managed Instances¶
For Lambda Managed Instances (multi-threaded concurrency), no changes are needed. The utility uses thread-safe caching with AtomicReference to ensure correct behavior across concurrent executions on the same instance.
1 2 3 4 5 6 7 8 9 10 11 12 | |
Error handling¶
The utility throws LambdaMetadataException when the metadata endpoint is unavailable or returns an error:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | |
Testing your code¶
When running outside a Lambda execution environment (e.g., in unit tests), the AWS_LAMBDA_METADATA_API and AWS_LAMBDA_METADATA_TOKEN environment variables are not available. Calling LambdaMetadataClient.get() in this context throws a LambdaMetadataException.
Mocking LambdaMetadataClient¶
For tests where you need to control the metadata values, use Mockito's mockStatic to mock LambdaMetadataClient.get():
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | |
Using WireMock¶
For integration tests, you can use WireMock to mock the metadata HTTP endpoint. Set AWS_LAMBDA_METADATA_API and AWS_LAMBDA_METADATA_TOKEN environment variables using junit-pioneer, and stub the endpoint response:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | |
Using with other Powertools utilities¶
Lambda Metadata integrates seamlessly with other Powertools utilities to enrich your observability data with Availability Zone information.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | |