For this project, I was required to write a Packer template and a Terraform template to deploy a customizable, scalable web server in Azure.
- Clone this repository
- Create a tagging policy for Azure
- Create a Packer file to server your image
- Create your infrastructure as code (IaC) using Terraform
- Deploy your IaC using Terraform
- Create an Azure Account
- Install the Azure command line interface
- Install Packer
- Install Terraform
- Write a policy definition to deny the creation of resources that do not have tags.
- Export your environment variables for
client_id
,client_secret
, andsubscription_id
. - Apply the policy definition to the subscription with the name "tagging-policy"
- Use
az policy assignment list
and to see the out of your policy
- Use an Ubuntu 18.04-LTS SKU as the base image
- Ensure the following in your provisions:
"inline": ["echo 'Hello, World!' > index.html",
"nohub busybox httpd -f -p 80 &" ],
"inline_shebang": "/bin/sh -x", "type": "shell"
- Ensure that the resource group you specify in Packer for the image is the same specified in Terraform
- Use
packer build server.json
to deploy the packer image
In your main.tf configure the following:
- Create a Resource Group
- Create a Virtual network and a subnet on that virtual network
- Create a Network Security Group. Ensure that you explicitly allow access to other VMs on the subnet and deny direct access from the internet
- Create a Network Interface
- Create a Public IP.
- Create a Load Balancer. Your load balancer will need a backend address pool and address pool association for the network interface and the load balancer
- Create a virtual machine availability set
- Create virtual machines. Make sure you use the image you deployed using Packer!
- Create managed disks for your virtual machines
- Ensure a variables file allows for customers to configure the number of virtual machines and the deployment at a minimum.
You can customize the values without hard-coding them into the main.tf
file, by using the vars.tf
file.
For example in the vars.tf file you can include:
variable "location" {
description = "The Azure Regin that all resources in the project should be created"
default = "eastus"
}
Can be used instead of hard-coding eastus and improves the reusability of the code.
Once finished use terraform plan -out solution.plan
to deploy the terraform template and review before you deploy.
After you confirm everything you can use terraform apply
to deploy all the resources to Azure.
When you are ready to remove everything you simply use terraform destroy
and to verify everything was deleted you can use terraform show
.