Serverless FrameworkでGoを使いつつCircle CIでデプロイしてみる

Posted on
Circle CI Serverless Golang

はじめに

AWS LambdaでGolangが使えるようになったなあと思っていたら早速Serverless FrameworkでもGoが使えるようになっていた。
下記の公式ブログにサンプルがあるので、そのまま試してみた。
Serverless Framework example for Golang and Lambda

やってみた

以下、ただの作業ログ。

サンプルをそのままデプロイしてみる

$ serverless create -t aws-go-dep -p serverless-example-golang
Serverless: Generating boilerplate...
Serverless: Generating boilerplate in "/Users/*****/go/src/github.com/shunsugai/serverless-example-golang"
 _______                             __
|   _   .-----.----.--.--.-----.----|  .-----.-----.-----.
|   |___|  -__|   _|  |  |  -__|   _|  |  -__|__ --|__ --|
|____   |_____|__|  \___/|_____|__| |__|_____|_____|_____|
|   |   |             The Serverless Application Framework
|       |                           serverless.com, v1.26.0
 -------'

Serverless: Successfully generated boilerplate for template: "aws-go-dep"
$ cd serverless-example-golang/
$ ll
total 40
drwxr-xr-x  9 *****  staff   306 Feb  3 00:02 .
drwxr-xr-x  3 *****  staff   102 Feb  3 00:02 ..
-rw-r--r--  1 *****  staff   123 Feb  3 00:02 .gitignore
-rw-r--r--  1 *****  staff   492 Feb  3 00:02 Gopkg.lock
-rw-r--r--  1 *****  staff   611 Feb  3 00:02 Gopkg.toml
-rw-r--r--  1 *****  staff   156 Feb  3 00:02 Makefile
drwxr-xr-x  3 *****  staff   102 Feb  3 00:02 hello
-rw-r--r--  1 *****  staff  2749 Feb  3 00:02 serverless.yml
drwxr-xr-x  3 *****  staff   102 Feb  3 00:02 world
$ make
dep ensure
env GOOS=linux go build -ldflags="-s -w" -o bin/hello hello/main.go
env GOOS=linux go build -ldflags="-s -w" -o bin/world world/main.go
$ sls deploy
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Creating Stack...
Serverless: Checking Stack create progress...
.....
Serverless: Stack create finished...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service .zip file to S3 (4.43 MB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
........................
Serverless: Stack update finished...
Service Information
service: serverless-example-golang
stage: dev
region: us-east-1
stack: serverless-example-golang-dev
api keys:
  None
endpoints:
  None
functions:
  hello: serverless-example-golang-dev-hello
  world: serverless-example-golang-dev-world
Serverless: Publish service to Serverless Platform...
Service successfully published! Your service details are available at:
https://platform.serverless.com/services/************/serverless-example-golang
$ sls  invoke -f hello
{
    "message": "Go Serverless v1.0! Your function executed successfully!"
}

Serverless Frameworkは相変わらず簡単で良い。

Circle CI 2.0でデプロイできるようにしてみる

どうせならCircle CIからデプロイできるようにしたいなあと思って、雑にやってみた。
手順としては

  • IAMでデプロイ用のユーザーを作成する
  • 作成したユーザーにデプロイに必要な権限を付与する
  • config.ymlを書く

という感じ。

ユーザー作成から権限付与まで

下記の記事を参考にした。

config.ymlの作成

作ってみた.circleci/config.ymlが下記。

version: 2
jobs:
  build:
    docker:
      - image: circleci/golang:latest
    working_directory: /go/src/serverless-example-golang
    steps:
      - checkout
      - run:
          name: Install node
          command: |
            curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
            sudo apt-get install -y nodejs            
      - run:
          name: Install awscli
          command: |
                        sudo apt-get update && sudo apt-get install -y awscli
      - run:
          name: Install serverless
          command: |
                        sudo npm install -g serverless
      - run:
          name: Install dep
          command: |
                        go get github.com/golang/dep/cmd/dep
      - run:
          name: Make
          command: |
                        make
      - run:
          name: Run serverless deploy
          command: |
                        sls deploy --stage dev
  • デプロイ用のimageを作っても良いのだけど、ひとまずデプロイできればいいやという感じでやった。
  • GOPATHが/goになっていたので、working_directoryを/go/src/serverless-example-golangにした。お作法的に良いのかどうかはわからない。
  • Serverlessのドキュメントにも書いてあるとおり、depが必要なのでインストールする。
  • ブランチによってstageをわけられるようにしたい。

結果

$ sls invoke -f hello
{
    "message": "Go Serverless v1.0! Your function executed successfully!!!!!"
}

おわりに

  • 既存のプロジェクトをGolangで書き直していきたい。
  • IAMがよくわからん。