Enable Slack Notifications for AWS Amplify Deployments
Tags: aws, javascript, slack, serverless
Published: 2022-03-20 08:09:39

---
series: AWS Amplify
---
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ktekmi2vr3rxsbfyfaf.png)
 
Select the app against which you want to enable slack notifications
![AWS Amplify Hosting App](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oef3737m9hpz10kdt76v.png)
 
Select "Notifications" from the left sidebar
![AWS Amplify App Details](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xh699zn0sisqn7qf308i.png)
 
Enter an email address that you use and then click save
![AWS Amplify Email Notifications](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yl5i0wes65d2rrt410it.png)
 
---
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ua047li3tp0iccriekt.png)

Select "Create function"
![Create AWS Lambda Function](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9y8g9dekmhyyjh3j5kjx.png)

Give a name to your function, choose runtime as node 14.x, and click create
![Create AWS Lambda Function](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ix7q01280bnqyk0ukkb7.png)

Add the following code to your function
```Javascript
// 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))
}
```
Or grab it from [github gist](https://gist.github.com/mubbashir10/d759d48b8c8de1d488672796dcef40d4)

*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](https://api.slack.com/messaging/webhooks).*

Save your changes and click the "Deploy" button
![AWS Lambda Code Deploy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a07qafp8c4nj9yys8xoy.png)
 
Next, add a trigger to your AWS Lambda function
![Add AWS Lambda Trigger](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fwtfxst29n1p31zsihhs.png)

 
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7xox6nfs04oftgkahtdu.png)
  
---
To test things out, go back to AWS Amplify and from being within the project, click "Redeploy this version"
![AWS Amplify Deployment](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5qaxxmkzrunk33mdvcqn.png)

You should receive a notification in your slack
![AWS Slack Notification from AWS Amplify](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b3g7bglfnchf50kgz5ia.png)
 
--- 
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/](https://www.linkedin.com/in/mubbashir10/) 

Twitter: [https://twitter.com/mubbashir100](https://twitter.com/mubbashir100)