Build Azure Service Bus Queues Using Terraform
TL;DR: 7 resources will be added to your Azure account. 1 — Configure Terraform to save state lock files on Azure Blob Storage. 2 — Use Terraform to create and keep track of your Service Bus Queues
You can find all the source code for this project on this GitHub repo: https://github.com/guisesterheim/TerraformServiceBusQueues
Azure Service Bus has two ways of interacting with it: Queues and Topics (SQS and SNS on AWS respectively). Take a look at the docs on the difference between them and check which one fits your needs. This article covers Queues only.
What are we creating?
The GRAY area on the image above shows what this Terraform repo will create. The retry queue automation on item 4 is also created by this Terraform. Below is how the information should flow in this infrastructure:
- Microservice 1 generates messages and posts them to the messagesQueue.
- Microservice 2 listens to messages from the Queue and process them. If it fails to process, post back to the same queue (for up to 5 times).
- If it fails for more than 5 times, post the message to the Error Messages Queue.
- The Error Messages Queue automatically posts back the errored messages to the regular queue after one hour (this parameter can be changed on file modules/queue/variables.tf)
- Whether there’s an error or success, Microservice 2 should always post log information to Logging Microservice
Starting Terraform locally
To keep track of your Infrastructure with Terraform, you will have to let Terraform store your tfstate file in a safe place. The command below will start Terraform and store your tfstate in Azure Blob Storage. Use the following command to start your Terraform repo:
terraform init \
-backend-config "container_name=<your folder inside Azure Blob Storage>" \
-backend-config "storage_account_name=<your Azure Storage Name>" \
-backend-config "key=<file name to be stored>" \
-backend-config "subscription_id=<subscription ID of your account>" \
-backend-config "client_id=<your username>" \
-backend-config "client_secret=<your password>" \
-backend-config "tenant_id=<tenant id>" \
-backend-config "resource_group_name=<resource group name to find your Blob Storage>"
If you don’t have the information for the variables above, take a look at this post to create your user for your Terraform+Azure interaction.
Should everything goes well you should get a screen similar to the one below and we are ready to plan our infrastructure deployment!
Planning your Service Bus deploy
The next step is to plan your deployment. Use the following command so Terraform can prepare to deploy your resources:
terraform plan \
-var 'client_id=<client id>' \
-var 'client_secret=<client secret' \
-var 'subscription_id=<subscription id>' \
-var 'tenant_id=<tenant id>' \
-var-file="rootVars.tfvars" \
-var-file="rootVars-<environment>.tfvars" \
-out tfout.log
Some of the information above are the some as we used in Terraform init. So go ahead and copy them. The rest of them are:
- -VAR-FILE — The first var file one has common variables for all our environments.
- -VAR-FILE — The second var file has a specific value for the current environment. Take a look at the rootVars-<all>.tfvars files.
- TFOUT.LOG — This is the name of the file to which Terraform will store the plan to achieve your Terraform configuration
Should everything goes well you’ll have a screen close to the one below and we’ll be ready to finally create your Service Bus Queues!
Take a look at the “outputs” section. These are the information Terraform is gonna retrieve us so our DEV team can use it.
Deploying your Service Bus infrastructure
All the hard work is done. Just run the command below and wait for about 10 minutes and your AKS will be running
terraform apply tfout.log
Once the deployment is done you should see a screen like this:
Once you are done you have the connection strings so the DEV team can configure the microservices to use your Queue.
To read more on how to integrate your applications to the Queues, Microsoft has the docs for Java, Node, PHP, and a few others.