gitkadoの気まぐれ日記

島根在住エンジニアが何かに興味を持ったらブログを更新します

aws sam initを試してみた(Go)

はじめに

gitkado.hatenadiary.jp

Goに興味が湧いたので、Goで試してみました。
実行環境はAWSのCloud9です。

開発環境準備

Cloud9使います!

Go

以下の手順にそってGoの環境を構築する。

gitkado.hatenadiary.jp

sam initを試す (Go)

$ sam init --runtime go1.x --name hello-sam
Which template source would you like to use?
        1 - AWS Quick Start Templates
        2 - Custom Template Location
Choice: 1

Quick start templates may have been updated. Do you want to re-download the latest [Y/n]: Y

# 以下のファイルが生成される
└── hello-sam
    ├── hello-world
    │   ├── main.go # lambda function
    │   └── main_test.go # unit test
    ├── Makefile 
    ├── README.md
    └── template.yaml

$ cd hello-sam

# go.mod作成
$ go mod init sample

# install
$ go get github.com/aws/aws-sdk-go

# test
$ go test ./...
ok      hello-sam/hello-world   (0.852s)

# build
$ GOOS=linux GOARCH=amd64 go build -o hello-world/hello-world ./hello-world

# Lambda実行(HelloWorldFuntion)
$ sam local invoke HelloWorldFunction

# APIサーバ起動
$ sam local start-api
$ curl 127.0.0.1:3000/hello

まとめ

Shimane.goもあるから、この調子でどんどんGo(&SAM)やってくぞ!
手っ取り早く取り組めて素晴らしい。

参考サイト

Hello SAM: AWS Golang Quickstart
Goの開発環境にAWS Cloud9をつこうてみる
GOPATH は適当に決めて問題ない