Salesforce Subscription to Amazon Simple Notification Service (SNS) using Cloud9, Lambda, and Python

Emilio Taylor
6 min readOct 22, 2020

Introduction

Imagine needing to expand Salesforce with AWS at your organization. Part of the design includes a messaging service. Messages published through this service are then used to create data within Salesforce. Sounds complicated? Not really, you can do this. Let’s begin …

In this lesson, our challenge will be to:

  • Setup AWS Cloud9 Development Environment
  • Install & Use Simple Salesforce REST API Client for Python
  • Enable Message Published to SNS to Trigger AWS Lambda Function
  • Use AWS Lambda Function to authenticate with Salesforce, and Create Salesforce Service Cloud Case

Prerequisites

Note: Here’s another article I wrote for how this would work on Google Cloud Platform (GCP)

Architecture

Step 1: Cloud9 — Setup Development Environment

Using a Cloud9 Development Environment sets up a dedicated environment to create functions, access terminals, and install modules through the command line. This comes in handy when installing modules, prototyping with sample data, and deploying completed functions for use within Lambda.

Steps:

  • Look for AWS Cloud9 in AWS Console
  • Select Create Environment
  • Within Name Environment: Supply Name & Description
  • Within Environment Settings: Choose the Following
  • Environment Type: Create a new EC2 instance for environment (direct access)
  • Instance Type: t2.micro (1 GiB RAM + 1 vCPU)
  • Platform: Amazon Linux
  • Select Next
  • Confirm Selection and Choose Create Environment

Step 2: Cloud9 — Create Function

Now that a dedicated development environment has been created, we’ll proceed to create a function using Python 3.6.

Steps:

  • On the Cloud9 homepage, select Create Lambda Function
  • On the Create serverless application form, please enter the Function/Application name
  • Select Next
  • On Select Runtime, select Python 3.6
  • On Select Blueprint, select empty-python
  • Select Next
  • On Function trigger, select none — note: Later, we’ll add the SNS trigger within AWS Lambda Function
  • Select Next
  • Memory (MB): 128 MB
  • Role: Automatically generate role
  • Select Next
  • On the confirmation page, review selections and select Finish

Step 3: Cloud9 — Import Simple Salesforce (Rest API Client) Python Module

Within the new application folder, we need to include the Simple Salesforce module to be used for our Lambda function. Let’s continue.

Steps:

  • From the application folder, right-click and select Open Terminal Here
  • Enter the following command to install Simple Salesforce to the application
python -m pip install --target=./ simple-salesforce

Step 4: Cloud9 — Update Code, Test & Deploy Function

Within this step, we’ll proceed to update the “sample” Lambda Handler with our updated code. This new code will include references to the Simple Salesforce API, Login function, and Create Record method to send data to Salesforce

Steps:

  • On the lambda_function.py file, replace with the following code
  • Select the Run button on the console
  • On the Test section, enter the JSON payload below (note: this Payload resembles the same format as an AWS SNS Message (note the Message section)
  • Then, in the Test section, select Run to validate the Function
  • A successful run should result in a Response reading: Main Request Passed
{
"Records": [
{
"EventSource": "aws:sns",
"EventVersion": "1.0",
"EventSubscriptionArn": "arn:aws:sns:us-east-1:{{{accountId}}}:ExampleTopic",
"Sns": {
"Type": "Notification",
"MessageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e",
"TopicArn": "arn:aws:sns:us-east-1:123456789012:ExampleTopic",
"Subject": "example subject",
"Message": "{'Subject':'Test AWS1','Description':'AWS2'}",
"Timestamp": "1970-01-01T00:00:00.000Z",
"SignatureVersion": "1",
"Signature": "EXAMPLE",
"SigningCertUrl": "EXAMPLE",
"UnsubscribeUrl": "EXAMPLE",
"MessageAttributes": {
"Test": {
"Type": "String",
"Value": "TestString"
},
"TestBinary": {
"Type": "Binary",
"Value": "TestBinary"
}
}
}
}
]
}
  • Upon successful confirmation, proceed to Deploy the function

Step 5: Simple Notification Service (SNS) — Create Topic

Before we can connect a Trigger to the function, we’ll first need to set up a Simple Notification Service (SNS) Topic to capture our “Case” messages.

Steps:

  • In the AWS Console, search for SNS
  • On the SNS page, enter “Case” as a Topic and choose Next Step
  • On the Create Topic page, simply choose Create Topic to Complete

Step 6: Lambda Function — Add SNS Trigger

Now that we’ve established a Topic, we’ll proceed to connect the SNS Message Trigger to the Lambda Function we previously created in order to send data to Salesforce.

Steps:

  • In the AWS Console, search for Lambda
  • Select the Lambda function from Cloud9
  • On the function page, select the Add trigger button
  • On the Trigger configuration page, select SNS
  • Then on SNS Topic, select Cases

Step 7: Test Using Salesforce (Case)

We now have all the pieces in place, let’s proceed to create an SNS message to be sent to Salesforce as a Case.

Steps:

  • In the AWS Console, search for SNS
  • On the SNS Dashboard, select Cases
  • On the Cases, topic page, select Publish Message
  • On the Publish Message page, within the Message Body, enter the following
  • Select the Publish Message button
  • In Salesforce, see that the Case is Created

What’s Next?

Expanding Salesforce with AWS is an extremely powerful combination. What has been done so far is a good first step. However, to make this service production-ready, consider the following enhancements to this code. First, consider changing the object reference to an environment variable for reuse. Second, consider storing your Salesforce credentials using Secret Manager. Lastly, consider the security of your message by enabling encryption then decrypting the message within Python. For another hint on how to implement these recommended items, and how this same pattern works on Google Cloud Platform (GCP), check my other article click here. Good Luck!

--

--

Emilio Taylor

Salesforce Practice Leader, Technology Enthusiast, Entrepreneur, Integrator, Architect, Developer, and Overall Cloud Advocate.