hi guys, wanted to ask your opinion about design pattern for aws lambda go functions.
as you know typical function looks like ( AWS Lambda function handler in Go - AWS Lambda ):
func handler(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
...
}
func main() {
lambda.Start(HandleRequest)
}
but of course in real world, 1 lambda calls another lambdas, have some stuff in envs etc. so I created common code:
type Application struct {
LambdaClient awsutils.LambdaClientInterface
Arn map[ServiceArn]string
OtherEnvs map[string]string
Log zerolog.Logger
Ctx context.Context
}
with NewApplication func that fills up arn map with other envs some helpers like:
func (app *Application) GetUsernArn() string {
return app.GetArn(User)
}
etc.
so now in every lambda I start code like:
type LambdaApp struct {
*lu.Application
}
func (app *LambdaApp) Start() {
lambda.Start(app.handler)
}
func main() {
lambdaClient := awsutils.NewLambdaClient()
serviceArns := []lu.ServiceArn{lu.XYZ, lu.ABC}
otherEnvs := []string{"WHATEVER"}
newApp := LambdaApp{lu.NewApplication(lambdaClient, serviceArns, otherEnvs)}
newApp.Start()
}
func (app *LambdaApp) handler(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
app.Log = logger.GetApiLogger(ctx, request)
app.Ctx = ctx
...
}
this way I have always in one place config for which services this lambda will call and what other envs will take
also logger and ctx is inside LambdaApp as you see, logger I use also custom method that shows me requestId path method body.
ctx well I use always the same from handler, no need to use other like with some specific timeout.
then later in code as of course there is no one huge handler function I divide code and most of the code needs to use logger so instead of declaring everytime I use this one from app ie:
func (app *LambdaApp) validateRequest...{
app.Log.Info().Msg("xyz")
}
I dont know if its good, if I should better declare log first in the beginning of a file under imports and set it on handler ?
the same with ctx.
I jumped to Go from Scala world totally diffent