Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions backend.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
terraform {
backend "azurerm" {
resource_group_name = "mate-azure-task-12"
storage_account_name = "yourstorageaccount"
container_name = "tfstate"
key = "terraform.tfstate"
}
}
44 changes: 44 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
provider "azurerm" {
features {}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the task description, the Terraform configuration should create the Azure Resource Group. This resource definition is missing from the root module. You should add an azurerm_resource_group resource here and use its name when calling the modules below.

resource "azurerm_resource_group" "main" {
name = var.resource_group_name
location = var.location
}

module "network" {
source = "./modules/network"
location = var.location
resource_group_name = azurerm_resource_group.main.name
virtual_network_name = var.virtual_network_name
vnet_address_prefix = var.vnet_address_prefix
subnet_name = var.subnet_name
subnet_address_prefix = var.subnet_address_prefix
network_security_group_name = var.network_security_group_name
public_ip_address_name = var.public_ip_address_name
dns_label = var.dns_label
}

module "compute" {
source = "./modules/compute"
location = var.location
resource_group_name = azurerm_resource_group.main.name
subnet_id = module.network.subnet_id
public_ip_id = module.network.public_ip_id
vm_name = var.vm_name
vm_size = var.vm_size
ssh_key_public = var.ssh_key_public
}

module "storage" {
source = "./modules/storage"
location = var.location
resource_group_name = azurerm_resource_group.main.name
storage_account_name = "stor${random_integer.rand.result}"
}

resource "random_integer" "rand" {
min = 1000
max = 9999
}
Comment on lines +41 to +44

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This random_integer resource is a good way to ensure unique names. However, the network module also defines its own identical random_integer resource.

To avoid duplication and improve consistency, it's better to define this resource only once here in the root main.tf. You can then pass the result (random_integer.rand.result) as a variable to the network module, just as you've done for the storage module.

55 changes: 55 additions & 0 deletions modules/compute/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
resource "azurerm_network_interface" "this" {
name = "${var.vm_name}-nic"
location = var.location
resource_group_name = var.resource_group_name

ip_configuration {
name = "internal"
subnet_id = var.subnet_id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = var.public_ip_id
}
}

resource "azurerm_linux_virtual_machine" "this" {
name = var.vm_name
location = var.location
resource_group_name = var.resource_group_name
size = var.vm_size
admin_username = "azureuser"

network_interface_ids = [azurerm_network_interface.this.id]

admin_ssh_key {
username = "azureuser"
public_key = var.ssh_key_public
}

os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}

source_image_reference {
publisher = "Canonical"
offer = "0001-com-ubuntu-server-jammy"
sku = "22_04-lts"
version = "latest"
}
}

# VM Extension to deploy ToDo app
resource "azurerm_virtual_machine_extension" "install_app" {
name = "install-app"
virtual_machine_id = azurerm_linux_virtual_machine.this.id
publisher = "Microsoft.Azure.Extensions"
type = "CustomScript"
type_handler_version = "2.1"

settings = <<SETTINGS
{
"fileUris": ["https://raw.githubusercontent.com/chornoknysh/devops_todolist_terraform_task/main/install-app.sh"],
"commandToExecute": "bash install-app.sh"
}
SETTINGS
}
3 changes: 3 additions & 0 deletions modules/compute/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "vm_id" {
value = azurerm_linux_virtual_machine.this.id
}
7 changes: 7 additions & 0 deletions modules/compute/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
variable "location" {}
variable "resource_group_name" {}
variable "subnet_id" {}
variable "public_ip_id" {}
variable "vm_name" {}
variable "vm_size" {}
variable "ssh_key_public" {}
32 changes: 32 additions & 0 deletions modules/network/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
resource "azurerm_virtual_network" "this" {
name = var.virtual_network_name
address_space = [var.vnet_address_prefix]
location = var.location
resource_group_name = var.resource_group_name
}

