Lambda Metadata
The Lambda Metadata utility provides access to the Lambda Metadata Endpoint (LMDS), giving you execution environment metadata like Availability Zone ID.
Key features
- Retrieve Lambda execution environment metadata
- Automatic caching for the sandbox lifetime
- Thread-safe access
- Native AOT compatible
Installation
| dotnet add package AWS.Lambda.Powertools.Metadata
|
Getting started
| using AWS.Lambda.Powertools.Metadata;
public class Function
{
public string Handler(object input, ILambdaContext context)
{
var azId = LambdaMetadata.AvailabilityZoneId;
return $"Running in AZ: {azId}";
}
}
|
| Property |
Type |
Description |
AvailabilityZoneId |
string? |
The AZ where the function is running (e.g., use1-az1), or null when unavailable |
Error handling
1
2
3
4
5
6
7
8
9
10
11
12
13
14 | using AWS.Lambda.Powertools.Metadata;
using AWS.Lambda.Powertools.Metadata.Exceptions;
try
{
var azId = LambdaMetadata.AvailabilityZoneId;
}
catch (LambdaMetadataException ex)
{
Console.WriteLine($"Failed to get metadata: {ex.Message}");
if (ex.StatusCode != -1)
Console.WriteLine($"HTTP Status: {ex.StatusCode}");
}
|
Metadata remains constant for the Lambda sandbox lifetime. If you need to force a refresh:
| LambdaMetadata.Refresh();
|
Thread safety
LambdaMetadata.AvailabilityZoneId is thread-safe. You can access it from multiple concurrent invocations without race conditions.
Use cases
Multi-AZ routing
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 | using AWS.Lambda.Powertools.Metadata;
public class Function
{
public async Task<string> Handler(OrderRequest request, ILambdaContext context)
{
var endpoint = LambdaMetadata.AvailabilityZoneId switch
{
"use1-az1" => "https://service-az1.internal",
"use1-az2" => "https://service-az2.internal",
_ => "https://service.internal"
};
return await ProcessOrder(request, endpoint);
}
}
|
Logging
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 | using AWS.Lambda.Powertools.Logging;
using AWS.Lambda.Powertools.Metadata;
public class Function
{
public Function()
{
Logger.AppendKey("az_id", LambdaMetadata.AvailabilityZoneId);
}
[Logging]
public string Handler(object input, ILambdaContext context)
{
Logger.LogInformation("Processing request");
return "Success";
}
}
|