Open In App

Creating SNS And SQS Using Terraform

Terraform, is an open-source and specific infrastructure as a code (IaC) tool developed by HashiCorp. that helps the Terraform users to build, change, and version their infrastructure in the most secure and efficient way. using terraform, users can manage resources such as virtual machines, storage, and network through infrastructure provisioning and management.

terraform helps to implement infrastructure provisioning and management across different environments in a single process, regardless of whether these environments are on-premises or in the cloud.



Why To Use Terraform ?

Managing infrastructure was often done by manually and it was a process prone to mistakes. There would be no automation to set up infrastructure without IaC tools like Terraform, which meant manually doing everything leading to inconsistencies and slow deployments. However, Chef and Puppet among other configuration management tools offer some level of automation though they are more about server configurations than the provision of complete infrastructure components. Without comprehensive automation in place, scaling out infrastructure was a challenging task that involved repetition of manual activities and exposed organizations to human errors. Traditional infrastructure management lacked strong versioning tools which made it difficult to keep track of changes, work together well and roll back to previous configurations when necessary. Nevertheless, Terraform changed all this with its introduction transforming how we provision and administer infrastructures.

How Does Terraform Works?

The core components of Terraform are as follows



1. Terraform Core

2. Terraform Plugins

1. Terraform Core

It forms the backbone of Terraform itself. This piece is statically compiled binary written in Go programming language. The communication between terraform core and terraform plugins happens through RPCs (remote procedure calls). It discovers various plugins paths to include into its working-flow via RPC-based protocol regarding their remote registration and discovery procedures.

Terraform Command Line Interface (CLI), enable users to communicate through an interface that facilitates their infrastructure provisioning and management activities.

Responsibilities Of Terraform Core

The following are the responsibilities of Terraform Core:

2. Terraform Plugins

Responsibilities Of Terraform Plugin

The following are the responsibilities of Terraform plugins:

Terraform Workflow

The fundamental Terraform workflow consists of three primary steps:

1. Definition / Write

2. Terraform Plan

Terraform plan 

3. Execution/ Apply

terraform apply

Core Components Of Terraform

The following are the core components of Terraform:

Features Of Terraform

The following are the features of Terraform:

Understanding Of Amazon SNS And SQS

Firstly, lets start with discuss about Amazon SNS,

Amazon SNS (Simple Notification Service)

Amazon SNS sends messages in two different ways. They are

1. Application To Application (A2A)

2. Application To Persion (A2P)

1. Application To Application (A2A)

Application-to-application (A2A) messaging in Amazon SNS enables a fast, push-based, many-to-many messaging way for different software programs to talk to each other. it sends message Asynchronously. which means sending messages between apps without needing to wait for a response.

Application-to-Application offers efficient messaging between distributed systems, microservices, and event-driven serverless applications. These applications include Amazon Simple Queue Service (SQS), Amazon Kinesis, Data Firehose, AWS Lambda, and other HTTPS endpoints.

2. Application To Person (A2P)

Terminologies Of Amazon SNS

Applications of SQS

The following are some Applications of Amazon SQS

Difference Between Amazon SNS and Amazon SQS

Amazon SNS

Amazon SQS

Amazon SNS is a publisher/subscriber messaging service.

Amazon SQS is a message queueing service.

it is designed for message distribution.

it is designed for message processing.

Amazon SNS enables publishers to send messages to multiple subscribers simultaneously.

Amazon SQS delivers messages to consumers by pulling them from the queues. Each message in the queue is typically processed by a single consumer.

in this service, Subscribers can be of different types, such as SQS queues, Lambda functions, or even email.

in this service Messages are stored in queues and can be processed by one or more consumers.

SNS offers real-time message distribution to subscribers opted in specific topics.

SQS provides asynchronous message processing with queues.

SNS allows for push-based message delivery.

SQS supports pull-based message retrieval.

Creating Amazon SNS And Amazon SQS Using Terraform: A Step-By-Step Guide

Step 1: Terraform Configuration file

terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.31"
}
}
}

provider "aws" {
region = "<your-aws-region>"
}

Step 2: Initialise A terraform Working Directory

Terraform init

Step 3: Defining AWS SNS And AWS SQS

resource "aws_sns_topic" "<your-desired-resource-name>" {
name = "<sns-name>"
}

resource "aws_sqs_queue" "<your-desired-resource-name>" {
name = "<sqs-name>"
}

aws_sns_topic

Step 4: Apply Terraform Configuration

execute following terraform command to see the execution plan and verify that Terraform will create the expected resources.

Terraform plan

terraform apply

Step 5. Verify Created Resources In AWS

Step 6: Clean up

Terraform destroy

Challenges Of Terraform

The following are the challenges of Terraform:

How Terraform Deals With These Challenges

Terraform is key in enabling modern approaches­ like DevOps as well as cloud-native environments by enhancing flexibility, dependability along with speed in infrastructure management.

Conclusion

It is beneficial for users to be able to construct a cost-effective scalable and reliable infrastructure by being able to control AWS resources such as SNS (Simple Notification Service) and SQS (Simple Queue Service) with Terraform. The infrastructures can be defined in a version controlled way so that developers can manage their infrastructure configurations through the use of Terraform’s declarative syntax and infrastructure as code principles, thereby ensuring reproducibility and reliability across environments.

Creating SNS And SQS Using Terraform – FAQ’s

What Is Terraform, And Why Should I Use It To Create Aws Resources Like SNS And SQS?

Terraform is an infrastructure as code tool that helps you in defining, provisioning, and managing infrastructure resources in a declarative manner. By using Terraform for AWS resources like SNS and SQS, automation is yielded, reusability realized and scalability achieved not forgetting time saved.

What Are SNS And SQS? And How Do They Differ?

While SQS (Simple Queue Service) decouples message processing between components by serving as a message queuing service for messages, SNS (Simple Notification Service) delivers notifications through messages sent from subscribers. Push-based message delivery is enabled on SNS while pull-based retrieval of messages is supported by SQS.

Is It Important To Clean Up Resources After Creating Them With Terraform?

Yes, cleaning up is important after building AWS resources or any provider’s resources as this will prevent expense control, protect security, and avoid hitting resource limits. accumulating costs for Unused resources can occur over time.

Is There A Risk Of Accidentally Deleting Critical Resources With Terraform?

Yes, when no one does not take the right precautions, they may delete some critical resources unintentionally. It is exceptionally essential that you review an execution plan before implementing changes and use state management and locking mechanisms to prevent accidental deletion of all resources defined in the configuration file from terraform destroy command.

Publishers in Amazon SNS is like sender that creates and sends a message to topic, and asynchronously communicates with the subscriber. a publisher sends messages only to a specific topic they have permission to publish.


Article Tags :