- 投稿日:2019-08-28T17:57:51+09:00
4STEPで作れる!Zeit Now で go サーバーレスアプリケーションを公開してみよう!
公式ページ
事前準備
Go プロジェクトの準備
デプロイするGoプロジェクトを準備しておきます。
今回は以下のようなプロジェクトを作成しました。ディレクトリ構成go-zeit-sample ├── api │ └── hello.go ├── go.mod └── now.json/api/hello.gopackage handler import ( "fmt" "net/http" ) func Handler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello Zeit Now!") }ZEITへの登録
こちらのページから登録しておきましょう。
GitHub,GitLab,メールアドレスを使用して登録が可能です。
クレジットカードの登録が不要なので気軽に試すことができます。実際にやってみる
Zeit Now CLI の インストール
npm i -g nowZeit Now の認証
now login実行するとメールアドレスの入力が求められるので、先程登録したメールアドレスを入力しましょう。
設定ファイルの作成
プロジェクト配下に以下の設定ファイルを作成します。
now.json{ "version": 2, "name": "go-zeit-sample", "public": true, "builds": [{ "src": "api/*.go", "use": "@now/go" }], "routes": [ { "src": "/", "dest": "api/hello.go" } ] }項目を簡単に補足すると以下になります。
項目 概要 version Zeit Now の バージョン name デプロイされた成果物の管理に使用される public 外部に公開するか否か builds ビルド対象の設定 builds.src ファイル名 builds.use 使用されるモジュール routes ドメイン以降のルーティング設定 routes.src 使用されるpath routes.dest 公開対象 参考 - Deployment Configuration (now.json)
デプロイ
作成した設定ファイルを指定しデプロイします
now -A ./now.json成功するとCLI上にURLが出力されるのでブラウザで確認しましょう。
Congratulations!
画面上に
"Hello Zeit Now!"が表示されていれば成功です!
これでサーバーレスアプリケーションが公開されました!ちなみに
Zeit Now では以下のこともできちゃったりします
- アプリケーションログをGUI上で確認できる
- 機密情報を格納する機能がある(Secrets)
- 独自ドメインを作成し適用できる
- GUIでダークモードが使える(重要!)
簡単なモックなどにも最適ですね!
今後はそれぞれの機能にも注目した記事もあげたいと思います。
- 投稿日:2019-08-28T15:46:41+09:00
Docker - Write File to Host Permission denied
1. Dockerfile
I created a normal user JohnnyChu to run the program in docker.
It will write a log file in /data/log inner container and container volume bind the host /foo/log. Now, it will occur permission denied problem.# build stage FROM golang:1.12.9-alpine AS build-env RUN apk update -qq && apk --no-cache add git gcc g++ COPY . /src/ ARG BUILD_ENV RUN cd /src && go build -tags ${BUILD_ENV} -o /src/dist/app # final stage FROM alpine:3.10.2 WORKDIR /app/ COPY --from=build-env /src/dist/ /app/ RUN addgroup -S JohnnyChu && adduser -S JohnnyChu -G JohnnyChu && chown -R JohnnyChu:JohnnyChu /app/ USER JohnnyChu EXPOSE 8080 ENTRYPOINT ["./app"]2. Run
# docker run -v /foo/log:/data/log ... --name boo ...2. Check User UID:GID inner Container.
# docker exec -it boo sh /app $ cat /etc/passwd ... ... guest:x:405:100:guest:/dev/null:/sbin/nologin nobody:x:65534:65534:nobody:/:/sbin/nologin JohnnyChu:x:100:101:Linux User,,,:/home/JohnnyChu:/sbin/nologin <--- UID:100 GID:1013. Chown Host's Log Folder
# chown -R 100:101 /foo/log4. Restart Container
# docker restart boohttps://stackoverflow.com/questions/29245216/write-in-shared-volumes-docker/
- 投稿日:2019-08-28T15:46:41+09:00
Docker - Permission Denied when Write Files to Host
1. Dockerfile
I created a normal user JohnnyChu to run the program in docker.
It will write a log file in /data/log inner container and container volume bind the host /foo/log. Now, it will occur permission denied problem.# build stage FROM golang:1.12.9-alpine AS build-env RUN apk update -qq && apk --no-cache add git gcc g++ COPY . /src/ ARG BUILD_ENV RUN cd /src && go build -tags ${BUILD_ENV} -o /src/dist/app # final stage FROM alpine:3.10.2 WORKDIR /app/ COPY --from=build-env /src/dist/ /app/ RUN addgroup -S JohnnyChu && adduser -S JohnnyChu -G JohnnyChu && chown -R JohnnyChu:JohnnyChu /app/ USER JohnnyChu EXPOSE 8080 ENTRYPOINT ["./app"]2. Run
# docker run -v /foo/log:/data/log ... --name boo ...2. Check User UID:GID inner Container.
# docker exec -it boo sh /app $ cat /etc/passwd ... ... guest:x:405:100:guest:/dev/null:/sbin/nologin nobody:x:65534:65534:nobody:/:/sbin/nologin JohnnyChu:x:100:101:Linux User,,,:/home/JohnnyChu:/sbin/nologin <--- UID:100 GID:1013. Chown Host's Log Folder
# chown -R 100:101 /foo/log4. Restart Container
# docker restart boohttps://stackoverflow.com/questions/29245216/write-in-shared-volumes-docker/
- 投稿日:2019-08-28T13:02:35+09:00
lambda(golang) の local 実行
概要
- docker と serverless-framework (golang) 併用時に docker.sock のマウントや、パスをホスト側と一致させないといけないのが辛い。
- serverless-framework の
sls invoke localで実行していることをホスト側で実行すればよいのでは、と思ったので試してみました。- 発端
コンテナ
Dockerfile# build stage FROM golang:1.12.7 as build-stage WORKDIR /go/src/app ARG GOOS=linux ARG GOARCH=amd64 COPY . . RUN go build -o ./bin/hello path/to/main.go # local invoke stage FROM lambci/lambda:go1.x as invoke WORKDIR /go/src/app COPY --from=build-stage /go/src/app/bin /var/task CMD ["hello"]docker-compose.ymlversion: '3.4' services: builder: build: context: . target: build-stage invoke: build: context: . target: invoke実行
$ docker-compose run --rm invoke START RequestId: c60e09aa-e9ef-1869-84e5-1a66bb8401d5 Version: $LATEST END RequestId: c60e09aa-e9ef-1869-84e5-1a66bb8401d5 REPORT RequestId: c60e09aa-e9ef-1869-84e5-1a66bb8401d5 Duration: 1.39 ms Billed Duration: 100 ms Memory Size: 1536 MB Max Memory Used: 6 MB {"statusCode":200,"headers":{"Content-Type":"application/json","X-MyCompany-Func-Reply":"hello-handler"},"multiValueHeaders":null,"body":"{\"message\":\"Go Serverless v1.0! Your function executed successfully!\"}"}補足
lambci/lambda:go1.xは、/var/task配下にバイナリを置き、バイナリ名を cmd で指定してやることで実行してくれます。sls invoke localの挙動についてはコードを見たものではなく、ホスト側に生成されるディレクトリを見て推測したものです。
- 投稿日:2019-08-28T10:22:21+09:00
go mod tidy で不要なpackageを削除する
概要
vgoでモジュール管理している際に
go getしたものの「やっぱり使わない!」から削除したいというケースだったり、「未使用のパッケージを削除したい」というケースだったりあると思います。はい。
go mod tidyでできます。go modコマンドを見てみる
$ go mod Go mod provides access to operations on modules. Note that support for modules is built into all the go commands, not just 'go mod'. For example, day-to-day adding, removing, upgrading, and downgrading of dependencies should be done using 'go get'. See 'go help modules' for an overview of module functionality. Usage: go mod <command> [arguments] The commands are: download download modules to local cache edit edit go.mod from tools or scripts graph print module requirement graph init initialize new module in current directory tidy add missing and remove unused modules vendor make vendored copy of dependencies verify verify dependencies have expected content why explain why packages or modules are needed Use "go help mod <command>" for more information about a command.
go mod tidyの説明にadd missing and remove unused modulesと書いてますね。
不要なパッケージの削除だけではなく、不足しているパッケージがあれば追加もしてくれるようです。go help mod tidyしてみる
$ go help mod tidy usage: go mod tidy [-v] Tidy makes sure go.mod matches the source code in the module. It adds any missing modules necessary to build the current module's packages and dependencies, and it removes unused modules that don't provide any relevant packages. It also adds any missing entries to go.sum and removes any unnecessary ones. The -v flag causes tidy to print information about removed modules to standard error.
-vオプションをつけて削除されたパッケージ情報を出力するのがわかりやすい。$ go mod tidy -v unused github.com/ascarter/requestid unused github.com/google/uuid