Delpoying Web Server in

Introduction

The infrastructure as code gives us a huge advantage in defining, deploying, updating and destroying our infrastructure. So to set up an image which contains our application for repeatable deployments, we will use packer to create the virtual machine images(in JSON format).

Terraform also expands on this by not only deploying virtual machines but also storage, networking and security entities across multiple infrastructures, clouds and vendors.

Based on these, this project will use a Packer template and a Terraform template to deploy a customizable, scalable web server in Azure.

Getting Started

  1. Clone this repository
  2. Create your infrastructure as code
  3. Create your resource group in Azure

Dependencies

  1. Create an Azure Account
  2. Install the Azure command line interface
  3. Install Packer
  4. Install Terraform

Instructions

Once you've collected your dependencies, to deploy the scalable web server in Azure we need:

  1. Deploy the packer image
  2. Deploy the infrastructure with Terraform template

πŸ“Œ Deploy the Packer Image

Packer is a server templating software. It will deploy virtual machines images. After deploying the virtual machines with the help of packer template, make sure to delete the packer images as it does not maintain the state.

πŸ”· Config Environment Variables πŸ”·

Go to the terminal and export the environment variables like below.

export ARM_CLIENT_ID=
export ARM_CLIENT_SECRET=
export ARM_SUBSCRIPTION_ID=

βœ”οΈ Get Subscription ID

  • Login into your azure account
  • Search and click "Subscriptions"
  • Select whichever subscriptions are needed
  • Click on the overview
  • Copy the Subscription Id

βœ”οΈ Get Client ID

  • Login into your azure account
  • Search and click "Azure Active Directory"
  • Click "App registrations" under the Manage tab
  • Click the application that you own
  • Copy the client ID

βœ”οΈ Get Client Secret

  • Login into your azure account
  • Search and click "Azure Active Directory"
  • Click "App registrations" under the Manage tab
  • Click the application that you own
  • Click the "Certificates & Secrets" under the Manage tab
  • Create a client secret as you need.

Once you have exported and config the environment variable, use printenv to check whether they are configured properly.

printenv

πŸ”· Deploy the Packer Image πŸ”·

Run the following command to deploy the packer image.

packer build server.json

packer output packer output

πŸ“ŒCreate and Update Azure Resouces with Terraform Template

πŸ”· Variables πŸ”·

To use variables for your main.tf, you can specify your variables like below in your vars.tf file.

variable "prefix" {
  description = "The prefix which should be used for all resources in this example"
  default = "udacity"
}

And in your main.tf, you can call the variables like

var.prefix

πŸ”· Deploy the Infrastructure Using Terraform πŸ”·

Now we come to deploy the resources using the Terraform template. One thing worth mentioning is that we have already created the resources group for our PackerImage, so we can't deploy the resource group with the same name. Instead, we need to import the existing resource group and then it will know which resource group to deploy. The similar command will be like:

terraform import azurerm_resource_group.main /subscriptions/{subsriptionId}/resourceGroups/{resourceGroupName}

In main.tf: The az availability set, platform_fault_domain_count = 2 has default value 5, so we need to specify it to 2. Run the following commands to deploy the infrastructure.

az login

az login output

Remember to copy the tenant id and export it to the environment like the last step. Then run the following to deploy the terraform template.

az policy definition create --name LinuxPasswordPolicy --subscription c28ed0f0-cbce-41fa-9cfa-f14da67960ac --mode all --rules policy.json

image

az policy assignment list

image

terraform plan -out solution.plan

terraform plan output

terraform apply

terraform output

Once you have deployed the infrastructure. You can go to the Azure portal to check the resources. Once you have finished, remember to destroy these resources.

terraform destroy
terraform show

After the deployment, remember to destroy the resources.

terraform destroy
```# deploy-web-server-in-azure