Bootstrap FreeKB - Amazon Web Services (AWS) - List Simple Notification Service (SNS) Topics using Terraform
Amazon Web Services (AWS) - List Simple Notification Service (SNS) Topics using Terraform

Updated:   |  Amazon Web Services (AWS) articles

Let's say you have the following files on your Terraform server.

├── required_providers.tf
├── sns (directory)
│   ├── provider.tf
│   ├── sns_topic.tf

 

required_providers.tf will almost always have this.

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
    }
  }
}

 

Let's say provider.tf has the following. In this example, the "default" profile in /home/username/.aws/config and /home/username/.aws/credentials is being used. This assumes you have setup Terraform as described in Amazon Web Services (AWS) - Getting Started with Terraform.

provider "aws" {
  alias   = "default"
  profile = "default"
  region  = "default"
}

 

Before creating an alarm, you will need the Amazon Resource Number (ARN) of one of your AWS Simple Notification Service (SNS) Topics because typically, you will want to be contacted somehow when an alert fires. For example, perhaps you want to get an email when an alert fires. An AWS Simple Notification Service (SNS) Topics can be setup so that you get an email when the alert fires. Check out my article on Creating a Simple Notification Service (SNS) Topics using the AWS CLI.

~]$ aws sns create-topic --name my-topic 
{
    "TopicArn": "arn:aws:sns:us-east-1:123456789012:my-topic"
}

 

Then sns_topic.tf could have something like this, to get the ARN of the SNS topic.

data "aws_sns_topic" "my_topic" {
  name = "my-topic"
}

 

And then users will subscribe to the Topic which can be done using the aws sns subscribe command.

 ~]$ aws sns subscribe --topic-arn arn:aws:sns:us-east-1:123456789012:my-topic --protocol email --notification-endpoint john.doe@example.com
{
    "SubscriptionArn": "pending confirmation"
}

 

You may need to reissue the terraform init command.

~]# terraform init
Initializing the backend...
Initializing modules...
Initializing provider plugins...
Terraform has been successfully initialized!

 

The terraform plan command can be used to see what Terraform will try to do.

~]$ terraform plan
data.aws_sns_topic.my_topic: Reading...
data.aws_sns_topic.my_topic: Read complete after 0s [id=arn:aws:sns:us-east-1:123456789012:my-topic]

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter e994da in the box below so that we can be sure you are a human.