Skip to content

Commit e344e20

Browse files
committed
Use azurerm as remote backend
1 parent a460f58 commit e344e20

13 files changed

+115
-37
lines changed

Diff for: .gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Local .terraform directories
2-
**/.terraform/*
2+
**/.terraform
33

44
# .tfstate files
55
*.tfstate
@@ -27,3 +27,5 @@ override.tf.json
2727

2828
# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
2929
# example: *tfplan*
30+
31+
*.conf

Diff for: README.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
# terraform-basics
2+
23
A git repository that I use to try terraform.
4+
5+
## Configuring the Remote Backend to use Azure Storage with Terraform
6+
7+
Use Microsoft Azure Storage to create a Remote Backend for Terraform to store the state file and lock the file to avoid mistakes or damage the existing infrastructure.
8+
9+
Use Terraform to create the storage account in Azure Storage.

Diff for: backend.tf

-9
This file was deleted.

Diff for: computes.tf

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
resource "azurerm_linux_virtual_machine" "vm" {
22
name = "tutorial-vm"
3-
location = azurerm_resource_group.rg.location
4-
resource_group_name = azurerm_resource_group.rg.name
3+
location = azurerm_resource_group.vm-rg.location
4+
resource_group_name = azurerm_resource_group.vm-rg.name
55
size = "Standard_D2ads_v5"
66
admin_username = var.vm_admin_username
77
network_interface_ids = [

Diff for: main.tf

-1
This file was deleted.

Diff for: networks.tf

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
# Create a virtual network and a subnet
22
resource "azurerm_virtual_network" "vnet" {
33
name = "tutorial-vnet"
4-
location = azurerm_resource_group.rg.location
5-
resource_group_name = azurerm_resource_group.rg.name
4+
location = azurerm_resource_group.vm-rg.location
5+
resource_group_name = azurerm_resource_group.vm-rg.name
66
address_space = ["10.0.0.0/16"]
77
tags = var.tags
88
}
99

1010
resource "azurerm_subnet" "subnet" {
1111
name = "tutorial-subnet"
12-
resource_group_name = azurerm_resource_group.rg.name
12+
resource_group_name = azurerm_resource_group.vm-rg.name
1313
virtual_network_name = azurerm_virtual_network.vnet.name
1414
address_prefixes = ["10.0.1.0/24"]
1515
}
1616

1717
resource "azurerm_public_ip" "vm_public_ip" {
1818
name = "vm-public-ip"
19-
location = azurerm_resource_group.rg.location
20-
resource_group_name = azurerm_resource_group.rg.name
19+
location = azurerm_resource_group.vm-rg.location
20+
resource_group_name = azurerm_resource_group.vm-rg.name
2121
allocation_method = "Dynamic"
2222
tags = var.tags
2323
}
2424

2525
resource "azurerm_network_interface" "nic" {
2626
name = "vm-nic"
27-
location = azurerm_resource_group.rg.location
28-
resource_group_name = azurerm_resource_group.rg.name
27+
location = azurerm_resource_group.vm-rg.location
28+
resource_group_name = azurerm_resource_group.vm-rg.name
2929

3030
ip_configuration {
3131
name = "internal"

Diff for: outputs.tf

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
## VM Outputs
2+
13
output "resource_group_id" {
2-
value = azurerm_resource_group.rg.id
4+
value = azurerm_resource_group.vm-rg.id
35
}
46

57
output "tls_private_key" {
@@ -10,3 +12,18 @@ output "tls_private_key" {
1012
output "vm_public_ip" {
1113
value = azurerm_public_ip.vm_public_ip.ip_address
1214
}
15+
16+
17+
## State Outputs
18+
19+
output "terraform_state_resource_group_name" {
20+
value = azurerm_resource_group.state-rg.name
21+
}
22+
23+
output "terraform_state_storage_account" {
24+
value = azurerm_storage_account.state-sta.name
25+
}
26+
27+
output "terraform_state_storage_container_core" {
28+
value = azurerm_storage_container.core-container.name
29+
}

Diff for: providers.tf

+7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ terraform {
77
}
88
}
99

10+
backend "azurerm" {
11+
resource_group_name = "tutorial-tfstate-rg"
12+
storage_account_name = "tutorialtfhj8tetzd"
13+
container_name = "tutorial-core-tfstate"
14+
key = "core.tutorial.tfstate"
15+
}
16+
1017
required_version = ">= 1.1.0"
1118
}
1219

Diff for: resource-groups.tf

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
resource "azurerm_resource_group" "rg" {
1+
resource "azurerm_resource_group" "vm-rg" {
22
name = var.resource_group_name
33
location = var.location
44
tags = var.tags
55
}
66

77
resource "azurerm_network_security_group" "nsg" {
88
name = "tutorial-network-security-group"
9-
location = azurerm_resource_group.rg.location
10-
resource_group_name = azurerm_resource_group.rg.name
9+
location = azurerm_resource_group.vm-rg.location
10+
resource_group_name = azurerm_resource_group.vm-rg.name
1111

1212
security_rule {
1313
name = "allow-ssh"

Diff for: state-storage.tf

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Create a Resource Group for the Terraform state file
2+
resource "azurerm_resource_group" "state-rg" {
3+
name = "${lower(var.company)}-tfstate-rg"
4+
location = var.location
5+
tags = var.tags
6+
7+
lifecycle {
8+
prevent_destroy = true
9+
}
10+
}
11+
12+
# Generate a random storage name
13+
resource "random_string" "state-sta-name" {
14+
length = 8
15+
upper = false
16+
numeric = true
17+
lower = true
18+
special = false
19+
}
20+
21+
# Create a Storage Account for the Terraform state file
22+
resource "azurerm_storage_account" "state-sta" {
23+
name = "${lower(var.company)}tf${random_string.state-sta-name.result}"
24+
resource_group_name = azurerm_resource_group.state-rg.name
25+
location = azurerm_resource_group.state-rg.location
26+
account_kind = "StorageV2"
27+
account_tier = "Standard"
28+
access_tier = "Hot"
29+
account_replication_type = "ZRS"
30+
enable_https_traffic_only = true
31+
tags = var.tags
32+
33+
lifecycle {
34+
prevent_destroy = true
35+
}
36+
}
37+
38+
# Create a Storage Container for the state file
39+
resource "azurerm_storage_container" "core-container" {
40+
name = "${lower(var.company)}-core-tfstate"
41+
storage_account_name = azurerm_storage_account.state-sta.name
42+
}

Diff for: storage.tf

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
resource "random_id" "rg_storage_account" {
2-
keepers = {
3-
resource_group = azurerm_resource_group.rg.name
4-
}
5-
byte_length = 8
1+
2+
# Generate a random storage name
3+
resource "random_string" "vm-sta-name" {
4+
length = 8
5+
upper = false
6+
numeric = true
7+
lower = true
8+
special = false
69
}
7-
resource "azurerm_storage_account" "storage_account" {
8-
name = "storage${random_id.rg_storage_account.hex}"
9-
location = azurerm_resource_group.rg.location
10-
resource_group_name = azurerm_resource_group.rg.name
10+
11+
resource "azurerm_storage_account" "vm-sta" {
12+
name = "${lower(var.company)}tf${random_string.vm-sta-name.result}"
13+
location = azurerm_resource_group.vm-rg.location
14+
resource_group_name = azurerm_resource_group.vm-rg.name
1115
account_tier = "Standard"
1216
account_replication_type = "LRS"
1317
tags = var.tags

Diff for: terraform.tfvars

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
resource_group_name = "my-rg"
1+
resource_group_name = "vm-rg"
2+
company = "tutorial"
23
location = "centralus"
34
tags = {
45
environment = "dev"

Diff for: variables.tf

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
variable "resource_group_name" {
22
type = string
3-
default = "tutorial-rg"
3+
default = "vm-rg"
4+
}
5+
6+
# company
7+
variable "company" {
8+
type = string
9+
description = "This variable defines the name of the company"
10+
default = "tutorial"
411
}
512

613
variable "location" {
7-
type = string
8-
default = "westus"
14+
type = string
15+
description = "Azure region where the resource group will be created"
16+
default = "westus"
917
}
1018

1119
variable "tags" {

0 commit comments

Comments
 (0)