** 前提条件 [#oaa7c38c] - AWSアカウント保有 - AWS CLIインストール済み(OS X 10.11 El Capitan) - Slackアカウント保有 ** SlackのWebhook設定確認 [#wd191ba3] + https://[slack名].slack.com/services/new にアクセス + 検索窓より[Slash Commands]を検索 + "Slash Commands"をインストール + "Choose a Command"にコマンド名を入力(今回は[/sayhello]と入力) + 設定画面遷移後に"Outgoing Data"の"token=<token name>"のToken名をコピー ** Tokenの暗号化 [#if4f86f0] - 暗号化用のIAMユーザー作成 + IAMユーザー名:lambdaSlackTest + IAMポリシー: ※今回はKMSによる暗号化のみ許可 { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "kms:Encrypt" ], "Resource": "*" } ] } - アクセスキーID、シークレットアクセスキー発行 - 暗号化キーの作成 + AWSマネジメントコンソール >> IAM >> 暗号化キー よりキーの作成 ~リージョン: アジアパシフィック(東京)~ エイリアス:lambdaSlackTest + "キー管理者"に作成した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作成 [#p90faecf] - "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の追加 [#i0b45363] - 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追加 [#lb037923] + 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 + "Submit"をクリック ** API Gateway設定 [#i3b55738] + /Slack-echo-command-python >> "統合リクエスト"をクリック + "マッピングテンプレートの追加"をクリック ~Content-Type: application/x-www-form-urlencoded~ Template: { "body": $input.json("$") } + "保存"をクリック + "アクション" >> "APIのデプロイ" >> デプロイされるステージ:prod でデプロイ ** SlackのWebhook設定 [#ab1fd176] - Integration Settings >> URL にAPI Endpointを入力 https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/prod/Slack-echo-command-python - Method: POST - Slackの"slackbot"に下記入力 - Slackアプリより"slackbot"に下記入力 /sayhello test - slackbotが下記を返したら成功 "<user> invoked /sayhello in directmessage with the following text: test"