|
2 | 2 |
|
3 | 3 | **AWS has [deprecated go1.x](https://aws.amazon.com/blogs/compute/migrating-aws-lambda-functions-from-the-go1-x-runtime-to-the-custom-runtime-on-amazon-linux-2/) runtime.** |
4 | 4 |
|
5 | | -This repository contains example lambda handler integration with API gateway. Terraform is used to deploy infrastructure including DNS, SSL Certificates. |
| 5 | +This repository contains example lambda handler integration with API Gateway. Terraform is used to deploy infrastructure including DNS, SSL Certificates. |
6 | 6 |
|
7 | 7 | ## Requirements |
8 | 8 |
|
9 | | -- go 1.20 |
| 9 | +- go 1.22 |
10 | 10 | - zip |
11 | 11 | - docker |
12 | 12 | - terraform |
13 | 13 | - aws cli |
| 14 | +- jq |
14 | 15 |
|
15 | | -## Setup (optional) |
| 16 | +## Setup (Optional) |
16 | 17 |
|
17 | | -### Docker |
| 18 | +For now an existing AWS ECR is required to exist already or created using `./create_ecr.sh`. Unfortunately it's not possible yet to create an ECR, build and push an image, then reference the image_uri needed for Lambda resource. |
18 | 19 |
|
19 | | -Although not require specifically to run go in lambdas, can be useful to avoid compatibility issues |
20 | | -when using cgo bindings or third party libraries using cgo such as sqlite3. |
| 20 | +If you an existing ECR you can run `./create_ecr.sh` with the `--auth-only` flag to skip creating a new ECR. This will pull credentials and inject into Docker engine. |
21 | 21 |
|
22 | | -- build docker container for building lambda function binary fully compatible with aws lambda runtime |
23 | | -- `docker build -t lambda-build .` |
| 22 | +```shell |
| 23 | +Usage: ./create-ecr.sh --repo-name NAME --region REGION --account-id ID [--auth-only] |
| 24 | + --repo-name ECR repository name (required) |
| 25 | + --region AWS region (required) |
| 26 | + --account-id AWS account ID (required) |
| 27 | + --auth-only Only login Docker to ECR and exit |
| 28 | +``` |
24 | 29 |
|
25 | 30 | ## Build |
26 | 31 |
|
27 | | -AWS `provided.al2` requires executables to be named bootstrap. |
| 32 | +The deploy artifact is a container image instead of a zip file with the built binary. AWS has optimized container images for Lambda and results in faster cold starts. |
28 | 33 |
|
29 | | -- `GOARCH=amd64 GOOS=linux go build -tags lambda.norpc -o ./infrastructure/bootstrap main.go` |
30 | | - - `provided.al2` provide a single process architecture and allow running without RPC dependency using `-tags lambda.norpc` |
31 | | - - Build without RPC using `GOARCH=amd64 GOOS=linux go build -tags lambda.norpc -o ./infrastructure/bootstrap main.go` |
| 34 | +To build an image and tag using `tag --points-at HEAD --sort=-version:refname` run `./build_image.sh`. This script will built a Lambda compatible container image and push to the configured AWS ECR. This script expects `./create_ecr.sh` to have run or at the minimum run with `--auth-only` for an existing ECR to get login credentials for Docker engine. |
32 | 35 |
|
33 | | -- `go build -o ./bin/main.local ./cmd/handler/main.go` for locally running lambda handler as a cli application. |
34 | | - - requires manually providing event data from file or hard coded into the main.go file |
35 | | - - it can be very easy to write a cli interface to read event from path or stdin or wrap in a http server |
| 36 | +```shell |
| 37 | +Usage: ./build_image.sh --account-id ID --region REGION --repo-name NAME [--tag TAG] |
| 38 | + --account-id AWS account ID (required) |
| 39 | + --region AWS region (required) |
| 40 | + --repo-name ECR repository name (required) |
| 41 | + --tag Image tag override; if omitted, use latest Git tag on HEAD |
| 42 | +``` |
36 | 43 |
|
37 | 44 | ## Deploy |
38 | 45 |
|
39 | 46 | Create a `terraform.tfvars` file inside infrastructure folder and fill out required variables. |
40 | 47 |
|
41 | | -- Manually deploy function code changes with cli `aws lambda update-function-code --function-name go-lambda --zip-file fileb://bootstrap.zip` |
42 | 48 | - Use terraform to deploy infrastructure changes and function changes `terraform apply --auto-approve` |
43 | | - - `main.aws_lambda_function.go_lambda.source_code_hash = filebase64sha256("bootstrap.zip")` will ensure new builds to redeploy with infrastructure changes. |
| 49 | +
|
| 50 | +To enable auto container image builds set `with_docker_build` to `true`. This will run the `./build_image.sh` script and output the image URI for use in the Lambda resource. Default is disabled. |
44 | 51 |
|
45 | 52 | ## Optional |
46 | 53 |
|
47 | | -Currently, the lambda handler takes in any request method and path. For full utilization of api gateway proxy functionality you can make use of <https://github.com/awslabs/aws-lambda-go-api-proxy> to run a standard Go http server and adapt api gateway requests to Go requests and vice versa Go responses to api gateway responses |
| 54 | +Currently, the lambda handler takes in any request method and path. For full utilization of API Gateway proxy functionality you can make use of <https://github.com/awslabs/aws-lambda-go-api-proxy> to run a standard Go http server and adapt API Gateway requests to Go requests and vice versa Go responses to API Gateway responses |
48 | 55 |
|
49 | 56 | - Be aware to write lambda aware handlers using the adapters. |
50 | 57 | - Optimize for fast startups |
|
0 commit comments