前提条件

  • AWSアカウント保有
  • AWS CLIインストール済み(OS X 10.11 El Capitan)
  • Slackアカウント保有

SlackのWebhook設定確認

  1. https://[slack名].slack.com/services/new にアクセス
  2. 検索窓より[Slash Commands]を検索
  3. "Slash Commands"をインストール
  4. "Choose a Command"にコマンド名を入力(今回は[/sayhello]と入力)
  5. 設定画面遷移後に"Outgoing Data"の"token=<token name>"のToken名をコピー

Tokenの暗号化

  • 暗号化用のIAMユーザー作成
  1. IAMユーザー名:lambdaSlackTest
  2. IAMポリシー: ※今回はKMSによる暗号化のみ許可
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "kms:Encrypt"
            ],
            "Resource": "*"
        }
    ]
}
  • アクセスキーID、シークレットアクセスキー発行
  • 暗号化キーの作成
  1. AWSマネジメントコンソール >> IAM >> 暗号化キー よりキーの作成

    リージョン: アジアパシフィック(東京)
    エイリアス:lambdaSlackTest

  2. "キー管理者"に作成したIAMユーザー"lambdaSlackTest"を追加
  • AWS CLIの認証情報設定
$ aws configure
AWS Access Key ID [None]: xxxxxxxxxx
AWS Secret Access Key [None]: xxxxxxxxxx
Default region name [None]: ap-northeast-1
Default output format [None]: json
  • Slack認証用のToken暗号化
$ aws kms encrypt --key-id alias/lambdaSlackTest --plaintext "<token name>"
{
    "KeyId": "arn:aws:kms:ap-northeast-1:xxxxxxxxxxxx:key/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "CiphertextBlob": "<encrypted token>"
}

Lambda Function作成

  • "Select blueprint"より"slack-echo-command-python"を選択

    Name: Slack-echo-command-python
    Runtime: Python 2.7
    Role: lambda-slack-integration-test ※新規作成

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "kms:Decrypt"
            ],
            "Resource": [
                "arn:aws:kms:ap-northeast-1:xxxxxxxxxxxx:key/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
            ]
        }
    ]
}
  • "Advanced settings"は初期値
  • "Next"をクリック
  • "Create function"をクリック

IAM Roleの追加

  • Cloudwatch LogsへのアクセスとPUT権限を付与したIAM RoleをLambda functionに追加

    ポリシー名:putlogs
    ポリシー:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "logs:CreateLogGroup",
                "logs:CreateLogStream",
                "logs:PutLogEvents"
            ],
            "Resource": "arn:aws:logs:*:*:*"
        }
    ]
}
  • Lambda functionのCode編集画面より"ENCRYPTED_EXPECTED_TOKEN"という変数にKMSで暗号化したTokenを代入
  • "Test"をクリックし下記のコードを入力(<token name>はSlackのtokenを入力)し"Save and test"をクリック
{
"body":
"token=<token name>&team_id=T0001&team_domain=example&channel_id=C2147483705&channel_name=test&user_id=U2147483697&user_name=Steve&command=/weather&text=94070&response_url=https://hooks.slack.com/commands/1234/5678"
}
  • "Execution result"が"succeeded"となり下記が出力されればOK
"Steve invoked /weather in test with the following text: 94070"

API Endpoint追加

  1. Lambda Functions >> "slack-echo-command-python" >> "API Endpoints"より"Add API endpoint"をクリック

    API endpoint type: API Gateway
    API name: LambdaPythonSlackintegrationTest
    Resource name: /Slack-echo-command-python
    Method: POST
    Deployment stage: prod
    Security: Open

  2. "Submit"をクリック

API Gateway設定

  1. /Slack-echo-command-python >> "統合リクエスト"をクリック
  2. "マッピングテンプレートの追加"をクリック

    Content-Type: application/x-www-form-urlencoded
    Template: { "body": $input.json("$") }

  3. "保存"をクリック
  4. "アクション" >> "APIのデプロイ" >> デプロイされるステージ:prod でデプロイ

SlackのWebhook設定

  • Integration Settings >> URL にAPI Endpointを入力
https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/prod/Slack-echo-command-python
  • Method: POST
  • Slackアプリより"slackbot"に下記入力
/sayhello test
  • slackbotが下記を返したら成功
"<user> invoked /sayhello in directmessage with the following text: test"

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2016-07-03 (日) 21:54:42 (2852d)