Golang AWS SAM Lambda example

Golang AWS SAM Lambda example

This example project shows how to use AWS SAM with two Golang Lambdas.

Files

  • cmd holds the executables that can be built and put into AWS Lambdas.
  • cmd/one and cmd/two are distinctly different Lambdas that handle different API URL paths.
  • util is a helper package that exports typed & wrapped HTTP Lambda interfaces to implement. It's helpful to have the input and outputs as typed Go structures, rather than []byte.
  • template.yml is a template used by AWS SAM for local deployments. AWS SAM can also use this template to deploy to production, but that's not covered in this example project.

Lambda one

This Lambda has shows how to handle environment variables and HTTP request metadata like the requester's source IP. It also has an external API request example.

Its HTTP method is GET.

Example curl:

$ curl http://localhost:3000/one
{
    "customString": "This is a value from an environment variable for Lambda one.",
    "randomPokemon": "raikou",
    "sourceIP": "127.0.0.1",
    "time": "2022-01-13T17:42:22.839696271Z",
    "userAgent": "Custom User Agent String"
}

Lambda two

This Lambda shows how to use custom path parameters and how data can be saved across a single Lambda instance's invocations.

Its HTTP method is POST.

Example curl:

$ curl --request POST http://localhost:3000/two/monkey
{
    "customPath": "monkey",
    "prevCustomPath": ""
}

Debugging

Confirm dlv is installed:

go install github.com/go-delve/delve/cmd/dlv@latest

Go remote debugging must be started shortly before or after AWS SAM gets a request. If a breakpoint is triggered, dlv or the compatible IDE can control and inspect the execution.

Due to and AWS SAM bug and the lack of support for building Go Lambda in containers, newer operating systems such as Ubuntu 21.04+ and Fedora 35+ will not be able to use AWS SAM's remote debugging feature. This has to do with the glibc version. See this GitHub issue.

Error: We do not support building Go Lambda functions within a container. Try building without the container. Most Go functions will build successfully.

Troubleshooting

General

The wrong HTTP method was used, but the Lambda still processed the request

AWS API Gateway request routing is pretty flexible, don't expect the request routing rules in template.yml or an OpenAPI specification to be followed strictly by API Gateway.

Switching the Lambda to use API Gateway V2 makes some parts of the request empty

This is because AWS SAM is only giving the API Gateway V1 format. I think Type: "AWS::Serverless::Function" needs to be changed in template.yml to something that indicates it's a Lambda that is triggered by API Gateway V2. It might also be the type of ApiEvent. I'm not sure if AWS SAM supports that use case yet.

Lambda one

The User Agent returned is always Custom User Agent String

I think this is specific to AWS SAM. I believe the developers didn't include logic to extract that from the request.

The Pokemon returned is always trubbish API

This means the Lambda was unable to successfully get a Pokemon from an external API.

This could be because it failed to connect over the internet. But a more likely scenario is a known bug in AWS SAM. This bug causes AWS SAM to always pass an expired context.Context to the Lambda handler. Since the context is always expired before the request is sent, it will never send.

Please downgrade AWS SAM to version 1.12.0. See this GitHub issue.

Lambda two

The value of prevCustomPath returned by the API is always an empty string or seemingly wrong

This is because AWS SAM spins up and spins down a single Lambda instance for every request. In production AWS will spin up Lambdas until the number of requests per second coming in dictate that instances should be spun down. This is an example of how data can be shared across invocations, but not instances.

Similar Resources

A package for access aws service using AWS SDK for Golang

goaws 馃殌 A package for access aws service using AWS SDK for Golang Advantage with goaws package Example for get user list IAM with AWS SDK for Golang

Nov 25, 2021

Una prueba t茅cnica: Servicio Golang REST API local, sobre Docker, gRPC, AWS Serverless y sobre Kubernetes en AWS EC2

Una prueba t茅cnica: Servicio Golang REST API local, sobre Docker, gRPC, AWS Serverless y sobre Kubernetes en AWS EC2

May 7, 2022

Integrate AWS EKS Anywhere cluster with AWS Services

 Integrate AWS EKS Anywhere cluster with AWS Services

This article provides step-by-step instruction on integrating AWS EKS Anywhere with AWS Services so the applications running on customer data center can securely connect with these services.

Mar 6, 2022

Apis para la administracion de notifiaciones, utilizando servicios como AWS SNS y AWS SQS

