Felipe Goldin
mobicareofficial
Published in
4 min readJun 6, 2019

--

This post describes a way to receive AWS CodeBuild statuses on Google Chat using WebHooks sent SNS.

Creating the SNS Topic

The first step is to create the SNS Topic that will be used to send notification. We won't configure advanced property or subscribers yet.

Create the SNS Topic

Then copy the ARN in the next screen:

ARN of the SNS Topic

CloudWatch Event Rules

Every CodeBuild build sends events to CloudWatch. The next step is to create and CloudWatch Event Rule to send this information to our SNS Topic.

On the Event Source section, select CodeBuild as the Service Name, and Code Build State Change as the Event Type.

Edit the Event Pattern and add to following JSON:

{
“source”: [
“aws.codebuild”
],
“detail-type”: [
“CodeBuild Build State Change”
],
“detail”: {
“build-status”: [
“IN_PROGRESS”,
“SUCCEEDED”,
“FAILED”,
“STOPPED”
]
}
}

On the Target section, select SNS Topic and the the recently created Topic named CodeBuildTopic.

For the configuration input, select Input Transformer and paste the following JSONs to transformer the input (CloudWatch Event) to a friendly message that will be used in SNS:

{“build-status”:”$.detail.build-status”,”project-name”:”$.detail.project-name”,”build-id”:”$.detail.build-id”}

and

“The build ‘<build-id>’ for project ‘<project-name>’ reached status ‘<build-status>’.”

On the next screen give this Rule a name.

Finding the Google Chat WebHook URL

Go to your Google Chat application, select the Room you would like to receive the build notifications and select Configure Webhooks.

Now name the WebHook and set the avatar URL. Below is the image we are using with the CodeBuild icon:
https://img.stackshare.io/service/6086/aws-codebuild.png

Copy the WebHook URL, it will be needed in the following section.

Lambda as a SNS Subscriber

Now that the build events are reaching SNS, we need to setup a subscriber. SNS has several options that goes from e-mail messages to HTTP requests. We will use Lambda to have the flexibility to modify the message before sending is the Google Chat.

For the lambda, we will use the serverless framework to help us create and deploy our code. This post is not focused on the serverless framework, so we are not using all its capabilities like automatically creating the Lambda Trigger or setting environment variables.

The complete code is available on: https://github.com/mobilecare/codebuild-sns-chat-webhook

First we need to install the serverless framework.

npm install serverless -g

Now clone the GitHub project:

git clone git@github.com:mobilecare/codebuild-sns-chat-webhook.git

The code is very simple, it receives events from SNS as a subscriber, and then post it to Google's Chat API.

var https = require(‘https’);
var util = require(‘util’);
exports.handler = function(event, context) {
console.log(JSON.stringify(event, null, 2));
console.log(‘From SNS:’, event.Records[0].Sns.Message);
var message = event.Records[0].Sns.Message;

var postData = {
“text”: message
};
var options = {
method: ‘POST’,
hostname: ‘chat.googleapis.com’,
port: 443,
path: process.env.CHAT_API_PATH
};
var req = https.request(options, function(res) {
res.setEncoding(‘utf8’);
res.on(‘data’, function (chunk) {
context.done(null);
});
});

req.on(‘error’, function(e) {
console.log(‘problem with request: ‘ + e.message);
});
req.write(util.format(“%j”, postData));
req.end();
};

To deploy the function you can use the serverless command below or do it manually.

serverless deploy --stage prod

After that you will need to go to the newly created Lambda using the AWS Console, the set the environment variable with the Google Chat WebHook URL from the previous section. Note that only the path part of the URL is being used.

Finally we need to configure this Lambda to receive SNS notifications from our Topic. Select the SNS from the Add Triggers menu, then select the CodeBuildTopic from the ComboBox and don't forget to save.

That's it, now every build state transition done by AWS CodeBuild will send messages to your Google Chat application like the example below:

Google Chat message example

A Mobicare e a Akross combinam os Melhores Talentos, Tecnologias de Ponta, Práticas Agile e DevOps com Capacidades Operacionais avançadas para ajudar Operadoras Telecom e grandes empresas a gerarem novas receitas e a melhorarem a experiência dos seus próprios clientes.

Se você gosta de inovar, trabalhar com tecnologia de ponta e está sempre buscando conhecimento, somos um match perfeito!

Faça parte do nosso time. 😉

--

--