Archetype Constructors

Sparta’s archetype package provides convenience functions to simplify creating AWS Lambda functions for specific types of event sources. See each section for more details:

  • Event Bridge
  • The EventBridge lambda event source allows you to trigger lambda functions in response to either cron schedules or account events. There are two different archetype functions available. Scheduled Scheduled Lambdas execute either at fixed times or periodically depending on the schedule expression. To create a scheduled function use a constructor as in: import ( spartaArchetype "github.com/mweagle/Sparta/v3/archetype" ) // EventBridge reactor function func echoEventBridgeEvent(ctx context.Context, msg json.RawMessage) (interface{}, error) { logger, _ := ctx.

  • CodeCommit
  • The CodeCommit Lambda event source allows you to trigger lambda functions in response to CodeCommit repository events. Events Lambda functions triggered in response to CodeCommit evetms use a combination of events and branches to manage which state changes trigger your lambda function. To create an event subscriber use a constructor as in: // CodeCommit reactor function func reactorFunc(ctx context.Context, event awsLambdaEvents.CodeCommitEvent) (interface{}, error) { logger, _ := ctx.Value(sparta.ContextKeyRequestLogger).(*zerolog.Logger) logger.Info(). Interface("Event", event).

  • CloudWatch
  • The CloudWatch Logs Lambda event source allows you to trigger lambda functions in response to either cron schedules or account events. There are three different archetype functions available. Scheduled Scheduled Lambdas execute either at fixed times or periodically depending on the schedule expression. To create a scheduled function use a constructor as in: import ( awsLambdaEvents "github.com/aws/aws-lambda-go/events" spartaArchetype "github.com/mweagle/Sparta/v3/archetype" ) // CloudWatch reactor function func reactorFunc(ctx context.Context, cwLogs awsLambdaEvents.CloudwatchLogsEvent) (interface{}, error) { logger, _ := ctx.

  • DynamoDB
  • To create a DynamoDB reactor that subscribes via an EventSourceMapping, use the NewDynamoDBReactor constructor as in: import ( awsLambdaEvents "github.com/aws/aws-lambda-go/events" spartaArchetype "github.com/mweagle/Sparta/v3/archetype" ) // DynamoDB reactor function func reactorFunc(ctx context.Context, dynamoEvent awsLambdaEvents.DynamoDBEvent) (interface{}, error) { logger, _ := ctx.Value(sparta.ContextKeyRequestLogger).(*zerolog.Logger) logger.Info(). Interface("Event", dynamoEvent). Msg("DynamoDB Event") return "Hello World 👋. Welcome to AWS Lambda! 🙌🎉🍾", nil } func main() { // ... handler := spartaArchetype.DynamoDBReactorFunc(reactorFunc) lambdaFn, lambdaFnErr := spartaArchetype.NewDynamoDBReactor(handler, "DYNAMO_DB_ARN_OR_CLOUDFORMATION_REF_VALUE", "TRIM_HORIZON", 10, nil) }

  • Kinesis
  • To create a Kinesis Stream reactor that subscribes via an EventSourceMapping, use the NewKinesisReactor constructor as in: import ( awsLambdaEvents "github.com/aws/aws-lambda-go/events" spartaArchetype "github.com/mweagle/Sparta/v3/archetype" ) // KinesisStream reactor function func reactorFunc(ctx context.Context, kinesisEvent awsLambdaEvents.KinesisEvent) (interface{}, error) { logger, _ := ctx.Value(sparta.ContextKeyRequestLogger).(*zerolog.Logger) logger.Info(). Interface("Event", kinesisEvent). Msg("Kinesis Event") return "Hello World 👋. Welcome to AWS Lambda! 🙌🎉🍾", nil } func main() { // ... handler := spartaArchetype.KinesisReactorFunc(reactorFunc) lambdaFn, lambdaFnErr := spartaArchetype.NewKinesisReactor(handler, "KINESIS_STREAM_ARN_OR_CLOUDFORMATION_REF_VALUE", "TRIM_HORIZON", 10, nil) }

  • Kinesis Firehose
  • There are two ways to create a Firehose Transform reactor that transforms a KinesisFirehoseEventRecord with a Lambda function: NewKinesisFirehoseLambdaTransformer Transform using a Lambda function NewKinesisFirehoseTransformer Transform using a go text/template declaration NewKinesisFirehoseLambdaTransformer import ( awsEvents "github.com/aws/aws-lambda-go/events" spartaArchetype "github.com/mweagle/Sparta/v3/archetype" ) // KinesisStream reactor function func reactorFunc(ctx context.Context, record *awsEvents.KinesisFirehoseEventRecord) (*awsEvents.KinesisFirehoseResponseRecord, error) { logger, _ := ctx.Value(sparta.ContextKeyRequestLogger).(*zerolog.Logger) logger.Info(). Interface("Record", record). Msg("Kinesis Firehose Event") responseRecord = &awsEvents.

  • REST Service
  • The rest package provides convenience functions to define a serverless REST style service. The package uses three concepts: Routes: URL paths that resolve to a single go struct Resources: go structs that optionally define HTTP method (GET, POST, etc.). ResourceDefinition: an interface that go structs must implement in order to support resource-based registration. Routes Routes are similar many HTTP-routing libraries. They support path parameters. Resources Resources are the targets of Routes.

  • S3
  • There are two different S3-based constructors depending on whether your lambda function should use an Object Key Name filter. The S3 subscriber is preconfigured to be notified of both s3:ObjectCreated:* and s3:ObjectRemoved:* events. Object Key Name Filtering Object key name filtering only invokes a lambda function when objects with the given prefix are created. To subscribe to object events created by objects with a given prefix, use the NewS3ScopedReactor constructor as in:

  • SNS
  • To create a SNS reactor that subscribes via an subscription configuration, use the NewSNSReactor constructor as in: import ( awsLambdaEvents "github.com/aws/aws-lambda-go/events" spartaArchetype "github.com/mweagle/Sparta/v3/archetype" ) // DynamoDB reactor function func reactorFunc(ctx context.Context, snsEvent awsLambdaEvents.SNSEvent) (interface{}, error) { logger, _ := ctx.Value(sparta.ContextKeyRequestLogger).(*zerolog.Logger) logger.Info(). Interface("Event", snsEvent). Msg("SNS Event") return "Hello World 👋. Welcome to AWS Lambda! 🙌🎉🍾", nil } func main() { // ... handler := spartaArchetype.SNSReactorFunc(reactorFunc) lambdaFn, lambdaFnErr := spartaArchetype.NewDynamoDBReactor(handler, "SNS_ARN_OR_CLOUDFORMATION_REF_VALUE", nil) }