This repo contains my Terraform infrastucture as code that you can use to build multiple VM's, a load balancer with a custom Linux / Ubuntu image
To get started you will need the following.
- An Azure subscription Azure free sub
- A computer with Linux, Windows or Mac.
- A text editor such as Vscode Vscode
- Lots of coffee.
In order to use this code you will need the following
- Terraform Terraform latest version.
- The Azurerm provider (Instructions how to install on right corner)
- Azure CLI Azurecli latest version
- Packer, to creat images Packer latest version
- A git client (to clone the repo) git client
-
Clone this repo by typing "git clone https://github.com/Danxx26hub/iacCourseassign.git"
-
You will have a folder called iacCourseassign, cd into the this folder.
-
find the Packer json file and enter your credentials for Azure.
-
You will need to create a "Service Principal" account
-
You can find those using the Azure portal or the Azure CLI.
- to create SPN account do the following in the az cli.
- Create an Azure Resource Group : az group create -n resourceGroup --tags Environment = Production -l region
- you will need the following:
- the clientd ID
- client secret
- subscription ID
- run this az cli command: az ad sp create-for-rbac --query "{ client_id: appId, client_secret: password, tenant_id: tenant }"
- Then get subscription ID by running the following command: az account show --query "{ subscription_id: id }"
- Be sure to safeguard these id's and not share them with anyone.
- edit your "server.json" file and enter the three required id's
-
Initialize Packer by typing "Packer init .", rename the .json file to "server.json".
-
Run "Packer build -var 'resources=yourResource' server.json" this will build the image (Note you will need to use the -var switch, this allows you to set what resource group to put the image )
-
Once the image is built and stored in Azure you will see the following:
-
Type "Terraform init" to initialize your environment.
-
Type "Terraform plan -out soution.plan".
-
You can modify any of the variables in the variables.tf file to add or modify use the terraform "variable" block.
- Notice that variables that are defined in the variables.tf file can be called in the main.tf file by using "var.machines" for example.
- You can create your own variables and then call them in the main file.
- Notice that I used a conditional in the variable file to restrict from 2 to 5 machines. you can change that.
-
The default variables will ask you to pick between 2 and 5 VM's, I used Terraform validation to ensure this stays between that range and will fail if not.
-
you will be asked to enter a password for the machines (not great security), but this is just an example. You can also do ssh public key encryption.
-
You will be asked to enter a resource group prefix BE SURE TO use the same as you used in the Packer .json file.
-
-
This will do a "dry run" of the build process.
- you will be presented with any changes that terraform will be making.
- any addtions will have "+" symbols and changes or deletions will have "-" symbols
-
Once you are satisfied with the output type "Terraform apply project.out", this will build the number of VM's of your choice, the Load Balancer and the Network Security group.
-
Login to your Azure account and get your static IP, Enjoy.
You will have something like the image below in the Topology section of the Azure Portal, in my example I picked 3 VM's.