Documentation
¶
Overview ¶
Package lambdacontext provides access to Lambda execution context information.
This package allows Lambda functions to access metadata about the current invocation, including request ID, function ARN, Cognito identity, and client context. Context information is retrieved from the standard Go context.Context using FromContext().
See https://docs.aws.amazon.com/lambda/latest/dg/golang-context.html
Index ¶
- Variables
- func MaxConcurrency() int
- func NewContext(parent context.Context, lc *LambdaContext) context.Context
- func NewLogHandler(opts ...LogOption) slog.Handler
- func NewLogger(opts ...LogOption) *slog.Logger
- func ReplaceAttr(groups []string, attr slog.Attr) slog.Attr
- type ClientApplication
- type ClientContext
- type CognitoIdentity
- type LambdaContext
- type LogOption
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var FunctionName string
FunctionName the name of the current Lambda Function
var FunctionVersion string
FunctionVersion is the published version of the current instance of the Lambda Function
var LogGroupName string
LogGroupName is the name of the log group that contains the log streams of the current Lambda Function
var LogStreamName string
LogStreamName name of the log stream that the current Lambda Function's logs will be sent to
var MemoryLimitInMB int
MemoryLimitInMB is the configured memory limit for the current instance of the Lambda Function
Functions ¶
func MaxConcurrency ¶ added in v1.51.0
func MaxConcurrency() int
func NewContext ¶
func NewContext(parent context.Context, lc *LambdaContext) context.Context
NewContext returns a new Context that carries value lc.
func NewLogHandler ¶ added in v1.52.0
NewLogHandler returns a slog.Handler for AWS Lambda structured logging. It reads AWS_LAMBDA_LOG_FORMAT and AWS_LAMBDA_LOG_LEVEL from environment, and injects requestId from Lambda context into each log record.
By default, only requestId is injected. Use WithFunctionARN or WithTenantID to include more. See the package examples for usage.
Example ¶
ExampleNewLogHandler demonstrates using NewLogHandler for more control.
package main
import (
"context"
"log/slog"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-lambda-go/lambdacontext"
)
func main() {
// Set up the Lambda-aware slog handler
slog.SetDefault(slog.New(lambdacontext.NewLogHandler()))
lambda.Start(func(ctx context.Context) (string, error) {
slog.InfoContext(ctx, "processing request", "action", "example")
return "success", nil
})
}
Example (WithOptions) ¶
ExampleNewLogHandler_withOptions demonstrates NewLogHandler with additional fields. Use WithFunctionARN() and WithTenantID() to include extra context.
package main
import (
"context"
"log/slog"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-lambda-go/lambdacontext"
)
func main() {
// Set up handler with function ARN and tenant ID fields
slog.SetDefault(slog.New(lambdacontext.NewLogHandler(
lambdacontext.WithFunctionARN(),
lambdacontext.WithTenantID(),
)))
lambda.Start(func(ctx context.Context) (string, error) {
slog.InfoContext(ctx, "multi-tenant request", "tenant", "acme-corp")
return "success", nil
})
}
func NewLogger ¶ added in v1.52.0
NewLogger returns a *slog.Logger configured for AWS Lambda structured logging. This is a convenience function equivalent to slog.New(NewLogHandler(opts...)).
Example ¶
ExampleNewLogger demonstrates the simplest usage of NewLogger for structured logging. The logger automatically injects requestId from Lambda context into each log record.
package main
import (
"context"
"log/slog"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-lambda-go/lambdacontext"
)
func main() {
// Set up the Lambda-aware slog logger
slog.SetDefault(lambdacontext.NewLogger())
lambda.Start(func(ctx context.Context) (string, error) {
// Use slog.InfoContext to include Lambda context in logs
slog.InfoContext(ctx, "processing request", "action", "example")
return "success", nil
})
}
Types ¶
type ClientApplication ¶
type ClientApplication struct {
InstallationID string `json:"installation_id"`
AppTitle string `json:"app_title"`
AppVersionCode string `json:"app_version_code"`
AppPackageName string `json:"app_package_name"`
}
ClientApplication is metadata about the calling application.
type ClientContext ¶
type ClientContext struct {
Client ClientApplication
Env map[string]string `json:"env"`
Custom map[string]string `json:"custom"`
}
ClientContext is information about the client application passed by the calling application.
type CognitoIdentity ¶
CognitoIdentity is the cognito identity used by the calling application.
type LambdaContext ¶
type LambdaContext struct {
AwsRequestID string //nolint: staticcheck
InvokedFunctionArn string //nolint: staticcheck
Identity CognitoIdentity
ClientContext ClientContext
TenantID string `json:",omitempty"`
}
LambdaContext is the set of metadata that is passed for every Invoke.
func FromContext ¶
func FromContext(ctx context.Context) (*LambdaContext, bool)
FromContext returns the LambdaContext value stored in ctx, if any.
Example ¶
package main
import (
"context"
"log"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-lambda-go/lambdacontext"
)
func main() {
lambda.Start(func(ctx context.Context) (string, error) {
lc, _ := lambdacontext.FromContext(ctx)
log.Printf("Request ID: %s", lc.AwsRequestID)
log.Printf("Function ARN: %s", lc.InvokedFunctionArn)
return "success", nil
})
}
type LogOption ¶ added in v1.52.0
type LogOption func(*logOptions)
LogOption is a functional option for configuring the Lambda log handler.
func WithFunctionARN ¶ added in v1.52.0
func WithFunctionARN() LogOption
WithFunctionARN includes the invoked function ARN in log records.
Example ¶
ExampleWithFunctionARN demonstrates using WithFunctionARN to include the function ARN.
package main
import (
"context"
"log/slog"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-lambda-go/lambdacontext"
)
func main() {
// Include only function ARN
slog.SetDefault(lambdacontext.NewLogger(
lambdacontext.WithFunctionARN(),
))
lambda.Start(func(ctx context.Context) (string, error) {
// Log output will include "functionArn" field
slog.InfoContext(ctx, "function invoked")
return "success", nil
})
}
func WithTenantID ¶ added in v1.52.0
func WithTenantID() LogOption
WithTenantID includes the tenant ID in log records (for multi-tenant functions).