This repository contains a VSCode Remote Container Setup for developing GoLang based AWS Lambda functions using the Serverless Framework.
Note: Once you meet the prerequisites, you can run ANY Visual Studio Code Remote Container, which provides a Docker based development environment ensuring a consistent and reliable set of tooling needed to interact and execute a repository codebase
- macOS, Windows, Linux -- System Requirements
- Docker - Documentation
- Visual Studio Code - Official Site
- Remote - Containers Visual Studio Code extension - Marketplace
The remote container honors the following environment variables to be set in the .devcontainer/.env
Note: You can copy the .devcontainer/.env.template file to .devcontainer/.env and supply the following variables
- AWS_ACCESS_KEY_ID
- AWS_SECRET_ACCESS_KEY
- AWS_REGION (optional) - defaults to
us-east-2
To prepare the environment for offline capabilities, once the repository is opened in the Remote Container, open a Terminal and type:
yarn
This will install the required serverless-offline
dependency.
There are some predefined makefile targets that can be used to simplify common tasks.
make deploy
- deploys the defined Lambda functions to AWSmake undeploy
- removes the defined Lambda functions from AWSmake offline
- runs the Lambda functions defined in serverless.yml locally. It also provides a mock AWS API Gateway.
Note: Due to limitations in serverless-offline with GoLang, this does NOT allow for debugging
This Remote Container is preconfigured so that any Go Lambda handler can be debugged individually. There is NOT, however, currently a way to do this via an API (over http(s)). Since a Lambda function is essentially just an RPC call, we instead will simply start the Function with the Debugger attached, and then make an "RPC" call sending the desired payload to the function.
In this case the input type for these function(s) is an APIGatewayProxyRequest. Therefore, input JSON must be in that form. As a convention, there is an events
folder at the root of the workspace to house these event payloads. You can create as many of these files as necessary for testing and debugging your functions. You can look at the pre-existing files in the event directory as examples, but you can also find more examples here
To debug GoLang based Lambda functions, use the following procedure:
- Open the .go file containing the Lambda Handler - i.e. hello/main.go
- Set breakpoints as you wish in the left gutter of the editor
- Open the Run and Debug View in VS Code (Shft+Cmd+D in OSX or Shft+Ctrl+D in Windows)
- Select Debug Lambda Function and press Run
- This will start the Lambda Function Handler, with the Debugger attached
- To send an event to the running handler, either:
- From the Command Palette (Cmd+Shft+P or Ctrl+Shft+P) select Run Task, then choose
send-lambda-event
, then choose the events/*.json file you wish to send as a Payload...or - From a Terminal Window, execute
awslambdarpc -e events/<choosefile>.json
- From the Command Palette (Cmd+Shft+P or Ctrl+Shft+P) select Run Task, then choose
- At this point, it will execute your Handler, and any breakpoints you have set will be honored.
- Debug as normal