Skip to content

Commit e8da630

Browse files
authored
Initial Go Lambda example commit (#24)
add readme --------- Signed-off-by: Madhav Mehta <madhav.mehta@chainguard.dev>
1 parent 452814e commit e8da630

5 files changed

Lines changed: 154 additions & 0 deletions

File tree

aws-lambda-go/Dockerfile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#Define Chainguard org to pull images from
2+
ARG CHAINGUARD_ORG="chainguard"
3+
4+
# BUILD STAGE: Use Chainguard distroless go variant for build stage. You may optionally switch to -dev if additional utilities are needed to build your app.
5+
FROM cgr.dev/${CHAINGUARD_ORG}/go:latest AS build
6+
7+
# Set working directory
8+
WORKDIR /helloworld
9+
10+
# Copy dependencies list
11+
COPY go.mod go.sum ./
12+
13+
# Build
14+
COPY main.go .
15+
RUN go build -o main main.go
16+
17+
# FINAL STAGE: Copy artifacts to a clean image. Chainguard recommends using glibc-dynamic or static as a runtime for go applications. For more info, visit https://images.chainguard.dev/directory/image/go/overview#compatibility-notes
18+
FROM cgr.dev/${CHAINGUARD_ORG}/glibc-dynamic
19+
COPY --from=build /helloworld/main /main
20+
ENTRYPOINT [ "/main" ]

aws-lambda-go/README.md

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# aws-lambda-go
2+
An example of using the Chainguard `python` image as a base image for an AWS
3+
Lambda function as described in [this documentation](https://docs.aws.amazon.com/lambda/latest/dg/go-image.html#go-image-other)
4+
5+
## Requirements
6+
7+
You should have these utilities installed:
8+
9+
- `aws`
10+
- `docker`
11+
- `jq`
12+
13+
## Usage
14+
15+
Export a name for your Lambda function. We'll use this value in later steps.
16+
17+
```
18+
$ export FUNCTION_NAME=chainguard-python-lambda
19+
```
20+
21+
Login to AWS. This may be different depending on how you authenticate to AWS.
22+
23+
```
24+
$ aws sso login
25+
```
26+
27+
Export the AWS account ID and region. We'll use these values in later steps.
28+
29+
```
30+
$ export AWS_REGION=us-west-2
31+
$ export ACCOUNT_ID=$(aws sts get-caller-identity | jq -r .Account)
32+
```
33+
34+
Create an AWS ECR repository.
35+
36+
```
37+
$ aws ecr create-repository --repository-name "${FUNCTION_NAME}"
38+
```
39+
40+
Login to AWS ECR.
41+
42+
```
43+
$ aws ecr get-login-password \
44+
| docker login \
45+
--username AWS \
46+
--password-stdin "${ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com"
47+
```
48+
49+
Build and push the image.
50+
51+
```
52+
$ docker buildx build \
53+
--push \
54+
--platform linux/amd64 \
55+
--provenance=false \
56+
-t "${ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${FUNCTION_NAME}:latest" \
57+
.
58+
```
59+
60+
Create a role for the Lambda.
61+
62+
```
63+
$ aws iam create-role \
64+
--role-name "${FUNCTION_NAME}" \
65+
--assume-role-policy-document '{"Version": "2012-10-17","Statement": [{ "Effect": "Allow", "Principal": {"Service": "lambda.amazonaws.com"}, "Action": "sts:AssumeRole"}]}'
66+
```
67+
68+
Create the Lambda function.
69+
70+
```
71+
$ aws lambda create-function \
72+
--function-name "${FUNCTION_NAME}" \
73+
--package-type Image \
74+
--code "ImageUri=${ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${FUNCTION_NAME}:latest" \
75+
--role "arn:aws:iam::${ACCOUNT_ID}:role/${FUNCTION_NAME}"
76+
```
77+
78+
Invoke the Lambda function.
79+
80+
```
81+
$ aws lambda invoke --function-name "${FUNCTION_NAME}" /dev/stderr >/dev/null
82+
```
83+
84+
The output should look similar to this:
85+
```
86+
{"statusCode":200,"headers":null,"multiValueHeaders":null,"body":"\"Hello from Lambda!\""}
87+
```
88+
89+
## Clean Up
90+
91+
Delete the Lambda function.
92+
93+
```
94+
$ aws lambda delete-function --function-name "${FUNCTION_NAME}"
95+
```
96+
97+
Delete the IAM role.
98+
99+
```
100+
$ aws iam delete-role --role-name "${FUNCTION_NAME}"
101+
```
102+
103+
Delete the ECR repository.
104+
105+
```
106+
$ aws ecr delete-repository --repository-name "${FUNCTION_NAME}" --force
107+
```

aws-lambda-go/go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module aws-lambda-go
2+
3+
go 1.22.5
4+
5+
require github.com/aws/aws-lambda-go v1.50.0 // indirect

aws-lambda-go/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/aws/aws-lambda-go v1.50.0 h1:0GzY18vT4EsCvIyk3kn3ZH5Jg30NRlgYaai1w0aGPMU=
2+
github.com/aws/aws-lambda-go v1.50.0/go.mod h1:dpMpZgvWx5vuQJfBt0zqBha60q7Dd7RfgJv23DymV8A=

aws-lambda-go/main.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import (
4+
"context"
5+
6+
"github.com/aws/aws-lambda-go/events"
7+
"github.com/aws/aws-lambda-go/lambda"
8+
)
9+
10+
func handler(ctx context.Context, event events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
11+
response := events.APIGatewayProxyResponse{
12+
StatusCode: 200,
13+
Body: "\"Hello from Lambda!\"",
14+
}
15+
return response, nil
16+
}
17+
18+
func main() {
19+
lambda.Start(handler)
20+
}

0 commit comments

Comments
 (0)