From 2538e21ffaedb0bf4a2f66afbbe62e8027aa2a77 Mon Sep 17 00:00:00 2001 From: directdevops Date: Tue, 2 Jun 2020 20:51:32 +0530 Subject: [PATCH 1/2] Added azure vms --- terraform_azurevms/main.tf | 145 ++++++++++++++++++++++++++++++++ terraform_azurevms/provider.tf | 8 ++ terraform_azurevms/variables.tf | 38 +++++++++ 3 files changed, 191 insertions(+) create mode 100755 terraform_azurevms/main.tf create mode 100755 terraform_azurevms/provider.tf create mode 100755 terraform_azurevms/variables.tf diff --git a/terraform_azurevms/main.tf b/terraform_azurevms/main.tf new file mode 100755 index 0000000..1a9ff93 --- /dev/null +++ b/terraform_azurevms/main.tf @@ -0,0 +1,145 @@ +resource "azurerm_resource_group" "terraform" { + location = var.location + name = var.groupname +} + +resource "azurerm_virtual_network" "vnet" { + resource_group_name = var.groupname + address_space = [var.vnetcidr] + location = var.location + name = local.network_name + depends_on = [azurerm_resource_group.terraform] + tags = local.common_tags + +} + +resource "azurerm_subnet" "subnets" { + count = length(var.subnetnames) + + resource_group_name = var.groupname + virtual_network_name = local.network_name + address_prefixes = [cidrsubnet(var.vnetcidr,8,count.index)] + name = var.subnetnames[count.index] + depends_on = [azurerm_resource_group.terraform, azurerm_virtual_network.vnet] + +} + +resource "azurerm_public_ip" "mypublicip" { + name = local.publicip_name + resource_group_name = var.groupname + location = var.location + allocation_method = "Dynamic" + tags = local.common_tags + depends_on = [azurerm_resource_group.terraform, azurerm_virtual_network.vnet] + +} + +resource "azurerm_network_security_group" "openall" { + name = local.nsgname + location = var.location + resource_group_name = var.groupname + tags = local.common_tags + security_rule { + name = "openalloutgoing" + access = "Allow" + destination_address_prefix = "*" + source_address_prefix = "*" + priority = 300 + direction = "Outbound" + protocol = "*" + source_port_range = "*" + destination_port_range = "*" + } + + security_rule { + name = "openallincoming" + access = "Allow" + destination_address_prefix = "*" + source_address_prefix = "*" + priority = 300 + direction = "Inbound" + protocol = "*" + source_port_range = "*" + destination_port_range = "*" + } + + depends_on = [ azurerm_public_ip.mypublicip] + +} + +resource "azurerm_network_interface" "vmnic" { + name = local.nicname + location = var.location + resource_group_name = var.groupname + ip_configuration { + name = "${local.nicname}ipconfig" + subnet_id = azurerm_subnet.subnets[0].id + private_ip_address_allocation = "Dynamic" + public_ip_address_id = azurerm_public_ip.mypublicip.id + } + tags = local.common_tags + + depends_on = [ azurerm_public_ip.mypublicip, azurerm_subnet.subnets] + +} + +resource "random_id" "storagerandom" { + keepers = { + # Generate a new ID only when a new resource group is defined + resource_group = azurerm_resource_group.terraform.name + } + + byte_length = 8 +} + +resource "azurerm_storage_account" "diagstorage" { + name = "qt${random_id.storagerandom.hex}" + resource_group_name = var.groupname + location = var.location + account_replication_type = "LRS" + account_tier = "Standard" + tags = local.common_tags + + depends_on = [ azurerm_public_ip.mypublicip, azurerm_subnet.subnets, random_id.storagerandom] + +} + +resource "azurerm_linux_virtual_machine" "qtvm" { + name = local.vmname + location = var.location + resource_group_name = var.groupname + size = "Standard_B1s" + admin_username = local.username + admin_password = var.password + disable_password_authentication = false + network_interface_ids = [azurerm_network_interface.vmnic.id] + source_image_reference { + publisher = "Canonical" + offer = "UbuntuServer" + sku = "18.04-LTS" + version = "latest" + } + + os_disk { + name = "myOsDisk" + caching = "ReadWrite" + storage_account_type = "Premium_LRS" + } + + tags = local.common_tags + + provisioner "remote-exec" { + inline = [ + "sudo apt-get update", + "sudo apt-get install apache2 -y" + ] + + + connection { + host = self.public_ip_address + user = self.admin_username + password = self.admin_password + } + } + +} \ No newline at end of file diff --git a/terraform_azurevms/provider.tf b/terraform_azurevms/provider.tf new file mode 100755 index 0000000..6565144 --- /dev/null +++ b/terraform_azurevms/provider.tf @@ -0,0 +1,8 @@ +provider "azurerm" { + client_id = "" + client_secret = "" + tenant_id = "" + subscription_id = "" + + features {} +} diff --git a/terraform_azurevms/variables.tf b/terraform_azurevms/variables.tf new file mode 100755 index 0000000..3611a1e --- /dev/null +++ b/terraform_azurevms/variables.tf @@ -0,0 +1,38 @@ +variable "groupname" { + type = string + default = "terraform" +} + +variable "location" { + type = string + default = "centralus" +} + +variable "vnetcidr" { + type = string + default = "192.168.0.0/16" +} + +variable subnetnames { + default = ["web", "app", "db", "management"] +} + +variable password { + default = "devops@qt123" +} + +locals { + network_name = "ntier" + publicip_name = "ntierpublic" + nsgname = "openall" + nicname = "vmnic" + vmname = "qtazure" + username = "qtdevops" +} + +locals { + common_tags = { + purpose = "learning" + created_by = "teraform" + } +} From 0dfec1c5546ffd411df1c9beb11bbb3ce1dad989 Mon Sep 17 00:00:00 2001 From: directdevops Date: Fri, 5 Jun 2020 21:55:59 +0530 Subject: [PATCH 2/2] Added workspace with backend --- terraform_workspace/azure.tf | 145 ++++++++++++++++++++++++++++ terraform_workspace/backend.tf | 8 ++ terraform_workspace/provider.tf | 4 + terraform_workspace/variables.tf | 64 ++++++++++++ terraform_workspace/vars/dev.tfvars | 14 +++ 5 files changed, 235 insertions(+) create mode 100755 terraform_workspace/azure.tf create mode 100755 terraform_workspace/backend.tf create mode 100755 terraform_workspace/provider.tf create mode 100755 terraform_workspace/variables.tf create mode 100755 terraform_workspace/vars/dev.tfvars diff --git a/terraform_workspace/azure.tf b/terraform_workspace/azure.tf new file mode 100755 index 0000000..98e6880 --- /dev/null +++ b/terraform_workspace/azure.tf @@ -0,0 +1,145 @@ +resource "azurerm_resource_group" "terraform" { + location = var.location + name = var.groupname +} + +resource "azurerm_virtual_network" "vnet" { + resource_group_name = var.groupname + address_space = [var.vnetcidr] + location = var.location + name = local.network_name + depends_on = [azurerm_resource_group.terraform] + tags = local.common_tags + +} + +resource "azurerm_subnet" "subnets" { + count = length(var.subnetnames) + + resource_group_name = var.groupname + virtual_network_name = local.network_name + address_prefixes = [cidrsubnet(var.vnetcidr,8,count.index)] + name = var.subnetnames[count.index] + depends_on = [azurerm_resource_group.terraform, azurerm_virtual_network.vnet] + +} + +resource "azurerm_public_ip" "mypublicip" { + name = local.publicip_name + resource_group_name = var.groupname + location = var.location + allocation_method = "Dynamic" + tags = local.common_tags + depends_on = [azurerm_resource_group.terraform, azurerm_virtual_network.vnet] + +} + +resource "azurerm_network_security_group" "openall" { + name = local.nsgname + location = var.location + resource_group_name = var.groupname + tags = local.common_tags + security_rule { + name = "openalloutgoing" + access = "Allow" + destination_address_prefix = "*" + source_address_prefix = "*" + priority = 300 + direction = "Outbound" + protocol = "*" + source_port_range = "*" + destination_port_range = "*" + } + + security_rule { + name = "openallincoming" + access = "Allow" + destination_address_prefix = "*" + source_address_prefix = "*" + priority = 300 + direction = "Inbound" + protocol = "*" + source_port_range = "*" + destination_port_range = "*" + } + + depends_on = [ azurerm_public_ip.mypublicip] + +} + +resource "azurerm_network_interface" "vmnic" { + name = local.nicname + location = var.location + resource_group_name = var.groupname + ip_configuration { + name = "${local.nicname}ipconfig" + subnet_id = azurerm_subnet.subnets[0].id + private_ip_address_allocation = "Dynamic" + public_ip_address_id = azurerm_public_ip.mypublicip.id + } + tags = local.common_tags + + depends_on = [ azurerm_public_ip.mypublicip, azurerm_subnet.subnets] + +} + +resource "random_id" "storagerandom" { + keepers = { + # Generate a new ID only when a new resource group is defined + resource_group = azurerm_resource_group.terraform.name + } + + byte_length = 8 +} + +resource "azurerm_storage_account" "diagstorage" { + name = "qt${random_id.storagerandom.hex}" + resource_group_name = var.groupname + location = var.location + account_replication_type = "LRS" + account_tier = "Standard" + tags = local.common_tags + + depends_on = [ azurerm_public_ip.mypublicip, azurerm_subnet.subnets, random_id.storagerandom] + +} + +resource "azurerm_linux_virtual_machine" "qtvm" { + name = local.vmname + location = var.location + resource_group_name = var.groupname + size = var.vmsize + admin_username = local.username + admin_password = var.password + disable_password_authentication = false + network_interface_ids = [azurerm_network_interface.vmnic.id] + source_image_reference { + publisher = "Canonical" + offer = "UbuntuServer" + sku = "18.04-LTS" + version = "latest" + } + + os_disk { + name = "myOsDisk" + caching = "ReadWrite" + storage_account_type = "Premium_LRS" + } + + tags = local.common_tags + + provisioner "remote-exec" { + inline = [ + "sudo apt-get update", + "sudo apt-get install apache2 -y" + ] + + + connection { + host = self.public_ip_address + user = self.admin_username + password = self.admin_password + } + } + +} \ No newline at end of file diff --git a/terraform_workspace/backend.tf b/terraform_workspace/backend.tf new file mode 100755 index 0000000..c614182 --- /dev/null +++ b/terraform_workspace/backend.tf @@ -0,0 +1,8 @@ +terraform{ + backend "azurerm" { + storage_account_name = "qtterraformstate" + container_name = "terraform" + key = "default.terraform.tfstate" + resource_group_name = "terraformstate" + } +} \ No newline at end of file diff --git a/terraform_workspace/provider.tf b/terraform_workspace/provider.tf new file mode 100755 index 0000000..66925a3 --- /dev/null +++ b/terraform_workspace/provider.tf @@ -0,0 +1,4 @@ +provider "azurerm" { + version = "2.13.0" + features {} +} diff --git a/terraform_workspace/variables.tf b/terraform_workspace/variables.tf new file mode 100755 index 0000000..5703698 --- /dev/null +++ b/terraform_workspace/variables.tf @@ -0,0 +1,64 @@ +variable "groupname" { + type = string + default = "terraform" +} + +variable "location" { + type = string + default = "centralus" +} + +variable "vnetcidr" { + type = string + default = "192.168.0.0/16" +} + +variable subnetnames { + default = ["web", "app", "db", "management"] +} + +variable password { + default = "devops@qt123" +} + +locals { + network_name = "ntier" + publicip_name = "ntierpublic" + nsgname = "openall" + nicname = "vmnic" + vmname = "qtazure" + username = "qtdevops" +} + +locals { + common_tags = { + purpose = "learning" + created_by = "teraform" + } +} + +variable vmsize { + default = "Standard_B1s" +} + +variable osdisksize { + default = "Premium_LRS" + +} + + +variable terraformazurebackend { + type = map + default = { + storage_account_name = "qtstoragefortfstate" + container_name = "terraform" + key = "default.terraform.tfstate" + resource_group_name = "terraformstate" + + } + +} + +locals { + terraformstatefile = "${terraform.workspace}.terraform.tfstate" +} diff --git a/terraform_workspace/vars/dev.tfvars b/terraform_workspace/vars/dev.tfvars new file mode 100755 index 0000000..11d1be8 --- /dev/null +++ b/terraform_workspace/vars/dev.tfvars @@ -0,0 +1,14 @@ +groupname = "terraformdev" +location = "centralus" +vnetcidr = "192.168.0.0/16" +subnetnames = ["web", "app", "db", "management"] +password = "devops@qt123" +vmsize = "Standard_B1s" +osdisksize = "Premium_LRS" +terraformazurebackend = { + storage_account_name = "qtstoragefortfstate" + container_name = "terraform" + key = "default.terraform.tfstate" + resource_group_name = "terraformstate" + +} \ No newline at end of file