Golang AWS SAM Lambda example

Overview

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/[email protected]

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.

Owner
Micah Parks
Micah Parks
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

Israel P. T. da Silva 0 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.

Amazon Web Services - Labs 588 Jan 20, 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’ll need an AWS

Datum Brain 0 Nov 3, 2021
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

Matheus Fidelis 62 Dec 15, 2021
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

Emilio del Cañal Calleja 0 Dec 6, 2021
Go-xrayprofile - Selective profiling of AWS Lambda functions

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

Aidan Steele 2 Jan 18, 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

Rohan Verma 74 Jan 8, 2022
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

Muhammad Ichsanul Fadhil 1 Nov 25, 2021
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.

Hari Ohm Prasath 1 Oct 25, 2021
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

Universidad Distrital Francisco José de Caldas 0 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.

Ari Palo 9 Jan 21, 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

Adam Malik 8 Jan 16, 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

Ian Mckay 30 Nov 10, 2021
Lambda service function

openfaas-lambda These are sets of example that i've used when migrating aws lambda to openfaas platform. A part of documenting on how it works, I'm op

Alexjander Bacalso 4 Oct 13, 2021
Go lambda ffprobe executable

Execute SAM locally $ cd build $ GOARCH=amd64 GOOS=linux go build ../main.go $ c

Coding Safari 0 Dec 18, 2021
Lambda microservice triggered by API Gateway to lookup ip address, domain or hash (md5, sha1, sha256)

lambdaGatewayAPI Lambda microservice triggered by API Gateway to lookup ip address, domain or hash (md5, sha1, sha256) How to deploy Build the lambdaG

null 0 Dec 21, 2021
aws credential solution by Golang

goCred Aws credential solution by Golang (Works on Linux, Arm, and Windows) v0.3 Detection of unauthorized access Locks access in case of repeated una

null 1 Oct 23, 2021
Pre-constructed source for CDKTF AWS Golang

cdktf-provider-aws-go Terraform CDK aws Provider v3.64.2 go get github.com/hortau/cdktf-provider-aws-go Example: package main import ( "github.com/

JC Dansereau 0 Nov 27, 2021
Golang database/sql driver for AWS Athena

go-athena go-athena is a simple Golang database/sql driver for Amazon Athena. import ( "database/sql" _ "github.com/akrennmair/go-athena" ) f

Andreas Krennmair 1 Dec 14, 2021