Hey, Gurus! Welcome to the Create a Cosmos DB Instance in Azure with Terraform lab. In this lab, we will cover these 4 objectives:
1. First, we will log into the Azure Portal, configure the Cloud Shell, and then download and run the lab setup script to setup our lab.
1. Second, we will import the resource group.
1. Third, we will create the configuration and deploy the Azure Cosmos DB instance.
1. And for the fourth objective, we will update our configuration with an application container instance that will use the database instance created in the previous step, with a simple database-driven application that we will test to verify if our deployment was successful.
Learning Objectives
Successfully complete this lab by achieving the following learning objectives:
- Set Up Cloud Shell and the Lab Environment
In the Portal
- Go to the Azure Portal and log in using your lab credentials.
- Click the Cloud Shell icon next to the search bar in the Portal.
- Select Bash at the prompt.
- Click Show Advanced Settings.
- Set the Cloud Shell region to the same location as the resource group.
- Select the existing Resource group, and select Use Existing for the Storage account.
- In the File share section, choose Create new and enter "terraform".
- Click Attach storage.
- Download the
lab_6_setup.sh
script athttps://raw.githubusercontent.com/ACloudGuru/advanced-terraform-with-azure/main/lab_cosmosdb_resources/lab_6_setup.sh
. - Add execute permissions to the script.
- Run the lab_6_setup.sh script.
- Import the Resource Group
In the Cloud Shell
- In the Cloud Shell, review the
cosmosDB.tf
. - Make note of the resource group name and label at the top and then close the file.
- Run the
az group list
command to get the subscription id. - Import your resource group into Terraform using the resource group name, label, and subscription id.
- After the import, add the name and location of your resource group to the
cosmosDB.tf
file so it looks like the code below (fill in with your resource group and location):resource "azurerm_resource_group" "super-vote" { name = "<RESOURCE_GROUP>" location = "<LOCATION>" }
- Save the file.
- In the Cloud Shell, review the
- Deploy the Cosmos DB Instance
In the Cloud Shell
- Open the
cosmosDB.tf
file. - Define the Cosmos DB instance using the
azurerm_cosmosdb_account resource
with a label ofsuper-vote
. - Create the DB instance using the following arguments and values:
- name:
"tfex-cosmos-db-${random_integer.ri.result}"
- location:
azurerm_resource_group.super-vote.location
- resource_group_name:
azurerm_resource_group.super-vote.name
- offer_type:
"Standard"
- kind:
"GlobalDocumentDB"
- consistency_policy:
- consistency_level:
"BoundedStaleness"
- max_interval_in_seconds:
10
- max_staleness_prefix:
200
- consistency_level:
- geo_location:
- location:
eastus
- failover_priority:
0
- location:
- name:
- Save the changes.
- Apply the configuration.
- Open the
- Update the Config, Deploy the Changes, and Test the Application
In the Cloud Shell
- Open the
cosmosDB.tf
file. - Define the Container Instance using the
azurerm_container_group
with a label ofsuper-vote
. Create the containuer using the following arguments and values:- name:
"super-vote"
- location:
azurerm_resource_group.super-vote.location
- resource_group_name:
azurerm_resource_group.super-vote.name
- ip_address_type:
"public"
- dns_name_label:
"super-vote"
- os_type:
"linux"
- container::
- name:
"super-vote"
- image:
"mcr.microsoft.com/azuredocs/azure-vote-front:cosmosdb"
- cpu:
"0.5"
- memory:
"1.5"
- ports::
- port:
80
- protocol:
"TCP"
- port:
- name:
- secure_environment_variables::
- COSMOS_DB_ENDPOINT:
azurerm_cosmosdb_account.super-vote.endpoint
- COSMOS_DB_MASTERKEY:
azurerm_cosmosdb_account.super-vote.primary_master_key
- TITLE:
"Best Superhero!"
- VOTE1VALUE:
"Batman"
- VOTE2VALUE:
"Superman"
- COSMOS_DB_ENDPOINT:
- name:
- Save the changes.
- Create a file called
output.tf
and paste the following:output "application_endpoint" { value = azurerm_container_group.super-vote.fqdn }
- Save the changes.
- Apply the configuration
- After the apply completes, copy the dns output for the application endpoint and go to your browser.
- Paste the link in your browser and go to the endpoint.
- Test the functionality of the application by voting to confirm and complete the lab.
- Open the