Activity 3: Create a ntier architecture in Azure contd
- So we need to create a database
- Manually creating a database
- While creating a database we need to provide
- database details
- server details
- credentials
- size
- Network endpoints
- Lets create a sql server Refer Here
- Lets create an azure sql database Refer Here
- Now apply the configuration. Refer Here for the changes done and check the resources in the resource group
- Now lets add a vnet network rule Refer Here and firewall rule Refer Here
- Lets try to use the following conditional expression to create a service endpoint for db-1 subnet
var.subnet_names[count.index] == "db-1"? ["Microsoft.sql"]: []
- Refer Here for the changes done.
Terraform plan
- All these days we were executing
terraform apply
to create infrastructure - Terraform apply command internally creates a plan and then executes the plan
- We can explicity create a plan
terraform plan -out <filename>
and thenterraform apply <filename>
Terraform Provisioners
- Terraform provisioners can execute specific actions on the local machine or on the remote machine in order to prepare server or other infrastructure objects.
- If you are using local machine provisioners we need not establish any connections, but if we are using remote machine provisioners we need connections
- To specify connections for remote machines terraform has a connection block Refer Here for the official docs
- Lets try to use remote-exec connection to execute a inline script on the linux server
- Terraform provisioner written with in resource will be executed only when that resource is created.
- Solution 1: is to taint the resource i.e. mark the resource for recreation.
- Resource can be marked to taint by execution
terraform taint <resourcetype>.<resourcename>
- Now if we execute terraform apply the resource will be destroyed and recreated.
- Execute Terraform apply
- Taint is designed to recreate the resource when you observe mismatches
- Note: before apply command is executed if you want to undo taint
terraform untaint <resourcetype>.<resourcename>
- This approach will execute the script only once while creation.
- Now lets assume you have written a shell script for deploying application & whenever you execute terraform apply you want to do provisioning as we might be deploying the new version of the application generated from recent build.
- Refer Here for the changes
- Solution 2: Use Terraform null resource in null provider.
- The null resource will be executed every time you execute apply command. Refer Here for null resource documentation
- Refer Here for the changes done to include the null provisioner
- Note we have used build_id as trigger, so whenever build_id changes the terraform will executed the provisioner
terraform apply -var "build_id=2" -auto-approve