DEV Community

Cover image for Enable Slack Notifications for AWS Amplify Deployments
Mubbashir Mustafa
Mubbashir Mustafa

Posted on • Updated on

Enable Slack Notifications for AWS Amplify Deployments

AWS Amplify is a set of purpose-built tools and features that lets front-end web and mobile developers quickly and easily build full-stack applications on AWS, with the flexibility to leverage the breadth of AWS services as your use cases evolve.

At our company, we use AWS Amplify for hosting our frontend (React) apps. Although it's an amazing service and we love it, it lacks one important feature: integration with AWS Chat Bot (for sending pipeline notification to slack). It however allows you to send notifications to email addresses (which is not ideal, at least for us).

But there's a robust workaround for this problem, let me share that with you.


From AWS web console, go to AWS Amplify
AWS Amplify from AWS Console

Select the app against which you want to enable slack notifications
AWS Amplify Hosting App

Select "Notifications" from the left sidebar
AWS Amplify App Details

Enter an email address that you use and then click save
AWS Amplify Email Notifications


Now you need to create a lambda function that will receive messages from the AWS SNS topic (created by AWS Amplify) and forward that to slack

From AWS Console, go to AWS Lambda
AWS Lambda Link

Select "Create function"
Create AWS Lambda Function

Give a name to your function, choose runtime as node 14.x, and click create
Create AWS Lambda Function

Add the following code to your function

// deps in lambda are added as layers
const fetch = require('node-fetch')

// this should be your webhook URL (doc: https://api.slack.com/messaging/webhooks)
const integrationURL = ''

exports.handler = async (event) => {

  const message = event.Records[0].Sns.Message

  return await fetch(integrationURL, {
      method: 'POST',
      body: JSON.stringify({
        attachments: [{
          title: `AWS Amplifyy Notification!`,
          text: message,
        }]
      }),
      headers: { 'Content-Type': 'application/json' },
    })
    .then((data) => console.log('sent!'))
    .catch((e) => console.error(e.response.data))
}
Enter fullscreen mode Exit fullscreen mode

Or grab it from github gist

The code uses the node-fetch package to make HTTP post request, you will need to add a dependency layer to your lambda function for it to work

To get integration URL you will need to create the slack app and enable incoming webhooks, check out the official tutorial.

Save your changes and click the "Deploy" button
AWS Lambda Code Deploy

Next, add a trigger to your AWS Lambda function
Add AWS Lambda Trigger

Select "SNS" as the trigger, and then select the SNS Topic that would be created by AWS Amplify (when you added email earlier to enable notifications)
Image description


To test things out, go back to AWS Amplify and from being within the project, click "Redeploy this version"
AWS Amplify Deployment

You should receive a notification in your slack
AWS Slack Notification from AWS Amplify


The email you added earlier is no longer needed (we only did that to expose SNS Topic) and you could remove it by visiting AWS Amplify->notifications.


Let's connect:

Linkedin: https://www.linkedin.com/in/mubbashir10/

Twitter: https://twitter.com/mubbashir100

Top comments (3)

Collapse
 
mohamad_el_bohsaly profile image
Mohamad El Bohsaly • Edited

Based on what are you creating SNS? Shouldn't you create a web-hook trigger within the build settings of Amplify?

Collapse
 
fachreza73 profile image
Fachreza Muslim

Do you know how to get the custom domain?

Collapse
 
mubbashir10 profile image
Mubbashir Mustafa

Custom domain for AWS Ampliffy? You could do that from domain management (It will setup SSL and provide you with a CNAME record pointing to a cloudfrotn distribution)