Skip to main content

Chapter 29 - Demo: Static Website Part 1

A quick demo to deploy a static website as a module

Overview​

This will take you on a journey from nothing to manual creation in the portal to creating via Terraform without modules to creating in Terraform with modules.

1. Manual creation​


We do this to see what resources and settings that we need to use in our automated IaC

  1. Create a resource group manually
  2. Create a storage account manually
    • standard LRS
  3. Enable Static Website and upload an index.html file for testing.
  4. Access via the public endpoint and test

2. Terraform creation​


We need to create these files:

  1. versions.tf
  2. main.tf
  3. variables.tf
  4. outputs.tf
  5. terraform.tfvars

Init, validate, plan, apply, destroy.

versions.tf​


# Terraform Block
terraform {
required_version = ">= 1.9.0"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "< 4.0"
}
random = {
source = "hashicorp/random"
version = ">= 3.0"
}
}
}

main.tf​


# Provider Block
provider "azurerm" {
features {}
}

# Random String Resource
resource "random_string" "myrandom" {
length = 6
upper = false
special = false
number = false
}

# Create Resource Group
resource "azurerm_resource_group" "resource_group" {
name = var.resource_group_name
location = var.location
}

# Create Azure Storage account
resource "azurerm_storage_account" "storage_account" {
name = "${var.storage_account_name}${random_string.myrandom.id}"
resource_group_name = azurerm_resource_group.resource_group.name

location = var.location
account_tier = var.storage_account_tier
account_replication_type = var.storage_account_replication_type
account_kind = var.storage_account_kind

static_website {
index_document = var.static_website_index_document
error_404_document = var.static_website_error_404_document
}
}


variables.tf​


# Input variable definitions
variable "location" {
description = "The Azure Region in which all resources groups should be created."
type = string
}
variable "resource_group_name" {
description = "The name of the resource group"
type = string
}
variable "storage_account_name" {
description = "The name of the storage account"
type = string
}
variable "storage_account_tier" {
description = "Storage Account Tier"
type = string
}
variable "storage_account_replication_type" {
description = "Storage Account Replication Type"
type = string
}
variable "storage_account_kind" {
description = "Storage Account Kind"
type = string
}
variable "static_website_index_document" {
description = "static website index document"
type = string
}
variable "static_website_error_404_document" {
description = "static website error 404 document"
type = string
}

outputs.tf​


# Output variable definitions
output "resource_group_id" {
description = "resource group id"
value = azurerm_resource_group.resource_group.id
}
output "resource_group_name" {
description = "The name of the resource group"
value = azurerm_resource_group.resource_group.name
}
output "resource_group_location" {
description = "resource group location"
value = azurerm_resource_group.resource_group.location
}
output "storage_account_id" {
description = "storage account id"
value = azurerm_storage_account.storage_account.id
}
output "storage_account_name" {
description = "storage account name"
value = azurerm_storage_account.storage_account.name
}

terraform.tfvars​


location                          = "centralus"
resource_group_name = "myrg1"
storage_account_name = "duposstaticwebsite" # the random will add some characters here.
storage_account_tier = "Standard"
storage_account_replication_type = "LRS"
storage_account_kind = "StorageV2"
static_website_index_document = "index.html"
static_website_error_404_document = "error.html"

Next Steps: Part 2​


Part 2 continues in the next chapter, Chapter 30 - Demo-Static-Website-Part-2