Using ColdFusion to send a one off SMS Message using AWS SNS (in five lines of code!)

September 23, 2019
Wizard 25 posts
Followers: 13 people

Using ColdFusion to send a one off SMS Message using AWS SNS (in five lines of code!)

Wizard 25 posts
Followers: 13 people
September 23, 2019

This can certainly be chalked up to be a “this is why I love ColdFusion” posts.

First my obligatory disclaimer;  I’m not an AWS guru.  I’m not a Java developer per se.  Most of what I’ve learned has been through brute force, hack and slash, trial and error development.  Think of my coding style like a drunken baby and accept that this may or may not be the “right way” to accomplish this task… but it’s the way I stumbled upon.

I’m working with a client who sells travel packages to customers.  Once an agent builds a package for a customer, they email them a link where the customer can complete their purchase.  They wanted to add a text messaging feature that sends an SMS text message to their mobile device.

Amazon Web Services includes a service called Simple Notification Service, or SNS.  SNS will do much, much, MUCH more than send simple SMS text messages, but the task in my hands is to send an SMS… so I’m going to limit  the scope of its use to doing just that.

ColdFusion runs on top of Java.  ColdFusion is, essentially a Java pre-parser.  In fact, the end result of ColdFusion processing a CFM page is Java byte code that does all of the processing for your ColdFusion pages.  As such, there’s a world of extensibility available to you in the Java world that you can leverage easily using ColdFusion.

Likewise, AWS has a Java Development Kit available for download.  Between the availability of the JDK for AWS and the extensibility of ColdFusion to be able to use JAR files, this becomes a very simple task.

Setting everything up

There are some steps needed to get this up and running on ColdFusion, but they’re pretty straightforward.

  1. Create an AWS account.
  2. Create (or make sure you have) an AWS User who has privileges to use SNS.
  3. Obtain that users Access and Secret keys for using AWS.
  4. Download the AWS JDK.
  5. Copy the appropriate JAR files into your ColdFusion installation.
  6. Restart the ColdFusion service.

Specifically relating to number 5 in this list, you need the following JAR files from the JDK Download:

  • jackson-annotations-2.6.0.jar
  • jackson-core-2.6.7.jar
  • jackson-databind-
  • joda-time-2.8.1.jar

The AWS SDK .jar is located in the lib directory, and the others are in the third-party directory.  Copy these files to your {cf-root}/cfusion/lib directory.

Once the JAR files are in your ColdFusion installation, and the service has been restarted, here’s the code needed to send an SMS through AWS SNS, in its entirety:

   awsCredentials = createObject('java','com.amazonaws.auth.BasicAWSCredentials').init([YOUR AWS ACCESS KEY], [YOUR AWS SECRET KEY]);
   awsStaticCredentialsProvider = createObject('java','com.amazonaws.auth.AWSStaticCredentialsProvider').init(awsCredentials);
   snsClient = createObject('java', '').standard().withCredentials(awsStaticCredentialsProvider).withRegion([YOUR AWS REGION THAT SUPPORTS SMS]).build();
   publishRequest = createObject('java', '').init().withPhoneNumber('+1XXX5551212').withMessage('This is my Message');
   Result = snsClient.publish(publishRequest);

And sure enough.  This arrived on my phone:

AWS SNS Result

BOOM! Text messages via AWS SNS in just five lines of code.

Making it Better

Now that I have a proof-of-concept, there’s a lot of things I can do to make this better.  I could make the AWS Credentials variable an application scoped variable that gets created on application start.  Heck the first three lines of my proof-of-concept could be cached in the application.  I could also write a function that I pass the message and the phone number and it just takes care of it for me.  What I have, however, is enough to prove it can work.


Some other ways I considered taking care of this task:

  • has an API for sending SMS messages that’s REST based.  I’m sure there are many other services that offer something similar.
  • Almost every mobile provider has an email to SMS gateway, so as long as you know the provider, you can send an email using cfmail and the mobile provider

Credit where Credit is Due

Brian Klass.  I’ve seen him present at CF Summit several times, and his blog has great articles on how to use AWS with ColdFusion for a multitude of services.  Pay special attention to the following articles:


Comments (0)
Add your comment