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
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.terraform/
*.tfstate
*.tfstate.*
*.tfvars
Comment thread
taniakolesnik marked this conversation as resolved.
*.tfvars.json
*.tfvars.*
override.tf
override.tf.json
Comment thread
taniakolesnik marked this conversation as resolved.
*_override.tf
*_override.tf.json
crash.log
crash.log.*
.terraform.lock.hcl
terraform.tfplan
terraform.tfplan.*
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 = "storageacctk19052026"
container_name = "tfstate"
key = "terraform.tfstate"
}
}
12 changes: 6 additions & 6 deletions install-app.sh
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#!/bin/bash

Comment thread
taniakolesnik marked this conversation as resolved.
# Script to silently install and start the todo web app on the virtual machine.
# Note that all commands bellow are without sudo - that's because extention mechanism
# Script to silently install and start the todo web app on the virtual machine.
Comment thread
taniakolesnik marked this conversation as resolved.
# Note that all commands bellow are without sudo - that's because extention mechanism
Comment thread
taniakolesnik marked this conversation as resolved.
# runs scripts under root user.

# install system updates and isntall python3-pip package using apt. '-yq' flags are
Comment thread
taniakolesnik marked this conversation as resolved.
# used to suppress any interactive prompts - we won't be able to confirm operation
Comment thread
taniakolesnik marked this conversation as resolved.
Comment thread
taniakolesnik marked this conversation as resolved.
# when running the script as VM extention.
# when running the script as VM extention.
apt-get update -yq
apt-get install python3-pip -yq

