AWS Messaging & Targeting Blog
Incident notification mechanism using Amazon Pinpoint two-way SMS
Unexpected situations that require immediate attention can occur in any industry. Part of resolving these incidents is the notifications’ delivery. For example, utility companies that have installed gas sensors need to notify immediately the available engineer if a leak occurs.
The goal of an incident management process is to restore a normal service operation as quickly as possible and to minimize the impact on business operations, thus ensuring that the best possible levels of service quality and availability are maintained. A key element of incident management is sending timely notifications to the assigned or available resource(s) who can rectify the issue.
An incident can take place at any time and the resource(s) assigned to it might not have internet access and even if they receive the message they might not be equipped to work on it. This creates five key requirements for an incident notifications mechanism:
- Notify the resources via a communication channel that ensures message delivery even without internet access
- Enable assigned resources to respond to a request via a communication channel that doesn’t require internet access
- Send reminder(s) in case there is no response from the assigned resource(s)
- Escalate to another resource in case the first one doesn’t reply or declines the incident
- Store the incident details & status for reporting and data analysis
In this blog post, I share a solution on how you can automate the delivery of incident notifications. This solution utilizes Amazon Pinpoint SMS channel to contact the designated resources who might not have access to the internet. Furthermore, the recipient of the SMS is able to reply with an acknowledgement. AWS Step Functions orchestrates the user journey using AWS Lambda functions to evaluate the recipients’ response and trigger the next best action. You will use AWS CloudFormation to deploy this solution.
Use Cases
An incident notification mechanism can vary depending the organization’s requirements and 3rd party system integrations. In this blog the solution covers all five points listed above but it might require further modifications depending your use case.
With minor modifications this solution can also be used in the following use cases:
- Medicine intake notification: It will notify the patient via SMS that it is their time to take their medicine. If the patient doesn’t acknowledge the SMS by replying then this can be escalated to their assigned doctor
- Assignment submission: It will notify the student that their assignment is due. If the student doesn’t acknowledge the SMS by replying then this can be escalated to their teacher
High-level Architecture
The solution requires the country of your SMS recipients to support two-way SMS. To check which countries, support two-way SMS visit this page. If two-way SMS is supported then you will need to request a dedicated originating identity. You can also use Toll Free Number or 10DLC if your recipients are in the US.
Note: Sender ID doesn’t support two-way SMS.
A new incident is represented as an item in an Amazon DynamoDB table containing information such as description, URL, incident_id as well as the contact numbers for two resources. A resource is someone who has been assigned to work on this incident. The second resource is for escalation purposes in case the first one doesn’t acknowledge or decline the incident notification.
The Amazon DynamoDB table covers three functions for this solution:
- A way to add new incidents using either the AWS console or programmatically
- As a storage for variables that indicate the incident’s status and can be used from the solution to determine the next action(s)
- As a historical data storage for all incidents that have been created for data analysis purposes
The solution utilizes Amazon DynamoDB Streams to invoke an AWS Lambda function every time a new incident is created. The AWS Lambda function triggers an AWS Step Function State machine, which orchestrates three AWS Lambda functions:
- Send_First_SMS: Sends the first SMS
- Reminder_SMS: Sends a reminder SMS if the resource does not acknowledge the first SMS
- Incident_State_Review: Assesses the status of the incident and either goes back to the first AWS Lambda function or finishes the AWS Step Function State machine execution
The AWS Step Functions State machine uses the Choice state, which evaluates the response of the previous AWS Lambda function and decides on the next state. This is a very useful feature that can reduce custom code and potentially AWS Lambda invocations resulting to cost savings.
Additionally, the waiting between steps is also managed from AWS Step Functions State machine using the Wait state. This can be configured to wait seconds, days or till a specific point in the future.
To be able to receive SMS, this solution uses Amazon Pinpoint’s two-way SMS feature. When receiving an SMS Amazon Pinpoint sends a payload to an Amazon SNS topic, which needs to be created separately. An AWS Lambda function that is subscribed to the Amazon SNS topic processes the SMS content and performs one or both of the following actions:
- Update the incident status in the DynamoDB table
- Create a new Step Function State machine execution
In this solution SMS recipients can reply by typing either yes or no. The SMS response is not case sensitive.
An inbound SMS payload contains the originationNumber, destinationNumber, messageKeyword, messageBody, inboundMessageId and previousPublishedMessageId. Noticeably there isn’t a direct way to associate an inbound SMS with an incident. To overcome this challenge this solution uses a second DynamoDB table, which stores the message_id and incident_id every time an SMS is send to any of the two resources. This allows the solution to use the previousPublishedMessageId from the inbound SMS payload to fetch the respective incident_id from the second DynamoDB table.
The code in this solution uses AWS SDK for Python (Boto3).
Prerequisites
- An Amazon Pinpoint project with the SMS channel enabled – Guide on how to enable Amazon Pinpoint SMS channel
- Check if the country you want to send SMS to, supports two-way SMS – List with countries that support two-way SMS
- An originating identity that supports two-way SMS – Guide on how to request a phone number
- Increase your monthly SMS spending quota for Amazon Pinpoint – Guide on how to increase the monthly SMS spending quota
Deploy the solution
Step 1: Create an S3 bucket
- Navigate to the Amazon S3 console
- Select Create bucket
- Enter a unique name for Bucket name
- Select the AWS Region to be the same as the one of your Amazon Pinpoint project
- Scroll to the bottom of the page and select Create bucket
- Follow this link to download the GitHub repository. Once the repository is downloaded, unzip it and navigate to \amazon-pinpoint-incident-notifications-mechanism-main\src
- Access the S3 bucket created above and upload the five .zip files
Step 2: Create a stack
- The application is deployed using an AWS CloudFormation template.
- Navigate to the AWS CloudFormation console select Create stack > With new resources (standard)
- Select Template is ready as Prerequisite – Prepare template and choose Upload a template file as Template source
- Select Choose file and from the GitHub repository downloaded in step 1.6 navigate to amazon-pinpoint-incident-notifications-mechanism-main\cfn upload CloudFormation_template.yaml and select Next
- Type Pinpoint-Incident-Notifications-Mechanism as Stack name, paste the S3 bucket name created in step 1.5 as the LambdaCodeS3BucketName, type the Amazon Pinpoint Originating Number in E.164 format as OriginatingIdenity, paste the Amazon Pinpoint project ID as PinpointProjectId and type 40 for WaitingBetweenSteps
- Select Next, till you reach to Step 4 Review where you will need to check the box I acknowledge that AWS CloudFormation might create IAM resources and then select Create Stack
- The stack creation process takes approximately 2 minutes. Click on the refresh button to get the latest event regarding the deployment status. Once the stack has been deployed successfully you should see the last Event with Logical ID Pinpoint-Incident-Notifications-Mechanism and with Status CREATE_COMPLETE
Step 3: Configure two-way SMS SNS topic
- Navigate to the Amazon Pinpoint console > SMS and voice > Phone numbers. Select the originating identity that supports two-way SMS. Scroll to the bottom of the page and click to expand the and check the box to enable it.
For SNS topic select Choose an existing SNS topic then using the drop down choose the one that contains the name of the AWS CloudFormation stack from Step 2.4 as well as the name TwoWaySMSSNSTopic and click Save.
Step 4: Create a new incident
To create a new incident, navigate to Amazon DynamoDB console > Tables and select the table containing the name of the AWS CloudFormation stack from Step 2.4 as well as the name IncidentInfoDynamoDB. Select View items and then Create item.
On the Create item page choose JSON, copy and paste the JSON below into the text box and replace the values for the first_contact and second_contact with a valid mobile number that you have access to.
Note: If you don’t have two different mobile numbers, enter the same for both first_contact and second_contact fields. The mobile numbers must follow E.164 format +<country code><number>.
{
"incident_id":{
"S":"123"
},
"incident_stat":{
"S":"not_acknowledged"
},
"double_escalation":{
"S":"no"
},
"description":{
"S":"Error 111, unit 1 malfunctioned. Urgent assistance is required."
},
"url":{
"S":"https://example.com/incident/111/overview"
},
"first_contact":{
"S":"+4479083---"
},
"second_contact":{
"S":"+4479083---"
}
}
Incident fields description:
- incident_id: Needs to be unique
- incident_stat: This is used from the application to store the incident status. When creating the incident, this value should always be not_acknowledged
- double_escalation: This is used from the application as a flag for recipients who try to escalate an incident that is already escalated. When creating the incident, this value should always be no
- description: You can type a description that best describes the incident. Be aware that depending the number of characters the SMS parts will increase. For more information on SMS character limits visit this page
- url: You can add a URL that resources can access to resolve the issue. If this field is not pertinent to your use case then type no url
- first_contact: This should contain the mobile number in E.164 format for the first resource
- second_contact: This should contain the mobile number in E.164 format for the second resource. The second resource will be contacted only if the first one does not acknowledge the SMS or declines the incident
Once the above is ready you can select Create item. This will execute the AWS Step Functions State machine and you should receive an SMS. You can reply with yes to acknowledge the incident or with no to decline it. Depending your response, the incident status in the DynamoDB table will be updated and if you reply no then the incident will be escalated sending a SMS to the second_contact.
Note: The SMS response is not case sensitive.
Clean-up
To remove the solution:
- Delete the AWS CloudFormation stack by following the steps listed in this guide
- Delete the dedicated originating identity that you used to send the SMS by following the steps listed in this guide
- Delete the Amazon Pinpoint project by navigating the Amazon Pinpoint console, select your Amazon Pinpoint Project, choose Settings > General settings > Delete Project
Next Steps
This solution currently works only if your SMS recipients are in one country. If your use case requires to send SMS to multiple countries you will need to:
- Check this page to ensure that these countries support two-way SMS
- Follow the instructions in this page to obtain a number that supports two-way SMS for each country
- Expand the solution to identify the country of the SMS recipient and to choose the correct number accordingly. To identify the country of the SMS recipient you can use Amazon Pinpoint’s phone number validate service via Amazon Pinpoint API or SDKs. The phone validate service returns a list of data points per mobile number with one of them being the Country
Incidents that are not being acknowledged by any of the assigned resources, have their status updated to unacknowledged but they don’t escalate further. Depending your requirements, you can expand the solution to send an email using Amazon Pinpoint APIs or perform an outbound call using Amazon Connect APIs.
Conclusion
In this blog post, I have demonstrated how your organization can use Amazon Pinpoint two-way SMS and Step Functions to automate incident notifications. Furthermore, the solution highlights the synergy of AWS services and how you can build a custom solution with little effort that meets your requirements.