Skip to content

najgel/terraform-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

terraform-demo

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:

Prerequisits

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.

Preparation

  • 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.

The Demo

  • 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.

initialize terraform

init

The terraform init command is used to initialize a working directory containing Terraform configuration files.

terraform init

Create workspaces

workspace

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\

Validate the configuration

validate

The terraform validate command is used to validate the syntax of the terraform files.

terraform validate -var-file="myvars.tfvars"

Plan for the creation of the resources in dev

plan

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

Apply the plan to dev

apply

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\

Test workspace

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!

Remove the resources in test

When we are done in test, lets remove the resources we created.

terraform destroy -var-file="myvars.tfvars" -force

Switch to workspace dev

destroy

terraform workspace select dev

Optional - change resource configuration and apply plan

  • 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.

Clean up

Lets remove all resources and all workspaces

terraform workspace delete test
terraform destroy -var-file="myvars.tfvars" -force
terraform workspace delete dev

About

No description or website provided.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages