Documentation
¶
Index ¶
- Constants
- Variables
- func GetParameter(ctx context.Context, input *ssm.GetParameterInput) (*ssm.GetParameterOutput, error)
- func GetSecretValue(ctx context.Context, input *secretsmanager.GetSecretValueInput) (*secretsmanager.GetSecretValueOutput, error)
- func JSON(v interface{}) fmt.Formatter
- func JSONIdent(v interface{}, prefix, indent string) fmt.Formatter
- func LoggerWithContext(ctx context.Context) (_ context.Context, l *zerolog.Logger)
- func SetUpGlobalLogger(ctx context.Context) func()
- func SetUpLogger(ctx context.Context, logger *log.Logger) func()
- func Start[TIn any](handler func(context.Context, TIn) error, options ...lambda.Option)
- func StartCloudWatchEventHandler(handler func(context.Context, events.CloudWatchEvent) error)
- func StartDynamoDBEventHandleFunc(...)
- func StartDynamoDBEventHandler(handler func(context.Context, events.DynamoDBEvent) error)
- func StartHandlerFunc[TIn any, TOut any](handler func(context.Context, TIn) (TOut, error), options ...lambda.Option)
- func StartS3EventHandler(handler func(context.Context, events.S3Event) error)
- func StartSNSEventHandler(handler func(context.Context, events.SNSEvent) error)
- func StartSQSEventHandler(handler func(context.Context, events.SQSEvent) error)
- func StartSQSEventHandlerFunc(...)
- type GetParameterClient
- type GetSecretValueClient
- type ParameterSecretsExtensionClient
- func (l *ParameterSecretsExtensionClient) GetParameter(ctx context.Context, input *ssm.GetParameterInput, _ ...func(*ssm.Options)) (*ssm.GetParameterOutput, error)
- func (l *ParameterSecretsExtensionClient) GetSecretValue(ctx context.Context, input *secretsmanager.GetSecretValueInput, ...) (*secretsmanager.GetSecretValueOutput, error)
Constants ¶
const ( // DebugLogFlags is the flag passed to log.SetFlags by SetUpLogger if IsDebug is true. DebugLogFlags = log.Ldate | log.Lmicroseconds | log.LUTC | log.Llongfile | log.Lmsgprefix // DefaultLogFlags is the flag passed to log.SetFlags by SetUpLogger if IsDebug is false. DefaultLogFlags = DebugLogFlags | log.Lshortfile )
Variables ¶
var DefaultParameterSecretsExtensionClient = &ParameterSecretsExtensionClient{Client: http.DefaultClient}
DefaultParameterSecretsExtensionClient is the client used by package-level GetSecretValue and GetParameter.
var IsDebug bool
IsDebug is true if the "DEBUG" environment have value "1" or "true".
The value of IsDebug is set at startup by way of init(). While many things in the lambda package use this value, nothing will modify it. If you want to use a different environment variable or a different way to toggle DEBUG behaviour, modify this value directly.
Functions ¶
func GetParameter ¶
func GetParameter(ctx context.Context, input *ssm.GetParameterInput) (*ssm.GetParameterOutput, error)
GetParameter is a wrapper around [DefaultClient.GetParameter].
func GetSecretValue ¶
func GetSecretValue(ctx context.Context, input *secretsmanager.GetSecretValueInput) (*secretsmanager.GetSecretValueOutput, error)
GetSecretValue is a wrapper around [DefaultClient.GetSecretValue].
func JSON ¶ added in v0.1.1
JSON returns a fmt.Formatter wrapper that returns the JSON representation of the given struct.
If encoding the struct v fails, falls back to original formatter.
Usage:
log.Printf("request=%s", JSON(v))
func LoggerWithContext ¶ added in v0.1.6
LoggerWithContext returns a valid zerolog.Logger for use.
Because zerolog.Ctx may return a disabled (no-op) logger, it's difficult to determine if user is intentionally disabling logging via context, or if the zerolog.DefaultContextLogger has not been set up. As a result, this method may create a new logger if it can determine that one should be created, and the logger will be attached to the new returned context in that case. If zerolog.DefaultContextLogger is not nil then the returned value from zerolog.Ctx is always used.
Furthermore, if a Lambda context is available from lambdacontext.FromContext, the zerolog.Context of the returned logger is updated with a string awsRequestID.
func SetUpGlobalLogger ¶
SetUpGlobalLogger applies sensible default settings to log.Default instance.
Specifically, log.SetFlags is called with DefaultLogFlags, and if lambdacontext.LambdaContext.AwsRequestId is available then it is set as the log prefix with log.SetPrefix.
A function is returned that should be deferred upon to reset the log flags and prefix back to the original values. Use SetUpLogger if you wish to modify a specific log.Logger.
Usage
// this should be the first line in your AWS Lambda handler. many Start methods in this package will do this // for you by default. // notice the double ()() to make sure SetUpGlobalLogger executes some function first, then its returned // function is deferred. defer logsupport.SetUpGlobalLogger()()
func SetUpLogger ¶
SetUpLogger is a variant of SetUpGlobalLogger that targets a specific log.Logger.
func Start ¶ added in v0.1.1
Start is a variant of StartHandlerFunc for handlers that don't have any explicit returned value.
See StartHandlerFunc for an in-depth explanation on what are available.
func StartCloudWatchEventHandler ¶ added in v0.1.1
func StartCloudWatchEventHandler(handler func(context.Context, events.CloudWatchEvent) error)
StartCloudWatchEventHandler logs the CloudWatch event (without Detail attribute) as `event` JSON property.
func StartDynamoDBEventHandleFunc ¶ added in v0.1.1
func StartDynamoDBEventHandleFunc(handler func(context.Context, events.DynamoDBEvent) (events.DynamoDBEventResponse, error))
StartDynamoDBEventHandleFunc logs the number of records and the number of batch item failure as `recordCount` and `batchItemFailureCount` counters respectively.
func StartDynamoDBEventHandler ¶ added in v0.1.1
func StartDynamoDBEventHandler(handler func(context.Context, events.DynamoDBEvent) error)
StartDynamoDBEventHandler logs the number of records as `recordCount` counter.
func StartHandlerFunc ¶ added in v0.1.1
func StartHandlerFunc[TIn any, TOut any](handler func(context.Context, TIn) (TOut, error), options ...lambda.Option)
StartHandlerFunc is a wrapper around lambda.StartHandlerFunc that adds sensible defaults.
What is available out of the box with this wrapper:
- SetUpGlobalLogger is used to prefix the AWS Request Id to every log message.
- A metrics.Metrics instance is attached to the context with a few default metrics logged at the end of the request such as start and end time, metrics.Metrics.Panicked or metrics.Metrics.Fault if the underlying handler panics or returns an error. The wrapper will not attempt to recover so that the stack trace can propagate. The metrics.Metrics instance can be retrieved via metrics.Ctx.
func StartS3EventHandler ¶ added in v0.1.1
StartS3EventHandler logs the number of records as `recordCount` counter.
func StartSNSEventHandler ¶ added in v0.1.1
StartSNSEventHandler logs the number of records as `recordCount` counter.
func StartSQSEventHandler ¶ added in v0.1.1
StartSQSEventHandler logs the number of records as `recordCount` counter.
func StartSQSEventHandlerFunc ¶ added in v0.1.1
func StartSQSEventHandlerFunc(handler func(context.Context, events.SQSEvent) (events.SQSEventResponse, error))
StartSQSEventHandlerFunc logs the number of records and the number of batch item failure as `recordCount` and `batchItemFailureCount` counters respectively.
Types ¶
type GetParameterClient ¶
type GetParameterClient interface {
GetParameter(ctx context.Context, params *ssm.GetParameterInput, optFns ...func(*ssm.Options)) (*ssm.GetParameterOutput, error)
}
GetParameterClient abstracts the GetParameter API that has an implementation using AWS Parameter and Secrets Lambda extension (ParameterSecretsExtensionClient).
type GetSecretValueClient ¶
type GetSecretValueClient interface {
GetSecretValue(ctx context.Context, params *secretsmanager.GetSecretValueInput, optFns ...func(*secretsmanager.Options)) (*secretsmanager.GetSecretValueOutput, error)
}
GetSecretValueClient abstracts the GetSecretValue API that has an implementation using AWS Parameter and Secrets Lambda extension (ParameterSecretsExtensionClient).
type ParameterSecretsExtensionClient ¶
type ParameterSecretsExtensionClient struct { // Client is the HTTP client to use for making HTTP requests. // // If nil, http.DefaultClient is used. Client *http.Client // contains filtered or unexported fields }
ParameterSecretsExtensionClient implements both GetParameterClient and GetSecretValueClient using the AWS Parameter and Secrets Lambda extension.
See https://docs.aws.amazon.com/secretsmanager/latest/userguide/retrieving-secrets_lambda.html and https://docs.aws.amazon.com/systems-manager/latest/userguide/ps-integration-lambda-extensions.html.
The zero-value DefaultParameterSecretsExtensionClient is ready for use.
func (*ParameterSecretsExtensionClient) GetParameter ¶
func (l *ParameterSecretsExtensionClient) GetParameter(ctx context.Context, input *ssm.GetParameterInput, _ ...func(*ssm.Options)) (*ssm.GetParameterOutput, error)
func (*ParameterSecretsExtensionClient) GetSecretValue ¶
func (l *ParameterSecretsExtensionClient) GetSecretValue(ctx context.Context, input *secretsmanager.GetSecretValueInput, _ ...func(*secretsmanager.Options)) (*secretsmanager.GetSecretValueOutput, error)