r/aws • • Aug 26 '19

serverless Good to Go: Getting Started with AWS Lambda and Go

https://epsagon.com/blog/getting-started-with-aws-lambda-and-go/

[removed] — view removed post

0 Upvotes

1 comment sorted by

1

u/notoriousbpg Aug 26 '19

I'm using Go & Lambda to provide resolvers for GraphQL amongst other things. One of the design decisions we made was to use a single Lambda function as the resolver for multiple queries and mutations for related activities, e.g. all of the tasks for managing a user's account such as their profile, address book, preferences etc is in one function, all KYC tasks are in another function etc. We achieve this by placing a switch statement into the Handler, and calling the appropriate logic for the requested query or mutation, the name of which is passed in the resolver request mapping template. Queries/mutations to Lambda functions doesn't have to be 1:1, and this n:1 approach still keeps our code clear without becoming monolithic.

The AWS session is a global variable set on startup, and the database, memcached etc connections are opened in an init() call that only fires once per lifecycle. The init function is also where we do things like grab database credentials from Secrets Manager, and load environment variables like queues, S3 bucket names etc. Assets that vary between environments (e.g. SQS ARNs) are managed as environment variables, in turn populated by Terraform deployments. Cold starts are mitigated with a "StayWarm" invocation triggered by a CloudWatch rule. Each time the Handler is called, the database connection is pinged for validity. With StayWarm in place, there's always at least one function instance ready to service GraphQL.

I find that with Go it's stupidly fast.