In this project, I have coded a Packer template and a Terraform template to deploy a customizable, scalable web server in Azure. The template is customizable because it is very generic for anyone to clone this repo and provide user specific arguments like subscription id, client id and secret to deploy the web server. The project is scalable , so the user can configure the number of virtual machines as per their need during the deployment.
-
Clone this repository
-
Create your infrastructure as code
-
Update this README to reflect how someone would use your code.
- Create an Azure Account
- Install the Azure command line interface
- Install Packer
- Install Terraform
-
Create Azure credentials
- Create a service principal with
az ad sp create-for-rbac
and output the credentials - Use the command
az ad sp create-for-rbac --query "{ client_id: appId, client_secret: password, tenant_id: tenant }"
- Replace the client_id , client_secret and tenent_id in the webserver.json
- Create a service principal with
-
Customizing and scaling the webserver
- The variable
num_of_vms
is set to default as 2 at variable.tf. It is scalable, thus feel free to change the number as per your requirement. - Other variables like
packer_image_name
,packer_resource_group
,tags
,resource_group
,location
can also be configured in this variable.tf file
- The variable
-
Create and deploy a policy definition to deny the creation of resources that donot have tags
- Create the Azure policy definition by running the shell script
create_az_policy_definition.sh
- Assign the policy definition using the Azure portal
- Verify the created policy via the Azure CLI using the command
az policy assignment list
- Create the Azure policy definition by running the shell script
-
Create a server image using packer
- Create an image resource group named PolicyRG by
az group create --location northeurope --name PolicyRG
- Use the required fields in packer template file webserver.json
- Build the packer image using the command
packer build webserver.json
- Use
az image list
to list out the images present - Use
az image delete -g packer-rg -n myPackerImage
to delete any existing packer image
- Create an image resource group named PolicyRG by
-
Create the infrastructure using terraform
- Create a terraform file main.tf and variable.tf
- Create a Resource Group
- Create a virtual network and a subnet on the virtual network
- Create a Network Security Group
- Create a Network Interface
- Create a Public IP
- Create a 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 declarative configuration is possible by using variable.tf file
-
Deploy all Azure resources
- Initializa the terraform using the command
terraform init
- See the plan by using the command
terraform plan -out solution.plan
- Apply the deployment using
terraform apply
- Initializa the terraform using the command
-
Deploy all Azure resources
- Destroy all the resources created by terraform using the command
terraform destroy
- Destroy the image built by the packer using the command
az image delete -g PolicyRG -n webserverPackerImage
- Destroy all the resources created by terraform using the command