Skip to content

Instantly share code, notes, and snippets.

@josephspurrier
Created December 3, 2020 02:14
Show Gist options
  • Save josephspurrier/05b9126279703a81122cba198df50d6f to your computer and use it in GitHub Desktop.
Save josephspurrier/05b9126279703a81122cba198df50d6f to your computer and use it in GitHub Desktop.
AWS Lambda Container in Go
# Create the working directory
mkdir hello

# Change to the directory
cd hello

# Create the files: main.go and Dockerfile

# Initialize the module in Go
go mod init example.com/hello

# Build the go app for Linux
GOOS=linux go build

# Build the docker image
docker build -t lambdatest .

# Run the docker image
docker run -p 9000:8080 lambdatest

# Send a CURL request with a payload
curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"name":"steve"}'

# Should output: "Hello steve!"
FROM public.ecr.aws/lambda/go:1
# Copy function code
COPY hello ${LAMBDA_TASK_ROOT}
# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
CMD [ "hello" ]
package main
import (
"context"
"fmt"
"github.com/aws/aws-lambda-go/lambda"
)
type MyEvent struct {
Name string `json:"name"`
}
func HandleRequest(ctx context.Context, name MyEvent) (string, error) {
return fmt.Sprintf("Hello %s!", name.Name), nil
}
func main() {
lambda.Start(HandleRequest)
}
@tomsoderlund
Copy link

Thank you for this, I created https://github.com/Binogi/golang-aws-lambda-docker partly based on your Gist.

@josephspurrier
Copy link
Author

Thank you for this, I created https://github.com/Binogi/golang-aws-lambda-docker partly based on your Gist.

I appreciate the thanks and sharing your work!!

@Stoff81
Copy link

Stoff81 commented Dec 22, 2022

When doing docker build I get this:

 => [internal] load build definition from Dockerfile                       0.0s
 => => transferring dockerfile: 37B                                        0.0s
 => [internal] load .dockerignore                                          0.0s
 => => transferring context: 2B                                            0.0s
 => [internal] load metadata for public.ecr.aws/lambda/go:1                1.1s
 => [internal] load build context                                          0.0s
 => => transferring context: 2B                                            0.0s
 => [1/2] FROM public.ecr.aws/lambda/go:1@sha256:b0fe93c122d4113a8a6470c3  0.0s
 => ERROR [2/2] COPY hello /var/task                                       0.0s
------
 > [2/2] COPY hello /var/task:
------
failed to compute cache key: "/hello" not found: not found```

@Stoff81
Copy link

Stoff81 commented Dec 22, 2022

Needed to go go build

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment