How to Publish to an Amazon SNS topic while keeping the messages secure in a private network using CloudFormation.

The architecture is defined below:

Before you start, you need an Amazon Web Services (AWS) account. If you haven’t created an account already, go to https://aws.amazon.com/, and then choose Create a Free Account.

The stack for this process includes the following resources:

  • A VPC and the associated networking resources, including a subnet, a security group, an internet gateway, and a route table.

Create the AWS resources

To set up the infrastructure, you use an AWS CloudFormation template.

Download the template file from the GitHub website. https://github.com/aws-samples/aws-sns-samples/blob/master/templates/SNS-VPCE-Tutorial-CloudFormation.template

  1. Sign in to the AWS CloudFormation console.

5. On the Specify Details page, specify stack and key names:

For Stack name, type name VPC-SNS.

For KeyName, choose KeyPair for that region if you do not have create new key pair .

For SSH Location, keep the default value of 0.0.0.0/0 and click next.

Keep everything default here

  1. On the Review page, verify the stack details.

Creation is in progress

Confirm that your Amazon EC2 instance lacks internet access

To connect to your Amazon EC2 instance

  1. Open the Amazon EC2 console .

Create an Amazon VPC endpoint for Amazon SNS

  1. Open the Amazon VPC console .

Publish a message to your Amazon SNS topic

The VPC you created includes an endpoint for Amazon SNS, you can log in to the Amazon EC2 instance and publish messages to the topic.

If your terminal is no longer connected to your Amazon EC2 instance, connect again

Run the command below to publish a message to your Amazon SNS topic. You should get a message ID :

aws sns publish — region us-west-2 — topic-arn arn:aws:sns:us-west-2:392804924255:VPCE-Tutorial-Topic — message “Hello”

Change the region and the topic arn from the sns topic .

Verify your message deliveries

  1. Open the AWS Lambda console .

Verify that the CloudWatch logs were updated

  1. Open the CloudWatch console.

Clean up

  1. Open the Amazon VPC console .

To delete your AWS CloudFormation stack

  1. Open the AWS CloudFormation console at https://console.aws.amazon.com/cloudformation.

Thanks!!! 👏🏻