LoginSignup
11
6

More than 5 years have passed since last update.

Lambda + Apex + Goでslackにメッセージを飛ばす

Last updated at Posted at 2017-02-01

概要

  • lambda + apex + GoでSlackのIncomming Webhooksを使ってメッセージを飛ばします

内容

①IAMで"lambda_apex_go"グループを作る

インラインポリシーは下記になります


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

②自分のIAMユーザに"lambda_apex_go"グループアタッチする

  • アタッチしてください

③ここからローカル作業。apexをインストールし、作業ディレクトリを掘ります。

### apexインストール
$ curl https://raw.githubusercontent.com/apex/apex/master/install.sh | sh
$ mkdir apex-sample-go
$ cd apex-sample-go

### ~/.aws/configは。。設定してると思うからスルーするね!

### init
$ go get github.com/apex/go-apex
$ 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

  [+] 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

### 確認
$ tree
.
├── functions
│   └── hello
│       └── index.js
└── project.json

④通知用プログラムを書く

  • 最終的な構成はこうなります。
  • 最初に出来た、index.jsは削除してください
$ tree
.
├── functions
│   └── hello
│       ├── main.go
│       └── run.go
└── project.json

main.go

package main

import (
    "encoding/json"

    "github.com/apex/go-apex"
)

type message struct {
    Hello string `json:"hello"`
}

func main() {
    apex.HandleFunc(func(event json.RawMessage, ctx *apex.Context) (interface{}, error) {
        Run()

        return nil, nil
    })
}

run.go  (チャンネル名を編集してください)

package main

import (
    "encoding/json"
    "net/http"
    "net/url"
)

type Slack struct {
    Text      string `json:"text"`
    Username  string `json:"username"`
    IconEmoji string `json:"icon_emoji"`
    IconURL   string `json:"icon_url"`
    Channel   string `json:"channel"`
}

func LoadSlackParam() *Slack {
    return &Slack{
        Text:      "testだよ!",
        Username:  "test",
        IconEmoji: ":bow:",
        IconURL:   "",
        Channel:   "チャンネル名書いてね",
    }
}

func Run()  {
    URL := "https://hooks.slack.com/services/xxxxxx/xxxxx/xxxxxxxxxxxxxxxxx"
    params, _ := json.Marshal(LoadSlackParam())

    resp, _ := http.PostForm(
        URL,
        url.Values{"payload": {string(params)}},
    )

    defer resp.Body.Close()
}

⑤deployする

$ apex deploy
# deployが終わった、lambdaでapex-sample-golang_hello関数ができてるはずです

⑥実行してみる

### apexで掘ったディレクトリ内であれば
$ apex invoke hello

### aws cli経由であれば
$ aws lambda invoke --function-name apex-sample-golang_hello --invocation-type Event outputfile.txt
  • こんな感じで投稿されるかと思います

スクリーンショット 2017-02-01 17.06.56.png

⑦削除するときは?

### apexから削除
$ apex delete
yes
# ウェブコンソールから削除しても良いです

### コンソール上でIAMから下記を削除
使ったPolicy
使ったrole
11
6
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
11
6