In order to proceed with the demo, you should have the following:
- AWS CLI installed
- AWS SAM installed
- Configure your AWS credentials
This repository contains a demo using AWS Application Composer and serverless technologies. We will be building an API allowing you to post messages and get notified whenever a message is identified as negative.
Our api should accept the following call:
URL :
YOUR_API_ENDPOINTPATH:
/Method :
POSTAuth :
NONEHeaders:
Content-Type: application/jsonBody :
{ "content": "I am not happy at all with your service.", "sender": "Angry Customer", "id": "as123kjsad21d032d3921031" }
In this demo we will start building an API. In order to do so, we will be leveraging AWS Application Composer.
- Create a directory in which you wish to create your project on your local computer
- Head to the Application Composer console
- Click on create project
- Click on Menu and Activate local sync
- Select the local directory you have created in point 1
- Drag and drop an API Gateway resource into the canvas
- Click on the resource, then details and call it SocialControlApi
- Change the method from GET to POST
- Click save

- Drag and drop an SQS Queue resource in the canvas
- Change the logical ID to SocialControlQueue
- Click save
- Link SocialControlQueuer with SocialControlQueue

- Drag and drop a Lambda resource into the canvas
- Change the logical ID to SocialControlHandler
- Click save
- Link Subscription of the SocialControlQueue with SocialControlHandler plot
- Click on the SocialControlHandler resource details and scroll down to Permissions
- Paste the following code
- Statement:
- Effect: Deny
Action:
- sns:Publish
Resource: arn:aws:sns:*:*:*
- Effect: Allow
Action:
- sns:Publish
Resource: '*'
- Statement:
- Effect: Allow
Action:
- comprehend:DetectSentiment
Resource: '*'These policies will:
- Allow the lambda function to send SMS through the Amazon SNS service (but not to acces any topic)
- Allow the lambda to perform the DetectSentiment command on Amazon Comprehend
Now we want to code the execution logic of our lambda functions in our local computer.
- Head to the directory you connected with Application Composer.
- Open your
template.yamlfile. This file contains all your SAM configuration and will be used to deploy resources on AWS. - Head to the end of the
template.ymldocument and paste the following code:This change will make sure that everytime you deploy your application with SAM, the API Gateway endpoint will be displayed.Outputs: MvpStoriesApi: Description: "API Gateway endpoint URL for Prod stage" Value: !Sub "https://${SocialControlApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/"
- Ιn the
src/Functiondirectory, change the extension of the theindex.jsfile toindex.mjs - Let's head to the
src/Function/index.mjsfile and paste the following code:This code extracts the message from SQS. It then makes a call to Amazon Comprehend to assess if the message is negative. If it is negative it sends a message to the phone number hard coded in this code snippet.import { SNSClient, PublishCommand } from "@aws-sdk/client-sns" import { ComprehendClient, DetectSentimentCommand } from "@aws-sdk/client-comprehend" export const handler = async (event) => { await Promise.all(event.Records.map(record => { const message = JSON.parse(record.body).data return handleMessage(message) })) } const handleMessage = async (message) => { const comprehendInput = { Text: message.content, LanguageCode: "en" } const comprehendClient = new ComprehendClient() const comprehendCommand = new DetectSentimentCommand(comprehendInput) const comprehendResponse = await comprehendClient.send(comprehendCommand) console.log(`Sentiment is ${comprehendResponse.Sentiment}`) if (comprehendResponse.Sentiment === "NEGATIVE") { const snsInput = { Message: `ALERT: Angry customer message received from ${message.sender} (ID: ${message.id})`, PhoneNumber: "YOUR_PHONE_NUMBER" } const snsClient = new SNSClient() const snsCommand = new PublishCommand(snsInput) await snsClient.send(snsCommand) console.log("SMS Sent") } }
NOTE: For testing purposes, you need to add your phone number to the SNS service before being able to send SMS messages to your phone number.
Now that we have all the configuration of our application ready, let's deploy it.
- At the root of your project run
sam buildthis will build your deployment files into a directory called.aws-sam - Run
sam deploy --guidedThis command will ask for some information regarding your deployment, you can fill the values in as follows:
- At the end of your deployment results, you should be able to find the outputs that should look like this:

- Copy the API endpoint and test your api with the following command
curl --location 'API_ENDPOINT' \
--header 'Content-Type: application/json' \
--data '{
"content": "Not happy at all with the service",
"sender": "Angry sender",
"id": "Angry ID"
}'- Copy the API endpoint and test your api with the following command
curl --location 'API_ENDPOINT' \
--header 'Content-Type: application/json' \
--data '{
"content": "Very happy with the service",
"sender": "Happy sender",
"id": "Happy ID"
}'In this demo, we have built a basic API that covers our requirements. This is kept very basic for the sake of this demo. Many improvements can be added to this example such as:
- Implementing API keys for API Gateway
- Storing the messages in a DynamoDB table for long term persistance
- ...