# Create a directory for the app and download the files.
mkdir /app
# Create a directory for the app and download the files.
mkdir /app
# make sure to uncomment the line bellow and update the link with your GitHub username
# git clone https://github.com/<your-gh-username>/azure_task_12_deploy_app_with_vm_extention.git
git clone https://github.com/taniakolesnik/azure_task_12_deploy_app_with_vm_extention.git
cp -r devops_todolist_terraform_task/app/* /app

# create a service for the app via systemctl and start the app
Expand Down
45 changes: 45 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 3.0"
}
}
}
Comment thread
taniakolesnik marked this conversation as resolved.

provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "name" {
name = var.azurerm_resource_group_name
location = var.location
}

module "network" {
source = "./modules/network"
location = var.location
azurerm_resource_group_name = var.azurerm_resource_group_name
azurerm_network_security_group_name = var.azurerm_network_security_group_name
azurerm_virtual_network_name = var.azurerm_virtual_network_name
azurerm_virtual_network_address_space = var.azurerm_virtual_network_address_space
subnet_name = var.subnet_name
subnet_prefix = var.subnet_prefix
azurerm_public_ip_name = var.azurerm_public_ip_name
dns_label = var.dns_label
}

module "compute" {
source = "./modules/compute"
location = var.location
subnet_id = module.network.subnet_id
vm_name = var.vm_name
vm_size = var.vm_size
public_ssh_key = var.public_ssh_key
}

module "storage" {
source = "./modules/storage"
azurerm_resource_group_name = var.azurerm_resource_group_name
location = var.location
}
Comment thread
taniakolesnik marked this conversation as resolved.
Comment thread
taniakolesnik marked this conversation as resolved.
63 changes: 63 additions & 0 deletions modules/compute/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
resource "azurerm_network_interface" "example" {
name = "${var.vm_name}-nic"
location = var.location
Comment thread
taniakolesnik marked this conversation as resolved.
resource_group_name = var.azurerm_resource_group_name

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

resource "azurerm_linux_virtual_machine" "example" {
name = var.vm_name
resource_group_name = var.azurerm_resource_group_name
location = var.location
size = var.vm_size
admin_username = "adminuser"
network_interface_ids = [
azurerm_network_interface.example.id,
]

admin_ssh_key {
username = "adminuser"
public_key = var.public_ssh_key
}

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

source_image_reference {
Comment thread
taniakolesnik marked this conversation as resolved.
publisher = "Canonical"
offer = "0001-com-ubuntu-server-jammy"
sku = "22_04-lts"
version = "latest"
}
}

resource "azurerm_virtual_machine_extension" "example" {
name = "hostname"
virtual_machine_id = azurerm_linux_virtual_machine.example.id
publisher = "Microsoft.Azure.Extensions"
type = "CustomScript"
type_handler_version = "2.0"

settings = jsonencode({
commandToExecute = <<-EOT
apt-get update -yq
apt-get install python3-pip git -yq

mkdir -p /app
git clone https://github.com/taniakolesnik/azure_task_12_deploy_app_with_vm_extention.git /tmp/apprepo
cp -r /tmp/apprepo/devops_todolist_terraform_task/app/* /app

mv /app/todoapp.service /etc/systemd/system/
systemctl daemon-reload
systemctl start todoapp
systemctl enable todoapp
EOT
Comment thread
taniakolesnik marked this conversation as resolved.
})
Comment on lines +40 to +62

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The task requirement explicitly states: 'VM Extension: use the CustomScript extension to execute install-app.sh script.' Currently, the extension has inline commands that duplicate the script content. Instead, use fileUris to reference the script (e.g., from a storage account blob URL) and commandToExecute = "bash install-app.sh" to execute it. This ensures the actual script file is used rather than duplicated inline commands.

}
Empty file added modules/compute/outputs.tf
Empty file.
34 changes: 34 additions & 0 deletions modules/compute/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
variable "azurerm_resource_group_name" {
description = "The name of the resource group"
default = "mate-azure-task-12"
Comment thread
taniakolesnik marked this conversation as resolved.
type = string
}

variable "location" {
description = "Resource location"
default = "uksouth"
type = string
}

variable "subnet_id" {
description = "subnet id"
type = string
}

variable "vm_name" {
description = "The name of the virtual machine"
default = "matebox"
type = string
}

variable "vm_size" {
description = "The size of the virtual machine"
default = "Standard_B1s"
type = string
}

variable "public_ssh_key" {
default = "linuxboxsshkey"
type = string
description = "SSH key"
Comment thread
taniakolesnik marked this conversation as resolved.
}
43 changes: 43 additions & 0 deletions modules/network/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

# network security group
resource "azurerm_network_security_group" "example" {
Comment thread
taniakolesnik marked this conversation as resolved.
name = var.azurerm_network_security_group_name
location = var.location
resource_group_name = var.azurerm_resource_group_name
}

# virtual network
resource "azurerm_virtual_network" "example" {
name = var.azurerm_virtual_network_name
location = var.location
resource_group_name = var.azurerm_resource_group_name
address_space = var.azurerm_virtual_network_address_space
}

# subnet
resource "azurerm_subnet" "default" {
name = var.subnet_name
resource_group_name = var.azurerm_resource_group_name
virtual_network_name = azurerm_virtual_network.example.name
address_prefixes = var.subnet_prefix
}

# assosiate subnet to network security group
resource "azurerm_subnet_network_security_group_association" "default" {
subnet_id = azurerm_subnet.default.id
network_security_group_id = azurerm_network_security_group.example.id
}

resource "random_integer" "dns" {
min = 1000
max = 9999
}

# public IP address
resource "azurerm_public_ip" "example" {
name = var.azurerm_public_ip_name
resource_group_name = var.azurerm_resource_group_name
location = var.location
allocation_method = "Dynamic"
domain_name_label = "${var.dns_label}${random_integer.dns.result}"
}
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.default.id
}

output "public_ip_address" {
value = azurerm_public_ip.example.ip_address
}

output "public_ip_fqdn" {
value = azurerm_public_ip.example.fqdn
}
43 changes: 43 additions & 0 deletions modules/network/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
variable "azurerm_resource_group_name" {
default = "mate-azure-task-12"
Comment thread
taniakolesnik marked this conversation as resolved.
type = string
}

variable "location" {
default = "uksouth"
type = string
}
Comment thread
taniakolesnik marked this conversation as resolved.

variable "azurerm_network_security_group_name" {
default = "defaultnsg"
type = string
}

variable "azurerm_virtual_network_name" {
default = "vnet"
type = string
}

variable "azurerm_virtual_network_address_space" {
default = ["10.0.0.0/16"]
type = list(string)
}
variable "subnet_name" {
default = "default"
type = string
}

variable "subnet_prefix" {
default = ["10.0.0.0/24"]
type = list(string)
}

variable "azurerm_public_ip_name" {
default = "linuxboxpip"
type = string
}

variable "dns_label" {
default = "matetask"
type = string
}
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" "example" {
name = "storageacctk19052026"
Comment thread
taniakolesnik marked this conversation as resolved.
resource_group_name = var.azurerm_resource_group_name
location = var.location
account_tier = "Standard"
account_replication_type = "LRS"
}

resource "azurerm_storage_container" "example" {
Comment thread
taniakolesnik marked this conversation as resolved.
name = var.azurerm_storage_container_name
storage_account_name = azurerm_storage_account.example.name
container_access_type = "private"
}
Empty file added modules/storage/outputs.tf
Empty file.
14 changes: 14 additions & 0 deletions modules/storage/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
variable "azurerm_resource_group_name" {
default = "mate-azure-task-12"
Comment thread
taniakolesnik marked this conversation as resolved.
type = string
}

variable "location" {
default = "uksouth"
type = string
}
Comment thread
taniakolesnik marked this conversation as resolved.

variable "azurerm_storage_container_name" {
default = "task-artifacts"
type = string
}
7 changes: 7 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
output "public_ip_address_name" {
value = module.network.public_ip_address
}

output "public_ip_fqdn" {
value = module.network.public_ip_fqdn
}
Loading
Loading