notificacion_api Servicio para env铆o de notificaci贸nes por difusi贸n en AWS SNS Especificaciones T茅cnicas Tecnolog铆as Implementadas y Versiones Golang

Jan 7, 2022

AWS credential_process utility to assume AWS IAM Roles with Yubikey Touch and Authenticator App TOPT MFA to provide temporary session credentials; With encrypted caching and support for automatic credential refresh.

AWS credential_process utility to assume AWS IAM Roles with Yubikey Touch and Authenticator App TOPT MFA to provide temporary session credentials; With encrypted caching and support for automatic credential refresh.

AWS credential_process utility to assume AWS IAM Roles with Yubikey Touch and Authenticator App TOPT MFA to provide temporary session credentials; With encrypted caching and support for automatic credential refresh.

Dec 20, 2022

Aws-parameter-bulk - Export AWS SSM Parameter Store values in bulk to .env files

aws-parameter-bulk Utility to read parameters from AWS Systems Manager (SSM) Par

Oct 18, 2022

Aws-console-plugin - The current HashiCorp Vault AWS Secret Engine currently supports the creation of short lived API keys using the IAM User

aws-console-plugin Background The current HashiCorp Vault AWS Secret Engine curr

Feb 7, 2022

Aws-cognito-demo-go - Source code for AWS Cognito in Go

AWS Cognito Demo in Go Source code for YouTube series, AWS Cognito in Go - https

Dec 10, 2022

Lambda Extension for iamlive

iamlive Lambda Extension The iamlive Lambda Extension helps generate a least-privilege IAM policy by monitoring the AWS calls made within the Lambda e

Dec 17, 2022
A Lambda function built with SAM (Serverless Application Module)

AWS SAM Lambda Function 漏 Israel Pereira Tavares da Silva The AWS Serverless Application Model (SAM) is an open-source framework for building serverle

Dec 19, 2021
lambda-go-api-proxy makes it easy to port APIs written with Go frameworks such as Gin to AWS Lambda and Amazon API Gateway.

aws-lambda-go-api-proxy makes it easy to run Golang APIs written with frameworks such as Gin with AWS Lambda and Amazon API Gateway.

Jan 6, 2023
Api-waf-example-cdk - Demo using sam to drive a CDK serverless api

CDK SAM Demo Demo using sam to drive a CDK serverless api Stack Setup go build -

Feb 5, 2022
Aws-cdk-go-examples - Example projects using the AWS CDK by Golang

aws-cdk-go-examples Example projects using the AWS CDK by Golang Useful commands

Nov 24, 2022
This repository shows how can we use `AWS Lambda` to build serverless applications in golang.

Serverless Api in Go with AWS Lambda Here we are going to use AWS Lambda to build serverless applications in golang. Prerequisites You鈥檒l need an AWS

Nov 3, 2021
Lambda stack to turn off and destroy all resources from your personal AWS Account to avoid billing surprises
Lambda stack to turn off and destroy all resources from your personal AWS Account to avoid billing surprises

AWS, Turn off my Account, please Lambda stack to turn off and destroy all resources from your personal AWS Account to avoid billing surprises Resource

Oct 25, 2022
Prueba de concepto: Boletia, una aplicaci贸n para venta de boletos, basada en microservicios event-driven. Desarrollada sobre AWS Serverless: Api Gateway, Lambda, DynamoDB, DynamoDB Streams
Prueba de concepto: Boletia, una aplicaci贸n para venta de boletos, basada en microservicios event-driven. Desarrollada sobre AWS Serverless: Api Gateway, Lambda, DynamoDB, DynamoDB Streams

Prueba de concepto: Boletia, una aplicaci贸n para venta de boletos, basada en microservicios event-driven. Desarrollada sobre AWS Serverless: Api Gatew

May 7, 2022
Go-xrayprofile - Selective profiling of AWS Lambda functions

go-xrayprofile AWS X-Ray is handy for understanding the overall performance of y

May 18, 2022
Mrrobot - A simple greetings bot for Slack that uses events api and hosted on AWS Lambda

Mr. Robot a greeter bot for your slack community build_docker

Aug 21, 2022
Simple no frills AWS S3 Golang Library using REST with V4 Signing (without AWS Go SDK)

simples3 : Simple no frills AWS S3 Library using REST with V4 Signing Overview SimpleS3 is a golang library for uploading and deleting objects on S3 b

Nov 4, 2022