Skip to content

Commit a609911

Browse files
authored
Merge pull request #1 from ryichk/add-echo-lambda-function-url
Add SAM template to expose the Echo server as a Lambda Function URL using the Lambda Web Adapter
2 parents 91a3d15 + 3d61c56 commit a609911

10 files changed

+321
-0
lines changed

.dockerignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.aws-sam
2+
samconfig.toml

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
bootstrap
2+
3+
.env
4+
.aws-sam/
5+
.DS_Store

Dockerfile

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
FROM golang:1.23-alpine AS build_base
2+
RUN apk add --no-cache git
3+
WORKDIR /app
4+
5+
COPY . .
6+
RUN go mod download
7+
8+
RUN GOOS=linux CGO_ENABLED=0 go build -o bootstrap ./app
9+
FROM alpine:3.9
10+
RUN apk add ca-certificates
11+
COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.8.4 /lambda-adapter /opt/extensions/lambda-adapter
12+
COPY --from=build_base /app/bootstrap /app/bootstrap
13+
14+
ENV PORT=8000 AWS_LWA_ASYNC_INIT=true AWS_LWA_PASS_THROUGH_PATH=/ RUST_BACKTRACE=1
15+
EXPOSE 8000
16+
17+
CMD ["/app/bootstrap"]

README.md

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# go-echo-lambda-function-url-template
2+
3+
This is a template for publishing Go's Echo Web app with Lambda Function URL using Lambda Web Adapter.
4+
5+
## Requirements
6+
7+
* AWS CLI already configured with Administrator permission
8+
* [Docker installed](https://www.docker.com/community-edition)
9+
* SAM CLI - [Install the SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html)
10+
11+
You may need the following for local testing.
12+
* [Golang](https://golang.org)
13+
14+
## Setup process
15+
16+
### Installing dependencies & building the target
17+
18+
In this example we use the built-in `sam build` to build a docker image from a Dockerfile and then copy the source of your application inside the Docker image.
19+
Read more about [SAM Build here](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-cli-command-reference-sam-build.html)
20+
21+
### Local development
22+
23+
```shell
24+
go run app/main.go
25+
```
26+
27+
If the previous command ran successfully you should now be able to hit the following local endpoint to invoke your function `http://localhost:8000/`
28+
29+
## Packaging and deployment
30+
31+
AWS Lambda Golang runtime requires a flat folder with the executable generated on build step. SAM will use `CodeUri` property to know where to look up for the application:
32+
33+
```yaml
34+
...
35+
FirstFunction:
36+
Type: AWS::Serverless::Function
37+
Properties:
38+
CodeUri: app/
39+
...
40+
```
41+
42+
To deploy your application for the first time, run the following in your shell:
43+
44+
```shell
45+
sam deploy --guided
46+
```
47+
48+
The command will package and deploy your application to AWS, with a series of prompts:
49+
50+
* **Stack Name**: The name of the stack to deploy to CloudFormation. This should be unique to your account and region, and a good starting point would be something matching your project name.
51+
* **AWS Region**: The AWS region you want to deploy your app to.
52+
* **Confirm changes before deploy**: If set to yes, any change sets will be shown to you before execution for manual review. If set to no, the AWS SAM CLI will automatically deploy application changes.
53+
* **Allow SAM CLI IAM role creation**: Many AWS SAM templates, including this example, create AWS IAM roles required for the AWS Lambda function(s) included to access AWS services. By default, these are scoped down to minimum required permissions. To deploy an AWS CloudFormation stack which creates or modifies IAM roles, the `CAPABILITY_IAM` value for `capabilities` must be provided. If permission isn't provided through this prompt, to deploy this example you must explicitly pass `--capabilities CAPABILITY_IAM` to the `sam deploy` command.
54+
* **Save arguments to samconfig.toml**: If set to yes, your choices will be saved to a configuration file inside the project, so that in the future you can just re-run `sam deploy` without parameters to deploy changes to your application.
55+
56+
You can find your API Gateway Endpoint URL in the output values displayed after deployment.
57+
58+
### Testing
59+
60+
We use `testing` package that is built-in in Golang and you can simply run the following command to run our tests locally:
61+
62+
```shell
63+
go test ./app -v
64+
```
65+
66+
## Appendix
67+
68+
### Golang installation
69+
70+
Please ensure Go 1.x (where 'x' is the latest version) is installed as per the instructions on the official golang website: https://golang.org/doc/install
71+
72+
A quickstart way would be to use Homebrew, chocolatey or your linux package manager.
73+
74+
#### Homebrew (Mac)
75+
76+
Issue the following command from the terminal:
77+
78+
```shell
79+
brew install golang
80+
```
81+
82+
If it's already installed, run the following command to ensure it's the latest version:
83+
84+
```shell
85+
brew update
86+
brew upgrade golang
87+
```
88+
89+
#### Chocolatey (Windows)
90+
91+
Issue the following command from the powershell:
92+
93+
```shell
94+
choco install golang
95+
```
96+
97+
If it's already installed, run the following command to ensure it's the latest version:
98+
99+
```shell
100+
choco upgrade golang
101+
```
102+
103+
## Bringing to the next level
104+
105+
Here are a few ideas that you can use to get more acquainted as to how this overall process works:
106+
107+
* Create an additional API resource (e.g. /hello/{proxy+}) and return the name requested through this new path
108+
* Update unit test to capture that
109+
* Package & Deploy
110+
111+
Next, you can use the following resources to know more about beyond hello world samples and how others structure their Serverless applications:
112+
113+
* [AWS Serverless Application Repository](https://aws.amazon.com/serverless/serverlessrepo/)

app/main.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/labstack/echo/v4"
7+
"github.com/labstack/echo/v4/middleware"
8+
)
9+
10+
func main() {
11+
e := echo.New()
12+
13+
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
14+
AllowOrigins: []string{"*"},
15+
AllowMethods: []string{echo.GET, echo.POST, echo.PUT, echo.DELETE},
16+
}))
17+
18+
e.GET("/", hello)
19+
e.Logger.Fatal(e.Start(":8000"))
20+
}
21+
22+
func hello(c echo.Context) error {
23+
return c.String(http.StatusOK, "Hello!")
24+
}

