Closed3

Golangで始める SAM CLI

yuucuyuucu

まっさらなEC2で試してみる

  • ec2立ち上げ

いつも通り

  • pip 入れる

https://docs.aws.amazon.com/ja_jp/elasticbeanstalk/latest/dg/eb-cli3-install-linux.html

  • docker install

https://docs.aws.amazon.com/ja_jp/serverless-application-model/latest/developerguide/serverless-sam-cli-install-linux.html

  • 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
yuucuyuucu

フォルダ名を変更する

~/.../sam/test >>> tree                                                                               
.
├── README.md
├── hello-world
│   ├── Dockerfile
│   ├── go.mod
│   ├── main.go
│   └── main_test.go
└── template.yaml

1 directory, 6 files

hello-world フォルダと
template.yamlDockerContext の名前が一致していないとビルドに失敗する

      Metadata:
        DockerTag: go1.x-v1
        DockerContext: ./hello-world
        Dockerfile: Dockerfile
yuucuyuucu

module名を変更する

go.modmodule名DockerfileCMD[""] が一致していないと実行に失敗する

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にクローズされました