resource "azurerm_subnet" "this" {
name = var.subnet_name
resource_group_name = var.resource_group_name
virtual_network_name = azurerm_virtual_network.this.name
address_prefixes = [var.subnet_address_prefix]
}

resource "azurerm_network_security_group" "this" {
name = var.network_security_group_name
location = var.location
resource_group_name = var.resource_group_name
}

resource "azurerm_public_ip" "this" {
name = var.public_ip_address_name
location = var.location
resource_group_name = var.resource_group_name
allocation_method = "Dynamic"
domain_name_label = "${var.dns_label}${random_integer.rand.result}"
}

resource "random_integer" "rand" {
min = 1000
max = 9999
}
Comment on lines +29 to +32

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This resource correctly generates a random number for the DNS label. However, there's a similar random_integer resource defined in the root main.tf.

To avoid duplication and centralize the random number generation, it's a best practice to remove this resource from the module. You can add a new variable to modules/network/variables.tf to accept the random number and pass it in from the root main.tf.

11 changes: 11 additions & 0 deletions modules/network/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
output "subnet_id" {
value = azurerm_subnet.this.id
}

output "public_ip_id" {
value = azurerm_public_ip.this.id
}

output "nsg_id" {
value = azurerm_network_security_group.this.id
}
9 changes: 9 additions & 0 deletions modules/network/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
variable "location" {}
variable "resource_group_name" {}
variable "virtual_network_name" {}
variable "vnet_address_prefix" {}
variable "subnet_name" {}
variable "subnet_address_prefix" {}
variable "network_security_group_name" {}
variable "public_ip_address_name" {}
variable "dns_label" {}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To follow the best practice of defining a resource only once, consider adding a new variable here to receive the random number generated in the root main.tf. For example:

variable "random_suffix" {
  type = number
}

You could then pass random_integer.rand.result from the root module to this new variable and remove the duplicate random_integer resource from this module's main.tf.

13 changes: 13 additions & 0 deletions modules/storage/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
resource "azurerm_storage_account" "this" {
name = var.storage_account_name
resource_group_name = var.resource_group_name
location = var.location
account_tier = "Standard"
account_replication_type = "LRS"
}

resource "azurerm_storage_container" "this" {
name = "task-artifacts"
storage_account_name = azurerm_storage_account.this.name
container_access_type = "private"
}
3 changes: 3 additions & 0 deletions modules/storage/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "storage_account_id" {
value = azurerm_storage_account.this.id
}
3 changes: 3 additions & 0 deletions modules/storage/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
variable "location" {}
variable "resource_group_name" {}
variable "storage_account_name" {}
Empty file added outputs.tf
Empty file.
12 changes: 12 additions & 0 deletions terraform.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
location = "uksouth"
resource_group_name = "mate-azure-task-12"
virtual_network_name = "vnet"
vnet_address_prefix = "10.0.0.0/16"
subnet_name = "default"
subnet_address_prefix = "10.0.0.0/24"
network_security_group_name = "defaultnsg"
public_ip_address_name = "linuxboxpip"
vm_name = "matebox"
vm_size = "Standard_B1s"
ssh_key_public = "ssh-rsa AAAAB3Nz...yourkey"
dns_label = "matetask"
47 changes: 47 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
variable "location" {
default = "uksouth"
}

variable "resource_group_name" {
default = "mate-azure-task-12"
}

variable "virtual_network_name" {
default = "vnet"
}

variable "vnet_address_prefix" {
default = "10.0.0.0/16"
}

variable "subnet_name" {
default = "default"
}

variable "subnet_address_prefix" {
default = "10.0.0.0/24"
}

variable "network_security_group_name" {
default = "defaultnsg"
}

variable "public_ip_address_name" {
default = "linuxboxpip"
}

variable "vm_name" {
default = "matebox"
}

variable "vm_size" {
default = "Standard_B1s"
}

variable "ssh_key_public" {
default = "ssh-rsa AAAAB3Nz...yourkey"
}

variable "dns_label" {
default = "matetask"
}
Loading