Using this repo, you can deploy a scalable webserver in Azure using Infrastructure as a Code (IaC) techniques. The code uses HashiCorp's Terraform to deploy a webserver in Azure. The virtual machine template is created using Packer while the Microsoft's Azure CLI is used to deploy the virtual machine like a script. Let's start!
-
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
After fulfilling the dependencies, you can follow the steps below to deploy a webserver in Azure.
- Clone this repository using
git clone
- Open the Azure Cli and login to your Azure account using
az login
- Create a resource group using:
az group create --name udacity-project1 --location francecentral --tags "udacity=project1"
- Create and deploy the custom policy using:
az policy definition create --name "tagging-policy" --display-name "Tagging-policy" --description "Enforcing all resource to have tags" --rules "policy.json" --mode All
az policy assignment create --policy tagging-policy --name "tagging-policy" --display-name "Tagging policy" --description "Policy to enforce tagging on all resources in the subscription"
-
Verify the deployment using
az policy assignment list
. The output should be something like this: -
Packer needs some enviornment variables which you can obtain by running the following command:
az ad sp create-for-rbac --role Contributor --scopes /subscriptions/<subscription_id> --query "{ client_id: appId, client_secret: password}"
you can use these values inside the packer.json
file's variables section or you can import them into your .env
file using
export ARM_CLIENT_ID=<client_id>
export ARM_CLIENT_SECRET=<client_secret>
export ARM_SUBSCRIPTION_ID=<subscription_id>
-
Now built the packer template using
packer build .\server.json
.This will take sometime and you will see the image inside the resource group. You can use theaz image list
command to verify the deployment. Alrernatively you could also see on the Azure portal which will look something like this: -
Now, since the stage is ready, it's time to deploy the webserver. Use the following command to deploy the webserver:
terraform init
terraform import azurerm_resource_group.main /subscriptions/<subscription_id>/resourceGroups/udacity-project1
terraform plan -out solution.plan
terraform apply solution.plan
- To verify the deployment, use the following command:
terraform show
- To delete the deployment, use the following command:
terraform destroy
It will ask for a confirmation before deleting the deployment in a following manner: