Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution #13

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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 = "tfstate144"
container_name = "tfstate"
key = "terraform.tfstate"
}
}
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.105.0"
}
}
}

provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "main" {
name = "todolist-terraform-task"
location = "West Europe"
}

module "network" {
source = "./modules/network"
resource_group_name = azurerm_resource_group.main.name
location = azurerm_resource_group.main.location
dns_label_prefix = var.dns_label
virtual_network_name = var.virtual_network_name
vnet_address_prefix = var.vnet_address_prefix
}

module "compute" {

Choose a reason for hiding this comment

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

The same here

source = "./modules/compute"
location = azurerm_resource_group.main.location
resource_group_name = azurerm_resource_group.main.name
vm_name = var.vm_name
subnet_id = module.network.subnet_id
public_ip_id = module.network.public_ip_address
ssh_key = var.ssh_key
virtual_network_name = var.virtual_network_name
vnet_address_prefix = var.vnet_address_prefix
}

module "storage" {
source = "./modules/storage"
resource_group_name = azurerm_resource_group.main.name
location = azurerm_resource_group.main.location
storage_account_name = var.storage_account_name
}
66 changes: 66 additions & 0 deletions modules/compute/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
resource "azurerm_public_ip" "linuxboxpip" {
name = "linuxboxpip"
location = var.location
resource_group_name = var.resource_group_name
allocation_method = "Static"
}

resource "azurerm_network_interface" "vm-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 = azurerm_public_ip.linuxboxpip.id
}
}

resource "azurerm_linux_virtual_machine" "matebox" {
name = "matebox"
resource_group_name = var.resource_group_name
location = var.location
size = "Standard_B1s"
admin_username = "adminuser"
network_interface_ids = [
azurerm_network_interface.vm-nic.id,
]

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

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"
}
}

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

settings = <<SETTINGS
{
"fileUris": [
"https://lvkzxjncklvjn.blob.core.windows.net/task-artifacts/install-app.sh"
],
"script": "bash install-app.sh"
}
SETTINGS

depends_on = [azurerm_linux_virtual_machine.matebox]
}
14 changes: 14 additions & 0 deletions modules/compute/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
output "network_interface_id" {
description = "The ID of the network interface."
value = azurerm_network_interface.vm-nic.id
}

output "virtual_machine_id" {
description = "The ID of the virtual machine."
value = azurerm_linux_virtual_machine.matebox.id
}

output "vm_extension_id" {
description = "The ID of the VM extension."
value = azurerm_virtual_machine_extension.CustomScript.id
}
39 changes: 39 additions & 0 deletions modules/compute/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
variable "location" {
description = "The location where resources will be created."
type = string
}

variable "resource_group_name" {
description = "The name of the resource group."
type = string
}

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

variable "subnet_id" {
description = "The ID of the subnet."
type = string
}

variable "public_ip_id" {
description = "The ID of the public IP address."
type = string
}

variable "ssh_key" {
description = "The SSH public key for authentication."
type = string
}

variable "virtual_network_name" {
description = "The name of the virtual network"
type = string
}

variable "vnet_address_prefix" {
description = "The address prefix for the virtual network"
type = string
}
44 changes: 44 additions & 0 deletions modules/network/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
resource "azurerm_virtual_network" "vnet" {
name = var.virtual_network_name
address_space = [var.vnet_address_prefix]
location = var.location
resource_group_name = var.resource_group_name
}

resource "azurerm_subnet" "default" {
name = "default"
resource_group_name = var.resource_group_name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefixes = ["10.0.0.0/24"]
}

resource "azurerm_network_security_group" "defaultnsg" {
name = "defaultnsg"
location = var.location
resource_group_name = var.resource_group_name

security_rule {
name = "AllowSSH"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "22"
source_address_prefix = "*"
destination_address_prefix = "*"
}
}

resource "azurerm_public_ip" "linuxboxpip" {
name = "linuxboxpip"
resource_group_name = var.resource_group_name
location = var.location
allocation_method = "Static"
domain_name_label = "${var.dns_label_prefix}${random_integer.random.result}"
}

resource "random_integer" "random" {
min = 1000
max = 9999
}
29 changes: 29 additions & 0 deletions modules/network/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
output "vnet_id" {
description = "The ID of the virtual network."
value = azurerm_virtual_network.vnet.id
}

output "network_security_group_id" {
description = "The ID of the network security group."
value = azurerm_network_security_group.defaultnsg.id
}

output "public_ip_id" {
description = "The ID of the public IP address."
value = azurerm_public_ip.linuxboxpip.id
}