app/main_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"testing"
7+
8+
"github.com/labstack/echo/v4"
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestHello(t *testing.T) {
13+
e := echo.New()
14+
e.GET("/", hello)
15+
16+
req := httptest.NewRequest(http.MethodGet, "/", nil)
17+
req.Header.Set(echo.HeaderOrigin, "http://localhost")
18+
19+
rec := httptest.NewRecorder()
20+
c := e.NewContext(req, rec)
21+
22+
if assert.NoError(t, hello(c)) {
23+
assert.Equal(t, http.StatusOK, rec.Code)
24+
assert.Equal(t, "Hello!", rec.Body.String())
25+
}
26+
}

go.mod

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module go-echo-lambda-function-url
2+
3+
go 1.23.0
4+
5+
require (
6+
github.com/labstack/echo/v4 v4.12.0
7+
github.com/stretchr/testify v1.8.4
8+
)
9+
10+
require (
11+
github.com/davecgh/go-spew v1.1.1 // indirect
12+
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
13+
github.com/labstack/gommon v0.4.2 // indirect
14+
github.com/mattn/go-colorable v0.1.13 // indirect
15+
github.com/mattn/go-isatty v0.0.20 // indirect
16+
github.com/pmezard/go-difflib v1.0.0 // indirect
17+
github.com/valyala/bytebufferpool v1.0.0 // indirect
18+
github.com/valyala/fasttemplate v1.2.2 // indirect
19+
golang.org/x/crypto v0.22.0 // indirect
20+
golang.org/x/net v0.24.0 // indirect
21+
golang.org/x/sys v0.19.0 // indirect
22+
golang.org/x/text v0.14.0 // indirect
23+
golang.org/x/time v0.5.0 // indirect
24+
gopkg.in/yaml.v3 v3.0.1 // indirect
25+
)

go.sum

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
4+
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
5+
github.com/labstack/echo/v4 v4.12.0 h1:IKpw49IMryVB2p1a4dzwlhP1O2Tf2E0Ir/450lH+kI0=
6+
github.com/labstack/echo/v4 v4.12.0/go.mod h1:UP9Cr2DJXbOK3Kr9ONYzNowSh7HP0aG0ShAyycHSJvM=
7+
github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0=
8+
github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU=
9+
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
10+
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
11+
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
12+
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
13+
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
14+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
15+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
16+
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
17+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
18+
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
19+
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
20+
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
21+
github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
22+
golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30=
23+
golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M=
24+
golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w=
25+
golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=
26+
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
27+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
28+
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
29+
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
30+
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
31+
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
32+
golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk=
33+
golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
34+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
35+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
36+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
37+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

samconfig.toml

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# More information about the configuration file can be found here:
2+
# https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-config.html
3+
version = 0.1
4+
5+
[default]
6+
[default.global.parameters]
7+
stack_name = "go-echo-app"
8+
9+
[default.build.parameters]
10+
parallel = true
11+
12+
[default.validate.parameters]
13+
lint = true
14+
15+
[default.deploy.parameters]
16+
capabilities = "CAPABILITY_IAM"
17+
confirm_changeset = true
18+
resolve_s3 = true
19+
resolve_image_repos = true
20+
s3_prefix = "go-echo-app"
21+
region = "ap-northeast-1"
22+
23+
[default.package.parameters]
24+
resolve_s3 = true
25+
26+
[default.sync.parameters]
27+
watch = true
28+
29+
[default.local_start_api.parameters]
30+
warm_containers = "EAGER"
31+
32+
[default.local_start_lambda.parameters]
33+
warm_containers = "EAGER"

template.yaml

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Transform: AWS::Serverless-2016-10-31
3+
Description: SAM Template for Go's Echo Web application.
4+
5+
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
6+
Globals:
7+
Function:
8+
Timeout: 5
9+
MemorySize: 128
10+
11+
Resources:
12+
GoEchoAppFunction:
13+
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
14+
Properties:
15+
CodeUri: app/
16+
PackageType: Image
17+
Architectures:
18+
- x86_64
19+
Environment: # More info about Env Vars: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#environment-object
20+
Variables:
21+
AWS_LWA_ASYNC_INIT: true
22+
AWS_LWA_PASS_THROUGH_PATH: '/'
23+
FunctionUrlConfig:
24+
AuthType: NONE
25+
Metadata:
26+
DockerTag: provided.al2023-v1
27+
DockerContext: .
28+
Dockerfile: Dockerfile
29+
30+
Outputs:
31+
# ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
32+
# Find out more about other implicit resources you can reference within SAM
33+
# https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
34+
GoEchoAppFunctionOutput:
35+
Description: "Go's Echo App Function ARN"
36+
Value: !GetAtt GoEchoAppFunction.Arn
37+
GoEchoAppFunctionUrlOutput:
38+
Description: "Go's Echo App Function URL"
39+
Value: !GetAtt GoEchoAppFunctionUrl.FunctionUrl

0 commit comments

Comments
 (0)