Using Simple Notification Service (SNS) in CFML: Creating Topics

Posted 8 June 2018

The process of creating a topic in SNS via CFML is simple, as is working with SNS in general.

If you haven’t already read the entry on the basic setup needed to access AWS from CFML, please do so now.

The JavaDocs for the AWS Java SDK are comprehensive and always up-to-date. They are, alas, also just JavaDocs. You’re not going to find detailed examples of how to complete full tasks in these docs. If you look at the documentation for the com.amazonaws.services.sns class, you can see all the things that you can do with a connection to SNS.

When working with the AWS Java SDK, there’s a basic pattern that you’ll follow. It goes like this:

  1. Get a copy of the client that’s making a connection to the service you want to use.
  2. Create a “request” object.
  3. Fill the “request” object with the parameters you want to add.
  4. Tell the client to make the request.
  5. Get back a “response” object.

We’re working directly with Java here, so everything is an object. CFML fortunately shields us from a lot of the verbose impementation fallout from this reality. The AWS Java SDK is not CFML, so we have to get a little verbose.

Following the pattern above, in order to create a new topic, we:

  1. Make a connection to SNS with a SNS client.
  2. Create a CreateTopicRequest object. (This is, literally, the request to create a new topic.)
  3. Use the CreateTopicRequest’s fluent “with” interface to set the name of the topic.
  4. Tell the SNS client to createTopic() using the CreateTopicRequest.
  5. Get back a createTopicResult.

The createTopicResult object will contain the ARN of the topic inside AWS. What’s an ARN?

An ARN — Amazon Resource Name — is the unique identifier of an object within all of AWS. Almost every object you’ll work with in AWS will have an ARN.

Those are the basics. Here’s the relevant code from /sns.cfm in the AWSPlaybox app:

sns = application.awsServiceFactory.createServiceObject('sns');

topicName = "AWSPlayboxDemoTopic-" & dateTimeFormat(Now(), "yyyy-mm-dd-HH-nn-ss");
createTopicRequest = CreateObject('java', 'com.amazonaws.services.sns.model.CreateTopicRequest').withName(topicName);

createTopicResult = sns.createTopic(createTopicRequest);

application.awsResources.currentSNSTopicARN = createTopicResult.getTopicArn();

Breaking this down:

We’re storing the new topic ARN in the application scope in the AWSPlaybox app. You’d want to store this in a database in a production app that uses SNS.

Now that we have a topic to work with, we can subscribe to that topic. That’s the subject of the next post.

Categories: AWS ColdFusion