output "public_ip_address" {
description = "The public IP address value."
value = azurerm_public_ip.linuxboxpip.ip_address
}

output "public_ip_fqdn" {
description = "The fully qualified domain name (FQDN) of the public IP address."
value = azurerm_public_ip.linuxboxpip.fqdn
}

output "subnet_id" {
description = "The ID of the subnet."
value = azurerm_subnet.default.id
}
25 changes: 25 additions & 0 deletions modules/network/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
variable "location" {
description = "The location where resources will be created."
type = string
}

variable "resource_group_name" {
description = "The name of the resource group."
type = string
}

variable "dns_label_prefix" {
description = "The prefix for the DNS label."
type = string
default = "matetask"
}

variable "virtual_network_name" {
description = "The name of the virtual network"
type = string
}

variable "vnet_address_prefix" {
description = "The address prefix for the virtual network"
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" "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" "task_artifacts" {
name = "task-artifacts"
storage_account_name = azurerm_storage_account.storage_account.name
container_access_type = "private"
}
9 changes: 9 additions & 0 deletions modules/storage/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
output "storage_account_name" {
value = azurerm_storage_account.storage_account.name
description = "The name of the storage account."
}

output "storage_container_name" {
value = azurerm_storage_container.task_artifacts.name
description = "The name of the storage container."
}
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 "location" {
description = "The location where resources will be created."
type = string
}

variable "resource_group_name" {
description = "The name of the resource group."
type = string
}

variable "storage_account_name" {
description = "The name of the storage account."
type = string
}
34 changes: 34 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
output "virtual_network_id" {
description = "The ID of the virtual network."
value = module.network.vnet_id
}

output "subnet_id" {
description = "The ID of the subnet."
value = module.network.subnet_id
}

output "network_security_group_id" {
description = "The ID of the network security group."
value = module.network.network_security_group_id
}

output "public_ip_address" {
description = "The public IP address."
value = module.network.public_ip_address
}

output "virtual_machine_id" {
description = "The ID of the virtual machine."
value = module.compute.virtual_machine_id
}

output "vm_extension_id" {
description = "The ID of the VM extension."
value = module.compute.vm_extension_id
}

output "network_interface_id" {
description = "The ID of the network interface."
value = module.compute.network_interface_id
}
3 changes: 3 additions & 0 deletions terraform.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ssh_key = <<EOF
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCjSa8xOgez5hycgVIbUnCA4Sf+pYofWWSc0H5fGM8ahdhzQ+RxvncmkMhda14lRU5tjTjVtbNKiw+9sZsUpkMcGKH+UEUGIOwXN9NKnZPMGvzByWQragaqAj5vanlDaTdiUYgqD/Ydvhy3zI5ax+PuNr1MQBOZ5bgwlR6fWT71NvY/XQt4cEDE/03ypjwAg57scK6hUsWfYk9iIF9V78d09PyOjIiTi5sVqUFkOtvy02Okqpuvjhtsr7W4gySu46ftKosXJTkYqCkxP7PmWuiOsBWrRAx6ZOvv/IY+CofrMOUUiRMKH9tXD/4mTjlkMwYtVQ4WEZkXLZOo2aVNri0usERnPjEkDmRxEV3YqHxTDhPWZBFnK59QLyPoEpy3HXS1H3Fe3xBVPSMJTzzPwrFo090Lovm8QYveBScLARh1WNGY4PiZFX/tOuadamb7LXNKeSA1MCVKK64D6H9MQ6CK9/NpQYeGXtGfl7TtJkqoIVg+1tgMwqx7JK27KaQG7nu3zY22oFgpqeBxady95JuGWm7mtrEQKv6kJhtByVTZ7xdcqJ62/sn1hcSfGpGsIcLn8tjdr6Ry0o6xSfvRC6iIEzubmCHiUyf/BIH9c1Ab1PgfLA4A8ecPUtAx9rvwvOyM3x2iKsTcmahV/gE6wt8wl0+n4BY9epwNdlUADmvNQQ== hyakunosuke7@gmail.com
EOF
32 changes: 32 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
variable "vm_name" {
description = "The name of the virtual machine."
type = string
default = "matebox"
}

variable "dns_label" {
description = "The prefix for the DNS label."
type = string
default = "todolist-terraform-task"
}

variable "ssh_key" {
description = "The SSH public key for the VM."
type = string
}

variable "storage_account_name" {
description = "The name of the storage account."
type = string
default = "tfstatekagerou4649"
}

variable "virtual_network_name" {
description = "The name of the virtual network"
type = string
}

variable "vnet_address_prefix" {
description = "The address prefix for the virtual network"
type = string
}