This repo show how to use terraform to create a web application in azure, just to show of what terraform can do and its capabilities in Azure. Its a very lightweight demo, to keep the presentation time as short as possible. By following these instructions and running terraform on the plan you will get:
- A web app
- A web app service plan
The name of the resources contains a random number, this is to lessen the chance of them already existing, which would cause an error an ruin the demo.. :-)
I will in this document not go into detail what each command does, please checkout the following resources to learn more about terraform:
To run this demo you need to have the following software installed:
I would recommend installing it using Chocolatey.
You will also need a azure subscription.
- Edit the file "myVars.tfvars" and replace the "REPLACE_ME" text with something more suiting.
- Read through the files and make sure you understand them, nothing attracts the demo devil more than a clueless presenter.
- Check Azure portal to see what resources you have in azure.
- Ensure you have a valid connection to azure.
az login
If you have access to multiple subscriptions, check that you are on the correct by:
az account list --query "[][isDefault,id,name]" --output table
The subscription where "isDefault" is marked as true is the active subscription. If you want to change perform (substitute the xxxxx.. with the subscription guid you got above):
az account set --subscription "xxxxx-xxxxx-xxxxx-xxxxx-xxxxx"
Show the files:
´provider.tf´ - contains the provider configuration, we do not actually need it but its nice to know exactly which provider version the demo will run in.
´output.tf´ - Controls what resource information that will get written at the end.
´variables.tf´ - Contains the variable definitions.
´resources.tf´ - contains the resource configuration.
Note that the information these files could have been in one, terraform searches through all .tf files in a directory, we just separate this to make it more readable. See the terraform documentation for more information.
The terraform init command is used to initialize a working directory containing Terraform configuration files.
terraform init
First we create a dev workspace and then a test workspace.
terraform workspace new dev
terraform workspace new test
We then list the workspaces and selects the dev workspace
terraform workspace list
terraform workspace select dev
Check terraform state directories under .\terraform.tfstate.d\
The terraform validate command is used to validate the syntax of the terraform files.
terraform validate -var-file="myvars.tfvars"
The terraform plan command is used to create an execution plan. Terraform performs a refresh, unless explicitly disabled, and then determines what actions are necessary to achieve the desired state specified in the configuration files.
terraform plan -var-file="myvars.tfvars" -out dev.tfplan
-- Show The plan, compare with the slide
The terraform apply command is used to apply the changes required to reach the desired state of the configuration, or the pre-determined set of actions generated by a terraform plan execution plan.
Remember, we are now executing this in the dev environment!
terraform apply dev.tfplan
If you apply the plan to dev again, nothing happens, this is because terraform keeps track of the infrastructure in the statefile
terraform apply dev.tfplan
- Check output of terraform
- Check Azure portal to see the new resources
- Check terraform environment state file under
.\terraform.tfstate.d\dev\
Let's do the same thing for the test environment, so lets change to the test workspace, plan and apply the plan.
terraform workspace select test
terraform plan -var-file="myvars.tfvars" -out test.tfplan
terraform apply test.tfplan
Just like that we have now cloned our configuration into another workspace.
- Check output of terraform
- Check Azure portal - we now have two resource groups with resources in them!
When we are done in test, lets remove the resources we created.
terraform destroy -var-file="myvars.tfvars" -force
- Check Azure portal to see that they are gone
terraform workspace select dev
- Check the web app in Azure portal. The application setting "Meaningoflife" is set to 41
- Change the application setting inside resources.tf
- Run the commands to update the resources, check the output of the plan to see what will change
terraform plan -var-file="myvars.tfvars" -out dev.tfplan
terraform apply dev.tfplan
- Check Azure portal again, the new value is present in the application setting.
Lets remove all resources and all workspaces
terraform workspace delete test
terraform destroy -var-file="myvars.tfvars" -force
terraform workspace delete dev