こんばんは、ボタ餅です。
先週はLambdaでのイベントの受け取り方を書いたので、
今週は続きを書きたいと思います。
SNS(aws)から来た情報でメッセージを成形しなければならないので、
JSONの中を見つつ取り出していきました。
node.jsはjavaやった人なら結構すぐ慣れると思います。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exports.handler = function(event, context) { | |
var hoge = event.event.detail.pullRequestId; | |
} |
こんな感じで受け取ったイベントのJSONの中身を取り出す事が出来ます。
varは変数の型?でStringもintもなんでも入ります。
JSONの中の変数名(表現あってる?)を知るために
まずはそのままコンソールに出力させて確認しないといけない。
特定のイベントから送られてくる中身の型は同じだけど、見なきゃ名前分からんので。
JSONの中にJSONが入っていて、どうすんの?ってなることもありました。
CodecommitのイベントがSNSに送られて、その後そのままLambdaに送られているからみたいです。
調べたら文字列をJSONにparse出来るみたいで、
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Messageの中身もJSONなので一度JSONにしてから | |
//情報取り出す | |
const message = event.Records[0].Sns.Message; | |
var posted_message = JSON.parse(message); | |
console.log(message); | |
//ユーザー名だけを切り取る | |
user_name = cut_user_arn(posted_message.Records[0].userIdentityARN); | |
//PUSH先のbranchを取得 | |
var ref = posted_message.Records[0].codecommit.references[0].ref; | |
var branch = ref.split('/'); |
みたいな感じでメッセージに必要な情報だけを取り出したりしてました。
最近、皆配置換えとか常駐先に移動とかでバラバラになって来てしまったので、
こういうの情報共有って改めて大事かなって思いました。
ワイヤーフレームとか設計書とか書いてたりもするんですが、
そういうのも今後書こうかなと思います。
author ボタ餅