Skip to content

in-the-keyhole/sls-go-example

Repository files navigation

sls-go-example

This repository contains a VSCode Remote Container Setup for developing GoLang based AWS Lambda functions using the Serverless Framework.

Visual Studio Code - Remote Container Usage

Official Documentation

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

Prerequisites:

  1. macOS, Windows, Linux -- System Requirements
  2. Docker - Documentation
  3. Visual Studio Code - Official Site
  4. Remote - Containers Visual Studio Code extension - Marketplace

Environment Variables

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

Developer Configuration

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.

Usage & Things you can do

Makefile Targets

There are some predefined makefile targets that can be used to simplify common tasks.

  • make deploy - deploys the defined Lambda functions to AWS
  • make undeploy - removes the defined Lambda functions from AWS
  • make 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

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:

  1. 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
  2. Open the Run and Debug View in VS Code (Shft+Cmd+D in OSX or Shft+Ctrl+D in Windows)
  3. Select Debug Lambda Function and press Run
  4. This will start the Lambda Function Handler, with the Debugger attached
  5. To send an event to the running handler, either:
    1. 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
    2. From a Terminal Window, execute awslambdarpc -e events/<choosefile>.json
  6. At this point, it will execute your Handler, and any breakpoints you have set will be honored.
  7. Debug as normal

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published