建立可部署到Lambda的Go應用程式的ECR image。
範例環境
- macOS Ventura 13.0.1
- Go 1.19
事前要求
參考「AWS 建立IAM管理使用者及credentials」設定供應用程式存取AWS需要的credentials。
參考「Mac Homebrew 安裝 AWS CLI」安裝AWS CLI。
參考「Golang 建立AWS Lambda API client」建立Lambda API client。
參考「AWS ECR 建立private repository」建立ECR private repositroy。
下載AWS SDK Go V2 modules
在專案根目錄執行以下命令下載需要的aws-sdk-go-v2 modules。
go get github.com/aws/aws-lambda-go/lambda
go get github.com/aws/aws-lambda-go/events
建立Go Lambda function
建立一個Go應用程式如下。此應用程式為要部署到Lambda function執行的程式。
main.go
package main
import (
"context"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)
func main() {
lambda.Start(handler)
}
func handler(ctx context.Context, event events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
response := events.APIGatewayProxyResponse{
StatusCode: 200,
Body: "\"Hello from Lambda!\"",
}
return response, nil
}
建立Dockerfile
在任意位置,例如專案根目錄下,建立一個Dockerfile內容如下。
Dockerfile
FROM golang:1.19 as build
WORKDIR /app
# Copy dependencies list
COPY go.mod go.sum ./
# Build with optional lambda.norpc tag
COPY main.go .
RUN go build -tags lambda.norpc -o main main.go
# Copy artifacts to a clean image
FROM public.ecr.aws/lambda/provided:al2
COPY --from=build /app/main ./main
ENTRYPOINT [ "./main" ]
建立Docekr Image
在Dockerfile所在目錄執行命令docker build --platform linux/amd64 -t go-image:test .
建立image。注意這邊要部署的Lambda會是Linux x86_64平台。這邊建立的image名稱為go-image
,tag為test
。
~% docker build --platform linux/amd64 -t go-image:test .
[+] Building 27.6s (13/13) FINISHED docker:desktop-linux
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 359B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for public.ecr.aws/lambda/provided:al2 4.4s
=> [internal] load metadata for docker.io/library/golang:1.19 2.9s
=> [build 1/5] FROM docker.io/library/golang:1.19@sha256:3025bf670b8363ec9f1b4c4f27348e6d9b7fec607c47e401e40df816853e743a 17.7s
=> => resolve docker.io/library/golang:1.19@sha256:3025bf670b8363ec9f1b4c4f27348e6d9b7fec607c47e401e40df816853e743a 0.0s
...
=> [build 2/5] WORKDIR /app 0.3s
=> [build 3/5] COPY go.mod go.sum ./ 0.0s
=> [build 4/5] COPY main.go . 0.0s
=> [build 5/5] RUN go build -tags lambda.norpc -o main main.go 5.0s
=> [stage-1 2/2] COPY --from=build /app/main ./main 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:c2e790aaaf6ce7a4c8e1407c5846d2fa5e72bc8932ebbb13c1560821189bb555 0.0s
=> => naming to docker.io/library/go-image:test
輸入docker images
檢視建立的image如下。
~% docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
go-image test c2e790aaaf6c 2 minutes ago 317MB
部署image到ECR repository
例如要部署的ECR repository的URI為123456789012.dkr.ecr.ap-northeast-1.amazonaws.com/ecr-1
。
Docker登入ECR private repository,輸入aws ecr get-login-password --region ap-northeast-1 | docker login --username AWS --password-stdin 123456789012.dkr.ecr.ap-northeast-1.amazonaws.com
。123456789012
替換成你的AWS帳戶號碼,ap-northeast-1
替換為ECR repository所在的region。
~% aws ecr get-login-password --region ap-northeast-1 | docker login --username AWS --password-stdin 123456789012.dkr.ecr.ap-northeast-1.amazonaws.com
Login Succeeded
輸入docker tag go-image:test 123456789012.dkr.ecr.ap-northeast-1.amazonaws.com/ecr-1:latest
將go-image:test
標記為123456789012.dkr.ecr.ap-northeast-1.amazonaws.com/ecr-1:latest
。
~% docker tag go-image:test 123456789012.dkr.ecr.ap-northeast-1.amazonaws.com/ecr-1:latest
輸入docker images
檢視標記的image如下。
~% docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
123456789012.dkr.ecr.ap-northeast-1.amazonaws.com/ecr-1 latest c2e790aaaf6c 2 hours ago 317MB
go-image test c2e790aaaf6c 2 hours ago 317MB
輸入docker push 123456789012.dkr.ecr.ap-northeast-1.amazonaws.com/ecr-1:latest
將123456789012.dkr.ecr.ap-northeast-1.amazonaws.com/ecr-1:latest
標記的image推送到ECR repository。
~% docker push 123456789012.dkr.ecr.ap-northeast-1.amazonaws.com/ecr-1:latest
The push refers to repository [123456789012.dkr.ecr.ap-northeast-1.amazonaws.com/ecr-1]
ebf1eb170624: Pushed
15dd6c63f3a2: Pushed
71baee2454b0: Pushed
bd9069c4d5c6: Pushed
02c24900bf38: Pushed
latest: digest: sha256:94abcebf0555af6fe8f3d43910537112facc38a775f69f1a39c3d8ff4cc882f3 size: 1368
測試
在AWS console,前往ECR repository檢視上傳的image。
沒有留言:
張貼留言