Repository for Learning DevOps Vm deployment with Terraform and Packer
With this example you will be able to write a Packer template and a Terraform template to deploy a customizable, scalable web server in Azure.
-
Clone this repository
-
Create your infrastructure as code
- Create an Azure Account
- Install the Azure command line interface
- Install Packer
- Install Terraform
This repository contains
- tagging-policy (personal/TaggingPolicy.json) -> thsi policy will ensure tag names while creating resources
- Packer template for creating ready image of Ubuntu 18.4.0 LTS (DevOpsLearning/personal/Packer/)
- Terraform template for cluster VM creation using existing Ubuntu 18.4.0 LTS packer image (DevOpsLearning/personal/terraform/main.tf,vers.tf)
- "solution.plan" as the plan output after runnuing "teraform-plan" command
Steps: Packer: After installation of packer test "packer -h" in the PowerSell to verify.
-
Using the server.json file change parameter as azure provided subscription:
"client_id": "{{env `ARM_CLIENT_ID`}}", "client_secret": "{{env `ARM_CLIENT_SECRET`}}", "subscription_id": "{{env `ARM_SUBSCRIPTION_ID`}}"
-
Provide desire provisiones defaults are given.
-
Open Powersell and run command
packer build server.json
This will create a Linux 18.4.0 LTs image in to azure subscription resource group. This image will be used later on by terraform script.
Policy:
-
Open PowerShell login to azure subscription using command (azure CLI also has to be installed):
az login - and follow the instruction
-
Using following command create the policy:
az policy assignment update --name tagging-policy --description "Policy enforcement for resource deployment" --display-name "Taggign policy to ensure environment and project tags on resource deployment"
-
Using following command assign the created policy:
policy assignment create --policy tagging-policy
-
Using following command list of policies can be shown:
az policy assignment list
Terraform:
-
After installation of terraform test "terraform -h" in the PowerShell to verify.
-
Using the "main.tf" change test as desire
-
If there is an existing resource group where the Vm needs to be installed. Following command needed to import existing resources:
terraform import azurerm_resource_group.p01-rg /subscriptions/xxx.xxxx.xxxx.xxxx/resourceGroups/<resource-group-name>
-
Run command following in the PowerSell or Azure CLI to intialize terraform:
terraform init / terraform init -upgrade for code modification
-
Run following command to validate writen code:
terraform validate
-
After validation rin following command to see the plan
terraform plan / terraform plan out solution.plan (to generate solution plan as file)
-
If there are no errors the Run following command to execute:
terraform apply
-
Run command "terraform destroy" to delete all created resources from azure subscription resource group. (to release all the booked resources $$$$ )
Variables:
In the file vers.tf contains all the predefine variables that will be used while executing terraform examples. values can be set using keywords defaults:
ip-addres = "/subscriptions/.../publicIPAddresses/TestPublicIp1"