filmov
tv
Create a Cosmos DB Instance in Azure with Terraform

Показать описание
In this video, we will cover these 4 objectives:
First, we will log into the Azure Portal, configure the Cloud Shell, and then download and run the setup script to setup our environment.
Second, we will import the resource group.
Third, we will create the configuration and deploy the Azure Cosmos DB instance.
And for the fourth objective, we will update our configuration with an application container instance that will use the database created in the previous step as a simple database-driven application that we will test in the browser.
Solution
Log into the Azure portal.
In the Azure Portal, click the Cloud Shell icon in the upper right. Take note of the Location for the lab environment.
Select Bash.
Click Show advanced settings.
If needed, change the Cloud Shell region to match the Location of the lab environment.
For Storage account, select Use existing.
For File share, select Create new and give it a name of terraform.
Click Create storage.
In the Cloud Shell, list the current directories:
ls
Change to the clouddrive directory:
cd clouddrive
Verify the script was downloaded successfully:
ls
Make the script executable:
Verify that the script ran successfully by listing the current sub-directories:
ls
Change to the terraformguru directory:
cd terraformguru/
List the contents of the directory:
ls
:q
vim cosmosDB.tf
:q
Initialize the working directory:
terraform init
Import the Resource Group into Terraform
Using az group list, find and copy everything within the quotes for the subscription id:
az group list
Using the resource name and label from the cosmosDB.tf file, as well as the subscription id that was just copied, import the Azure resource group:
Define the Cosmos DB Instance and Apply the Configuration
Open the cosmosDB.tf file:
vim cosmosDB.tf
In the Azure Portal, copy the Resource group name.
In the Cloud Shell, on line 2 of the cosmosDB.tf file, replace RESOURCE_GROUP_NAME with the Resource group name that was just copied.
On line 3, replace RESOURCE_GROUP_LOCATION with the location for the lab environment, using westus for West US, centralus for Central US, and so on.
Starting on line 11, add a new azurerm_cosmosdb_account resource using the following configuration:
resource "azurerm_cosmosdb_account" "super-vote" {
offer_type = "Standard"
kind = "GlobalDocumentDB"
consistency_policy {
consistency_level = "BoundedStaleness"
max_interval_in_seconds = 10
max_staleness_prefix = 200
}
geo_location {
location = "eastus"
failover_priority = 0
}
}
Save and exit the file:
ESC
:wq!
terraform fmt
terraform validate
terraform plan
terraform apply
Open the cosmosDB.tf file:
vim cosmosDB.tf
Starting on line 29, add a new azurerm_container_group resource using the following configuration:
resource "azurerm_container_group" "super-vote" {
name = "super-vote"
ip_address_type = "public"
dns_name_label = "super-vote"
os_type = "linux"
container {
name = "super-vote"
cpu = "0.5"
memory = "1.5"
ports {
port = 80
protocol = "TCP"
}
secure_environment_variables = {
"TITLE" = "Best Superhero!"
"VOTE1VALUE" = "Batman"
"VOTE2VALUE" = "Superman"
}
}
}
Save and exit the file:
ESC
:wq!
In the file, paste the following:
output "application_endpoint" {
}
Save and exit the file:
ESC
:wq!
terraform fmt
terraform validate
terraform plan
terraform apply
When the deployment is complete, copy the output within the quotes for the application_endpoint.
Use terraform state list to verify that the deployment was successful.
Test Your Application
In a new browser tab, navigate to the application_endpoint output URL copied earlier.
Test the application by casting your vote.
First, we will log into the Azure Portal, configure the Cloud Shell, and then download and run the setup script to setup our environment.
Second, we will import the resource group.
Third, we will create the configuration and deploy the Azure Cosmos DB instance.
And for the fourth objective, we will update our configuration with an application container instance that will use the database created in the previous step as a simple database-driven application that we will test in the browser.
Solution
Log into the Azure portal.
In the Azure Portal, click the Cloud Shell icon in the upper right. Take note of the Location for the lab environment.
Select Bash.
Click Show advanced settings.
If needed, change the Cloud Shell region to match the Location of the lab environment.
For Storage account, select Use existing.
For File share, select Create new and give it a name of terraform.
Click Create storage.
In the Cloud Shell, list the current directories:
ls
Change to the clouddrive directory:
cd clouddrive
Verify the script was downloaded successfully:
ls
Make the script executable:
Verify that the script ran successfully by listing the current sub-directories:
ls
Change to the terraformguru directory:
cd terraformguru/
List the contents of the directory:
ls
:q
vim cosmosDB.tf
:q
Initialize the working directory:
terraform init
Import the Resource Group into Terraform
Using az group list, find and copy everything within the quotes for the subscription id:
az group list
Using the resource name and label from the cosmosDB.tf file, as well as the subscription id that was just copied, import the Azure resource group:
Define the Cosmos DB Instance and Apply the Configuration
Open the cosmosDB.tf file:
vim cosmosDB.tf
In the Azure Portal, copy the Resource group name.
In the Cloud Shell, on line 2 of the cosmosDB.tf file, replace RESOURCE_GROUP_NAME with the Resource group name that was just copied.
On line 3, replace RESOURCE_GROUP_LOCATION with the location for the lab environment, using westus for West US, centralus for Central US, and so on.
Starting on line 11, add a new azurerm_cosmosdb_account resource using the following configuration:
resource "azurerm_cosmosdb_account" "super-vote" {
offer_type = "Standard"
kind = "GlobalDocumentDB"
consistency_policy {
consistency_level = "BoundedStaleness"
max_interval_in_seconds = 10
max_staleness_prefix = 200
}
geo_location {
location = "eastus"
failover_priority = 0
}
}
Save and exit the file:
ESC
:wq!
terraform fmt
terraform validate
terraform plan
terraform apply
Open the cosmosDB.tf file:
vim cosmosDB.tf
Starting on line 29, add a new azurerm_container_group resource using the following configuration:
resource "azurerm_container_group" "super-vote" {
name = "super-vote"
ip_address_type = "public"
dns_name_label = "super-vote"
os_type = "linux"
container {
name = "super-vote"
cpu = "0.5"
memory = "1.5"
ports {
port = 80
protocol = "TCP"
}
secure_environment_variables = {
"TITLE" = "Best Superhero!"
"VOTE1VALUE" = "Batman"
"VOTE2VALUE" = "Superman"
}
}
}
Save and exit the file:
ESC
:wq!
In the file, paste the following:
output "application_endpoint" {
}
Save and exit the file:
ESC
:wq!
terraform fmt
terraform validate
terraform plan
terraform apply
When the deployment is complete, copy the output within the quotes for the application_endpoint.
Use terraform state list to verify that the deployment was successful.
Test Your Application
In a new browser tab, navigate to the application_endpoint output URL copied earlier.
Test the application by casting your vote.