File tree 13 files changed +115
-37
lines changed
13 files changed +115
-37
lines changed Original file line number Diff line number Diff line change 1
1
# Local .terraform directories
2
- ** /.terraform / *
2
+ ** /.terraform
3
3
4
4
# .tfstate files
5
5
* .tfstate
@@ -27,3 +27,5 @@ override.tf.json
27
27
28
28
# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
29
29
# example: *tfplan*
30
+
31
+ * .conf
Original file line number Diff line number Diff line change 1
1
# terraform-basics
2
+
2
3
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.
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1
1
resource "azurerm_linux_virtual_machine" "vm" {
2
2
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
5
5
size = " Standard_D2ads_v5"
6
6
admin_username = var. vm_admin_username
7
7
network_interface_ids = [
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1
1
# Create a virtual network and a subnet
2
2
resource "azurerm_virtual_network" "vnet" {
3
3
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
6
6
address_space = [" 10.0.0.0/16" ]
7
7
tags = var. tags
8
8
}
9
9
10
10
resource "azurerm_subnet" "subnet" {
11
11
name = " tutorial-subnet"
12
- resource_group_name = azurerm_resource_group. rg . name
12
+ resource_group_name = azurerm_resource_group. vm- rg. name
13
13
virtual_network_name = azurerm_virtual_network. vnet . name
14
14
address_prefixes = [" 10.0.1.0/24" ]
15
15
}
16
16
17
17
resource "azurerm_public_ip" "vm_public_ip" {
18
18
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
21
21
allocation_method = " Dynamic"
22
22
tags = var. tags
23
23
}
24
24
25
25
resource "azurerm_network_interface" "nic" {
26
26
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
29
29
30
30
ip_configuration {
31
31
name = " internal"
Original file line number Diff line number Diff line change
1
+ # # VM Outputs
2
+
1
3
output "resource_group_id" {
2
- value = azurerm_resource_group. rg . id
4
+ value = azurerm_resource_group. vm- rg. id
3
5
}
4
6
5
7
output "tls_private_key" {
@@ -10,3 +12,18 @@ output "tls_private_key" {
10
12
output "vm_public_ip" {
11
13
value = azurerm_public_ip. vm_public_ip . ip_address
12
14
}
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
+ }
Original file line number Diff line number Diff line change @@ -7,6 +7,13 @@ terraform {
7
7
}
8
8
}
9
9
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
+
10
17
required_version = " >= 1.1.0"
11
18
}
12
19
Original file line number Diff line number Diff line change 1
- resource "azurerm_resource_group" "rg" {
1
+ resource "azurerm_resource_group" "vm- rg" {
2
2
name = var. resource_group_name
3
3
location = var. location
4
4
tags = var. tags
5
5
}
6
6
7
7
resource "azurerm_network_security_group" "nsg" {
8
8
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
11
11
12
12
security_rule {
13
13
name = " allow-ssh"
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 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
6
9
}
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
11
15
account_tier = " Standard"
12
16
account_replication_type = " LRS"
13
17
tags = var. tags
Original file line number Diff line number Diff line change 1
- resource_group_name = " my-rg"
1
+ resource_group_name = " vm-rg"
2
+ company = " tutorial"
2
3
location = " centralus"
3
4
tags = {
4
5
environment = " dev"
Original file line number Diff line number Diff line change 1
1
variable "resource_group_name" {
2
2
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"
4
11
}
5
12
6
13
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"
9
17
}
10
18
11
19
variable "tags" {
You can’t perform that action at this time.
0 commit comments