How To Deploy a Simple Application in Azure using Cosmos DB and Terraform
In this post, you'll learn how to deploy a simple application in Azure, by using Azure Cosmos DB, Terraform and Azure Container Instances.
Jun 08, 2023 • 8 Minute Read
Azure Cosmos DB is a globally distributed database service. It provides developers with a large amount of flexibility when it comes to developing globally distributed applications. Thanks to Terraform, you can easily deploy Cosmos DB accounts and resources with just a few lines of HCL code.
In this post, I’m going to show you how to deploy a simple application in Azure. We'll do this by using Terraform to deploy Azure Cosmos DB to Azure Container Instances. First we'll learn how to create an Azure Cosmos DB instance, then update the configuration to create an Azure Container Instance, and finally create a simple application that works with both these resources.
Accelerate your career!
Get started with ACG and transform your career with courses and real hands-on labs in AWS, Microsoft Azure, Google Cloud, and beyond
What you’ll need
To follow along with this post, you will need a few things:
Step 1: Creating the initial configuration
Assuming that you have everything you need to get started, the next thing to do is configure Terraform to use the Azure provider for your configuration. You’ll need to create a file in the directory you create for your project called main.tf. I created a directory called terraformguru that will be my working directory for this project.
In the main.tf file you will first need to define the providers that you will use for the configuration.
Once we have defined our providers, we will need to define three resources in our initial configuration.
- The first resource is our resource group that we will tie our other resources to.
- We will need to use the random provider in our next resource block to generate a random integer to use in our Azure Cosmos DB Account configuration.
- We then can define our Azure Cosmos DB Account.
Your configuration should look like the example below:
In our Azure Cosmos DB Account resource block we need to define these arguments:
- name (combined with the random integer to make a unique name)
- location (resource group location)
- resource_group_name (name of the resource group)
- offer_type
- kind
- consistency_policy
- geo_location
Once we have defined our three resources it is time to apply our configuration and create our Cosmos DB instance.
Step 2: Applying our configuration
The first step is to initialize our working directory so we can pull down the needed provider plugins for our project. We can accomplish this by running the following command:
Next we will validate our configuration to make sure the syntax of our code is correct by running the following command:
Once we confirm our configuration is valid, we can next do a dry run of the configuration by running the plan command like our example below:
If it all checks out and looks like it will do what we intend it to do we can finally apply our configuration:
After the apply runs we should have an instance of Cosmos DB in Azure. Now we can update our configuration so we can use our new instance.
Step 3: Update the configuration with a container instance
Now it’s time to update our configuration and add a container instance. This container instance will then use our Cosmos DB instance, which itself uses a container image that runs a small voting web application. The added configuration should look similar to our below example:
Here we are adding a resource block for our container instance. In our Azure Container Group resource block we need to define these arguments:
- name
- location (resource group location)
- resource_group_name (name of the resource group)
- ip_address_type
- dns_name_label
- os_type
- container (this is where we specify our image)
- secure_environment_variables
There are a few key things to point out here.
- First off in our container argument, notice we are specifying an image that’s being pulled from the Microsoft container image registry. We are using the frontend voting application with a Cosmos DB backend.
- We are pulling two environment variables from our Cosmos DB instance that we created and setting an environment variable for each.
- First we are pulling the endpoint for our database and setting the environment variable COSMOS_DB_ENDPOINT and then we are pulling the primary master key of our database and setting the environment variable COSMOS_DB_MASTERKEY. These two environment variables are keys to your database and allow the container to connect to it and use the database instance. Without them the application will not work.
- Also notice we are setting an output variable that will display our application endpoint so we can connect to the application.
Once we have added this to our configuration, it should look like this:
We can now deploy our changes to our configuration. We should validate our configuration to make sure the syntax of our code is correct:
We'll then do a dry run of the configuration:
And then finally apply our configuration:
After the apply runs we should see the following output:
Testing our application
Now our resources have been deployed, and we have our application endpoint that was displayed in the output in our terminal.
Go to your browser and use the address displayed in our output to navigate to our application. You should see the following:
Now if you select one of the the options you should instantly see a tally of the votes like so:
Step 4: Success!
That’s it! You have successfully been able to deploy a simple application in Azure using Terraform and Cosmos DB. As you can see it was quick and easy, and only took a few lines of Terraform code. To learn more, please check out my course Advanced Terraform with Azure. Stay awesome gurus and learn all the things! Until next time.
Want to keep up with all things Azure? Subscribe to A Cloud Guru on YouTube for weekly Amazon news and AWS announcements. You can also like ACG on Facebook, follow us on Twitter, or join the conversation on Discord!