Setting Up an AWS SQS Queue with Terraform: A Step-by-Step Guide

This guide demonstrates how to create an Amazon SQS queue using Terraform in the eu-west-3 region, allowing all entities to send messages. Before we begin, a fundamental understanding of both SQS and Terraform is assumed.

Once your SQS queue is set up, you can explore further by creating a subscription between SQS and SNS. Click here for a guide, though it’s beyond this article’s scope.

This article will focus on creating a standard queue. For detailed information on the available arguments and properties for SQS in Terraform, visit this link.

Pre-requisites

  1. Basic understanding of Terraform.
  2. Terraform installed on your system.
  3. An active AWS Account (Sign up if you don’t have one).
  4. ‘access_key’ and ‘secret_key’ of an AWS IAM User with the necessary permissions for creating and deleting SQS queues. Learn about IAM users here.

Steps to Follow

  1. Draft Terraform configuration files for the SQS Queue.
  2. Create an SQS Queue using the drafted Terraform files.
  3. Remove the created SQS Queue using Terraform.

Draft Terraform Configuration Files for SQS Queue

Create a file named “main.tf” with the following resource definition to deploy an SQS in “eu-west-3“. Update the region to your preference if necessary.

Access the complete code on my Github repository.

File: main.tf
provider "aws" {
    access_key = "${var.access_key}"
    secret_key = "${var.secret_key}"
    region = "eu-west-3"
}

resource "aws_sqs_queue" "my_first_sqs" {
  name = var.sqs_name
}

resource "aws_sqs_queue_policy" "my_sqs_policy" {
  queue_url = aws_sqs_queue.my_first_sqs.id

  policy = <<POLICY
{
  "Version": "2012-10-17",
  "Id": "sqspolicy",
  "Statement": [
    {
      "Sid": "First",
      "Effect": "Allow",
      "Principal": "*",
      "Action": "sqs:SendMessage",
      "Resource": "${aws_sqs_queue.my_first_sqs.arn}"
    }
  ]
}
POLICY
}
  • name: Defined in variables.tf.
  • queue_url: Retrieved from the my_first_sqs queue.

Store your AWS IAM user access and secret keys in “terraform.tfvars“. Ensure the IAM user has adequate permissions for SQS.

Access the file here on Github.

File: terraform.tfvars
access_key = "<your-aws-access-key>"
secret_key = "<your-aws-secret-key>"

Create a “variables.tf” file to define variables, preventing hardcoding in “main.tf“. Change the “sqs_name” variable as needed.

GitHub link to the variables file: variables.tf

File: variables.tf
variable "access_key" {
  description = "Access key of AWS IAM User for SQS Queue creation and deletion"
}
variable "secret_key" {
  description = "Secret key of AWS IAM User for SQS Queue creation and deletion"
}

variable "sqs_name" {
  description = "Name of the SQS queue to be created. Assign any unique name."
  default = "my-first-sqs"
}
  • sqs_name: Default value is my-first-sqs, modify it if needed.

Create an SQS Queue using the Terraform Configuration Files

Once main.tf, terraform.tfvars, and variables.tf are ready, initiate the creation of an SQS queue using Terraform.

Initialize the working directory containing the Terraform files with:

terraform init

terraform init

Create an execution plan to review proposed changes:

terraform plan

Apply the configuration to create the SQS queue and attach the policy using:

terraform apply

Verify the newly created SQS queue in the AWS SQS console.

Delete the Created SQS Queue using Terraform

To remove the SQS queue, execute the command below. Ensure you confirm deletion as it cannot be undone.

terraform destroy

terraform destroy

Conclusion

This guide detailed the creation and deletion of an Amazon SQS queue using Terraform, emphasizing the use of separate variable files to avoid hardcoding in main.tf, and the ease of resource management with Terraform.

Frequently Asked Questions (FAQ)

  1. Why is Terraform used for SQS queue management?
    Terraform allows for infrastructure as code, enabling efficient management, deployment, and version control of AWS resources like SQS queues across different environments.
  2. What are the advantages of using a variables file?
    A variables file promotes modularity and reuse, making Terraform configurations more maintainable and easier to manage or modify.
  3. Can I create multiple SQS queues using this configuration?
    Yes, modify the configuration to include multiple resource blocks with different queue names using variables or directly within the main.tf file.
  4. Is it possible to recover an SQS queue after using ‘terraform destroy’?
    No, once the SQS queue is deleted via Terraform, recovery isn’t possible. Always confirm the resources before destruction.