LoginSignup
32
21

More than 5 years have passed since last update.

Apexを使ってGoでlambdaを動かす

Last updated at Posted at 2016-06-19

最近Goの勉強をしているので、lambdaもGoで動かせないかなと思ってやってみた。

準備するもの

Go
Apexのインストール
AWSのアカウント
awscli

Apexとは


lambdaのビルドとデプロイなどを簡単に管理をするツール。
サポートしている言語はNode.js、Java、Python、Go
公式にはサポートしてないGoが使えるのはいいですね。

どうやってるのかというと、Node.jsでGoのバイナリを実行してるっぽい。

インストール

Mac もしくは Linuxの場合

% curl https://raw.githubusercontent.com/apex/apex/master/install.sh | sh

Windowsは直接バイナリをインストール

インストールが終わったらはApexのアップグレードをします。

% apex upgrade

AWSのAPIキーの設定

aws configureでAPIキーを設定します。

% aws configure

AWS Access Key ID [None]: アクセスキーID
AWS Secret Access Key [None]: アクセスキー
Default region name [None]: ap-northeast-1 //東京リージョン
Default output format [None]:ENTER

今回作ったアカウント用のIAMポリシー

テスト用なのでApex用のポリシーを適応しました。
これで最低限動きます。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "iam:Create*",
                "iam:Attach*",
                "lambda:*"
            ],
            "Resource": "*"
        }
    ]
}

プロジェクト作成

適当なディレクトリにプロジェクトフォルダを作ります。

% mkdir apex-sample-go
% cd apex-sample-go

init

プロジェクトの雛形を作ります。

% apex init


             _    ____  _______  __
            / \  |  _ \| ____\ \/ /
           / _ \ | |_) |  _|  \  /
          / ___ \|  __/| |___ /  \
         /_/   \_\_|   |_____/_/\_\



  Enter the name of your project. It should be machine-friendly, as this
  is used to prefix your functions in Lambda.

    Project name: apex-sample-golang //プロジェクト名の入力

  Enter an optional description of your project.

    Project description: apex-samle-golang is Hello world App by Golang //プロジェクトの説明

  [+] creating IAM apex-sample-golang_lambda_function role
  [+] creating IAM apex-sample-golang_lambda_logs policy
  [+] attaching policy to lambda_function role.
  [+] creating ./project.json
  [+] creating ./functions

  Setup complete, deploy those functions!

    $ apex deploy

lambdaの作成

公式にGoのサンプルがあるのでそれを写経。functions/hello/main.goに保存し、自動作成されているfunctions/hello/index.js(node.jsのhello world)は削除します。ファイル構成は以下の形になります。

apex-sample-go/
├── event.json (サンプルから)
├── functions/
│   └── hello/
│       └── main.go (サンプルから)
└── project.json

できたらapex deployをします。

% apex deploy
   • creating function         function=hello
   • created alias current     function=hello version=1
   • function created          function=hello name=apex-samle-golang_hello version=1

なお失敗してもエラーメッセージが出るので安心

% apex deploy
   ⨯ Error: function hello: build hook: can't load package: package main:
main.go:1:1: expected 'package', found 'EOF'

lambda実行

apex invoke 関数名でlambdaを実行します。

% apex invoke hello < event.json
{"hello":"world"}

上記のように返ってきたら成功です。

マネジメントコンソールで確認

AWSのマネジメントコンソールからlambdaを開いてみると

Apex01.JPG

ちゃんと作られているのが確認できました。

ちなみにCode Sizeは 875.4KBでした。
apex-goパッケージを使ってるから他の言語より大きいのかな。

最後に

サンプルで試しただけでしたが、とても簡単にGoでlambdaを作ることが出来ました。
apex-goは他にも
* CloudWatch Logs
* Cognito
* Kinesis
* Dynamo
* S3
* SNS
* SES

サポートしているみたいなのでいろんなアプリを作ることが出来そうです。
でも一番は公式対応してくれるといいなと思います。

参考

Apexの公式ドキュメント

32
21
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
32
21