Getting into Azure with Terraform
Tags: terraform, azure, azuredevops, newbie
Published: 2020-10-17 17:12:28

---
title: Getting into Azure with Terraform
published: true
description: First part of  series of 
tags: #terraform, #azure, #azuredevops, #newbie
---

# Terraform for beginers with Azure 

I originally posted this on my [personal blog site](https://www.jevason.com) where I have more posts explain how I got the site up and running.

---

# Working with Terraform

This post will discuss some of the basic concepts around using terraform and how it has been used to build this blog site.

## What is Terraform 

Terraform is an open source tool used to create, configure and maintain a wide variety of system infrastructure. 

In most cases it's used to manage systems built on the cloud, whether that's Azure, AWS or Google Cloud Platform (or a mixture!) but it can also be used to manage other things like Datadog amongst many more.

This is typically done via "providers" which can be thought of extensions to the base terraform tool with specific functionality to create resources.

In this example we will be using the Azure RM (resource manager) provider.

### Prerequisites

- Your gonna need an [Azure account](https://azure.microsoft.com/en-gb/free/) (if you don't have one already).
- You will also need the [terraform tool](https://www.terraform.io/downloads.html)

## How does it work

I won't profess to known the inner workings of Terraform, but I will go over what I know.

### It's all about state

State is how Terraform knows what you've currently got managed via the tool. As we will see later as the terraform tool is used it creates a .tfstate file which is basically a big block of JSON containing all of the knowledge about what you are intending to deploy or have deployed. It's also used by Terraform to work out what you haven't currently got managed under it's system. 

State plays a very important role and if the state file is deleted or becomes corrupted it can be a bit of a head ache!

State can be configured to either be a local file or stored externally, in the case of this site I had chosen to upload the state file to blob storage. It's all automated (probably a post for another day) so I don't have to overly think about losing the state.

### Command the helm - init, plan and apply

Terraform has a wide variety of commands to manage resources, but in my experience so far the main 3 I've been working with are `init`, `plan` and `apply`.

`init` initialises the directory ready for use with Terraform, it scans through the .tf files to do a sort of "pre assesment", here it determines what prodivders will be required and goes and fetches them.

It also determines the backend configuration i.e if it will keep the state file local or should use on refernced elsewhere. 

`plan` Is the second main command I use, as the name implies it generates a plan that will be executed by the `apply` command. Running the plan displays a list of changes that the Terraform has calculated, be it new resources or configuration denoted by `+`, changes to existing resources denoted with `~` or the "destruction" of resources denoted with the `-`. This is really useful for giving a visual check before you up and remove say a database. 

`apply` Happy with the plan? Hit apply. With this final command Terraform simply executes and creates, changes or destroys the resources. There really isn't anything more to it.

### Bonus command

`destroy` is probably worth mentioning, as it's useful when you're done with the resources and want to completely remove them.(Note it will only destroy the resources currently managed by Terraform).

## Creating the Resources

Now I'm going to go through some steps to spin up a serverless static website. 

### Getting set up

So you've got the azure account and downloaded Terraform, then you're ready to go.

Open up your favourite IDE and let's begin.

Firstly if you haven't already grab the resources on my [github](https://github.com/JEvason90/StaticBlog) then open a terminal in the src directory (I use VS Code).

I'll breifly explain the files. 

![Terraform files in VS code](https://www.jevason.com/images/post2img1.png "Terraform files in folder")

There is the main file, which specifies the version of the azurerm provider (terraform recommends sticking to a specific version, mainly for continuity purposes) as well as a resource group.

```
    provider "azurerm" {
    version = "=2.23.0"
    features {}
    }

    resource "azurerm_resource_group" "resource-group" {
    name     = "simple-terraform-app"
    location = "ukwest"
    }
```

The other file static_site contains the definitions for the static site

```
    resource "azurerm_storage_account" "static-site" {
    name                     = "simpleterraformsite" #This will need changing to a unique value
    resource_group_name      = azurerm_resource_group.resource-group.name
    location                 = azurerm_resource_group.resource-group.location
    account_tier             = "Standard"
    account_replication_type = "LRS"
    account_kind             = "StorageV2"

    enable_https_traffic_only = false

    static_website {
        index_document = "index.html"
        error_404_document = "404.html"
    }
    }
```

From here it's a case of running `terraform init` which should give results as below:

![Terraform init output](https://www.jevason.com/images/post2img2.png "Terraform init command output")

Next step is to run the `terraform plan` again the out put will bt similar to the below

![Terraform plan output](https://www.jevason.com/images/post2img3.png "Terraform plan command output")

Once that's done and dusted it's time to run `terraform apply` here terraform should display the plan but this time prompt you to confirm the plan, simply type yes.

You will then see more console output from terraform detailing the current completion state.

![Terraform apply command](https://www.jevason.com/images/post2img4.png "Terraform apply command output")
![Terraform apply command](https://www.jevason.com/images/post2img5.png "Terraform apply command output")

### Checking the resources

Now head over to Azure to confirm that the resource group has been created as expected.

![Azure resource group](https://www.jevason.com/images/post2img6.png "Azure resource group")

In this resource group you should find a single storage account resource

![Azure storage account](https://www.jevason.com/images/post2img7.png "Azure storage account")

If you then inspect the Static Website tab in the left hand panel of the resource you should be able to see details on the sites configuration

![Azure static website](https://www.jevason.com/images/post2img8.png "Azure static website")
![Azure static site details](https://www.jevason.com/images/post2img9.png "Azure static site details")

From here the next steps to get a working site are to upload the html files found in the repo.
Navigate to the container in the storage account and upload the files in the $web container.

![Static site web container](https://www.jevason.com/images/post2img10.png "Static site web container")

Once complete when you navigate to the web address of the blob container you should get the index page.

![Resultant Web page](https://www.jevason.com/images/post2img11.png "Resultant web page")

A static site hosted in blob is a low cost (practicaly free) way of creating a website in Azure. 

That said it's always a good idea to remove resources you don't need, so it you don't plan on keeping the site run `terraform destroy` and confirm to remove the resources

And that's it, hope you enjoyed.

## Where to find me

Best place would be [twitter](https://twitter.com/jonnyev5)