Closed3
Golangで始める SAM CLI
まっさらなEC2で試してみる
- ec2立ち上げ
いつも通り
- pip 入れる
- docker install
- awscli, samcli instal
pip install awscli
pip install aws-sam-cli
- sam init
[ec2-user@ip-xxxxx]$ sam init
Which template source would you like to use?
1 - AWS Quick Start Templates
2 - Custom Template Location
Choice: 1
What package type would you like to use?
1 - Zip (artifact is a zip uploaded to S3)
2 - Image (artifact is an image uploaded to an ECR image repository)
Package type: 2
Which base image would you like to use?
1 - amazon/nodejs12.x-base
2 - amazon/nodejs10.x-base
3 - amazon/python3.8-base
4 - amazon/python3.7-base
5 - amazon/python3.6-base
6 - amazon/python2.7-base
7 - amazon/ruby2.7-base
8 - amazon/ruby2.5-base
9 - amazon/go1.x-base
10 - amazon/java11-base
11 - amazon/java8.al2-base
12 - amazon/java8-base
13 - amazon/dotnetcore3.1-base
14 - amazon/dotnetcore2.1-base
Base image: 9
Project name [sam-app]: test
Cloning app templates from https://github.com/aws/aws-sam-cli-app-templates
-----------------------
Generating application:
-----------------------
Name: test
Base Image: amazon/go1.x-base
Dependency Manager: mod
Output Directory: .
Next steps can be found in the README file at ./test/README.md
imageを選択
- sam build
sam build
- lambda check
sam local invoke
- 立ち上げ
sam local start-api &
- check
curl localhost:3000/hello
フォルダ名を変更する
~/.../sam/test >>> tree
.
├── README.md
├── hello-world
│ ├── Dockerfile
│ ├── go.mod
│ ├── main.go
│ └── main_test.go
└── template.yaml
1 directory, 6 files
hello-world
フォルダと
template.yaml
の DockerContext
の名前が一致していないとビルドに失敗する
Metadata:
DockerTag: go1.x-v1
DockerContext: ./hello-world
Dockerfile: Dockerfile
module名を変更する
go.mod
の module名
と Dockerfile
の CMD[""]
が一致していないと実行に失敗する
require github.com/aws/aws-lambda-go v1.13.3
module hello-world
FROM golang:1.14 as build-image
WORKDIR /go/src
COPY go.mod main.go ./
RUN go build -o ../bin
FROM public.ecr.aws/lambda/go:1
COPY --from=build-image /go/bin/ /var/task/
# Command can be overwritten by providing a different command in the template directly.
CMD ["hello-world"]
このスクラップは2023/09/10にクローズされました