Skip to content
Open

Dev #56

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
1 change: 1 addition & 0 deletions .terraform/modules/modules.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Modules":[{"Key":"","Source":"","Dir":"."},{"Key":"network_module","Source":"./modules/network","Dir":"modules/network"},{"Key":"storage_module","Source":"./modules/storage","Dir":"modules/storage"},{"Key":"vm_module","Source":"./modules/compute","Dir":"modules/compute"}]}
25 changes: 25 additions & 0 deletions backend.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "3.105.0"
}
random = {
source = "hashicorp/random"
version = "~> 3.0"
}
}

backend "azurerm" {
resource_group_name = "mate-azure-task-12"
storage_account_name = "yourstorageaccount"
container_name = "tfstate"
key = "terraform.tfstate"
use_oidc = true
}
}

provider "azurerm" {
features {}
use_oidc = true
}
4 changes: 2 additions & 2 deletions install-app.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ apt-get install python3-pip -yq
# 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
cp -r devops_todolist_terraform_task/app/* /app
git clone https://github.com/dimak20/azure_task_12_deploy_app_with_vm_extention.git
cp -r azure_task_12_deploy_app_with_vm_extention/devops_todolist_terraform_task/app/* /app

# create a service for the app via systemctl and start the app
mv /app/todoapp.service /etc/systemd/system/
Expand Down
33 changes: 33 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
resource "azurerm_resource_group" "rg" {
name = var.resource_group_name
location = var.location
Comment on lines +1 to +3

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 requires creating all necessary resources, including the resource group. This data block fetches an existing resource group instead of creating a new one. You should use an azurerm_resource_group resource block to create the group managed by this Terraform configuration.

}
module "network_module" {
source = "./modules/network"
virtual_network_name = var.virtual_network_name
vnet_address_prefix = var.vnet_address_prefix
resource_group_name = var.resource_group_name
location = var.location
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 "storage_module" {
source = "./modules/storage"
resource_group_name = var.resource_group_name
location = var.location
storage_account_name = var.storage_account_name
storage_container_name = var.storage_container_name
}
module "vm_module" {
source = "./modules/compute"
resource_group_name = var.resource_group_name
location = var.location
vm_name = var.vm_name
vm_size = var.vm_size
ssh_key_public = var.ssh_key_public
subnet_id = module.network_module.subnet_id
pip_id = module.network_module.public_ip_id
}
57 changes: 57 additions & 0 deletions modules/compute/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
resource "azurerm_network_interface" "nic" {
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.pip_id
}
}

resource "azurerm_linux_virtual_machine" "main" {
name = var.vm_name
location = var.location
resource_group_name = var.resource_group_name
network_interface_ids = [azurerm_network_interface.nic.id]
size = var.vm_size

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

os_disk {
name = "${var.vm_name}-osdisk"
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}

computer_name = "matebox"
admin_username = "azureuser"

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

resource "azurerm_virtual_machine_extension" "api_install" {
name = "install-api"
virtual_machine_id = azurerm_linux_virtual_machine.main.id
publisher = "Microsoft.Azure.Extensions"
type = "CustomScript"
type_handler_version = "2.1"

settings = <<SETTINGS
{
"fileUris": ["https://raw.githubusercontent.com/dimak20/azure_task_12_deploy_app_with_vm_extention/main/install-app.sh"],
"commandToExecute": "bash install-app.sh"
}
SETTINGS
}
Empty file added modules/compute/outputs.tf
Empty file.
28 changes: 28 additions & 0 deletions modules/compute/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
variable "vm_name" {
description = "The name of the virtual machine."
type = string
}
variable "location" {
description = "The location of the virtual machine."
type = string
}
variable "resource_group_name" {
description = "The name of the resource group."
type = string
}
variable "subnet_id" {
description = "The ID of the subnet."
type = string
}
variable "pip_id" {
description = "The ID of the public ip address"
type = string
}
variable "vm_size" {
description = "The size of the virtual machine."
type = string
}
variable "ssh_key_public" {
description = "The public SSH key for the virtual machine."
type = string
}
81 changes: 81 additions & 0 deletions modules/network/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
resource "azurerm_virtual_network" "vnet" {
name = var.virtual_network_name
location = var.location
resource_group_name = var.resource_group_name
address_space = [var.vnet_address_prefix]
}
resource "azurerm_subnet" "internal" {
name = var.subnet_name
resource_group_name = var.resource_group_name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefixes = [var.subnet_address_prefix]
}
resource "azurerm_network_security_group" "nsg" {
name = var.network_security_group_name
location = var.location
resource_group_name = var.resource_group_name

security_rule {
name = "SSH"
priority = 1001
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "22"
source_address_prefix = "*"
destination_address_prefix = "*"
}
security_rule {
name = "HTTP"
priority = 1002
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "80"
source_address_prefix = "*"
destination_address_prefix = "*"
}
security_rule {
name = "HTTPS"
priority = 1003
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "443"
source_address_prefix = "*"
destination_address_prefix = "*"
}
security_rule {
name = "API"
priority = 1004
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "8080"
source_address_prefix = "*"
destination_address_prefix = "*"
}
}
resource "random_integer" "random_id" {
min = 1000
max = 9999
}

resource "azurerm_public_ip" "pip" {
name = var.public_ip_address_name
location = var.location
resource_group_name = var.resource_group_name
allocation_method = "Dynamic"
sku = "Basic"

domain_name_label = "${var.dns_label}-${random_integer.random_id.result}"
}

resource "azurerm_subnet_network_security_group_association" "nsg_assoc" {
subnet_id = azurerm_subnet.internal.id
network_security_group_id = azurerm_network_security_group.nsg.id
}
12 changes: 12 additions & 0 deletions modules/network/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
output "public_ip_id" {
value = azurerm_public_ip.pip.id
}
output "nsg_id" {
value = azurerm_network_security_group.nsg.id
}
output "subnet_id" {
value = azurerm_subnet.internal.id
}
output "public_ip_fqdn" {
value = azurerm_public_ip.pip.fqdn
}
36 changes: 36 additions & 0 deletions modules/network/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
variable "virtual_network_name" {
description = "The name of the virtual network."
type = string
}
variable "vnet_address_prefix" {
description = "The address space for the virtual network."
type = string
}
variable "resource_group_name" {
description = "The name of the resource group."
type = string
}
variable "location" {
description = "The location of the resources."
type = string
}
variable "subnet_name" {
description = "The name of the subnet."
type = string
}
variable "subnet_address_prefix" {
description = "The address prefix for the subnet."
type = string
}
variable "network_security_group_name" {
description = "The name of the network security group."
type = string
}
variable "public_ip_address_name" {
description = "The name of the public IP address."
type = string
}
variable "dns_label" {
description = "The DNS label for the public IP address."
type = string
}
11 changes: 11 additions & 0 deletions modules/storage/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
resource "azurerm_storage_account" "storage_account" {
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" "container" {
name = var.storage_container_name
storage_account_name = azurerm_storage_account.storage_account.name
}
6 changes: 6 additions & 0 deletions modules/storage/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
output "storage_account_name" {
value = azurerm_storage_account.storage_account.name
}
output "storage_container_name" {
value = azurerm_storage_container.container.name
}
16 changes: 16 additions & 0 deletions modules/storage/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
variable "location" {
description = "Location of the storage account"
type = string
}
variable "resource_group_name" {
description = "Name of the resource group"
type = string
}
variable "storage_account_name" {
description = "Name of the storage account"
type = string
}
variable "storage_container_name" {
description = "Name of the storage container"
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_fqdn" {
value = module.network_module.public_ip_fqdn
}

output "vm_name" {
value = var.vm_name
}
14 changes: 14 additions & 0 deletions terraform.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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 AAAAB3NzaC1yc2EAAAADAQABAAACAQDYmf9RefboNxShzSA8q80ZcBcV2yBGS3ejlnU+AuZ3Q62zgzRzmbAgvVSELvVd77TpQC/+6KyRQjOoU86tuNEVgZNrSJ5rLkrntExKCOb9mmhWKqquxSLkvPrG6HhnF/mjODERA0WxPe0ceYO2qX5W2WHonynEt2JpW3VArbFD0SkFM/zu4UySOEtTyQijYhQxGZ9+pGtWz1RQYTaYzetFN3g9L08P31ng1nrNs9FdYR50xmSkm+S8KsSR6gQTHF6F1OFxLL8APWL1/IJNf+xKDBLbFcjEPZnMvwkp04qjn3K/exHdLennInOmT0Z+Vnd4wqzXzRu+4LeQitiOJUAx+cZVLddolmHZU9hW+b5eBogdx3CozcFUUrZfhVIsJJFfZGbloDXXV8PlMOTWeDOU5/oA/9s3I0Vwls2FirSvP0M4MGo7/l6UfMM3F5GSwatrs5EXChrbQbl9Ru5FpMAx56v5VjClRwb0XhYZVCGCxYsbZaBRpE3YmfZhCkoA6Y2sk7578gnzEVDqWgPDiGGA+wpUuQVPiuuU9oX24QYrgxnq0m1834vgAOBUacfhQ6GKVGeuM6CJXciEh+x1gBkaAcf/D/8vF80cf50HNyExSDz8WgSsBWQa2KSMkHqP5hK2ZhoL6BtpEaWM5QeYGJ5z6cEq+7ghg9f3yzZEO7wxJQ== acernitro7@DESKTOP-DL5L0MS"
dns_label = "matetask"
storage_account_name = "dimak100task12"
storage_container_name = "task-artifacts"
Loading
Loading