deploying an application in Azure
Home » Blog » Steps for Deploying an Application in Azure using Terraform and Cosmos DB

Steps for Deploying an Application in Azure using Terraform and Cosmos DB

A worldwide distributed database platform, Azure, gives developers several options and flexibility to create internationally distributed apps. With only some lines of HCL code, developers can quickly and easily install Cosmos DB resources and accounts.

This article will demonstrate how to launch a straightforward app in Azure. In order to accomplish this, we will use Terraform for the deployment of applications to Azure Container Instances from Azure Cosmos DB. 

Firstly, we’ll discover how to set up an Azure Cosmos DB instance. Next, we’ll modify the setup to configure an Azure Container instance. Lastly, we’ll build a straightforward app that leverages both of these services.

service disabled veteran owned small business

SERVICE DISABLED VETERAN OWNED SMALL BUSINESS (SDVOSB)

Things You Will Need

You will require a few things in order to comply with the instructions in this article:

  • A subscription to Azure
  • either utilizes the Cloud Shell on Azure having to Terraform integrated into or has Terraform locally installed.
  • To connect Terraform to Azure, if you are using it locally, you must also have Azure CLI downloaded.
  • Your chosen text editor

Now, let’s move on to the steps.

Step 1 – Initial Set Up of the System

Presuming you have all you require to get started, the following step is to set up Terraform to employ the Azure provider for your configurations. You must add a file named main.tf to the directory you designate for your application.

Then you have to specify the providers you will be used for configuration in the main.tf file.

After the providers have been specified, our initial configuration will require us to define three resources. 

The resource group is the first, and it will link us to all other resources. In order to create a random integer for our Azure Cosmos DB Account settings, we must utilize the random provider in the following resource block. The Azure Cosmos DB Account can then be defined.

We must define the following parameters in the resource block for the Azure Cosmos DB Account:

  • Name – should be unique and include a random integer.
  • Resource group location 
  • Resource group name
  • Consistency policy
  • Kind
  • Geolocation
  • Offer type

Now we have to set up and build our Cosmos DB instance after we’ve specified our three resources.

Unlock the future of intelligent applications with our cutting-edge Generative AI integration services!

Step 2 – Apply the Configuration

The first step involves setting up our existing code so that we can download the provider plugins that are required for our application. To do this, we simply issue this command:

$ terraform init

Then, we will execute the following function to verify our configuration and ensure that our code has the proper syntax:

$ terraform validate

After making sure that our settings are correct, we can then test it out by executing the plan function:

$ terraform plan

At last, we can use our setup if everything looks to be in order and appears to be capable of performing the intended functions:

$ terraform apply

After the apply process completes, we will need a Cosmos DB instance in Azure. Now that our setup has been updated, we can start using our new instance.

Step 3 – The Configuration Must be Updated Using a Container Instance

Afterward, this container instance will make use of the Cosmos DB instance, which in turn makes use of a docker container running a simple web app for casting votes.

For the container instance, we will install a resource block. We must specify the following parameters in the resource block for the Azure Container Group:

  • Name 
  • DNS name label
  • IP address 
  • Type
  • Resource group location
  • Secure environment variables
  • OS type
  • Name of the resource group
  • Container 

There are some crucial points to make in this case.

First off, you’ll see that we’re requesting an image from the Microsoft container image database in our container parameter. Using a Cosmos DB backend, we use the frontend voting app.

We are creating an environment variable for every one of the two environment variables we pulled from the newly established Cosmos DB instance.

The database’s main master key is then pulled, as well as the environment parameter COSMOS DB MASTERKEY is set after we have established the COSMOS DB ENDPOINT configuration file for our network. The container can access and utilize the database instance by using these two configuration settings, which are the keys to the databases. The app won’t function without these.

Additionally, you’ll see that we’ve specified an output variable that will show the app endpoint, so we are able to access it.

The changes may now be deployed to the configuration. To ensure that our code’s grammar is proper, we must check the configuration:

$ terraform validate

We’ll then verify the configuration in advance:

$ terraform plan

Lastly, implement the configuration:

Test the Application

We now have our app endpoint, which was shown in the output in the terminal, and the resources have already been uploaded. Using your browser, you can access the app by entering the address shown in the output.

Step 4 – Successful Deployment

After following these simple steps and very easy Terraform coding, you will be able to successfully deploy an application in Azure.

Small Disadvantaged Business

Small Disadvantaged Business

Small Disadvantaged Business (SDB) provides access to specialized skills and capabilities contributing to improved competitiveness and efficiency.

Wrap Up

The open-source software, Terraform, makes it very easy to manage and create a cloud architecture. Terraform allows us to automate network management, which gives developers the ability to see what will be deployed prior to it being done. Terraform supports a wide range of clouds, giving us dependable infrastructural deployment tools.

We hope the article above helped you understand the ease with which an application can be deployed in Azure using Terraform and Cosmos DB.

Further blogs within this Steps for Deploying an Application in Azure using Terraform and Cosmos DB category.

Frequently Asked Questions