Commit 53f2b391 authored by Santan Thottempudi's avatar Santan Thottempudi

Initial commit

parents
terraform {
backend "azurerm" {
resource_group_name = "myTerra-rg"
storage_account_name = "tfstatem6w16"
container_name = "tfstate"
key = "terraform.tfstate"
}
}
\ No newline at end of file
terraform {
required_version = ">=0.12"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~>2.0"
}
}
}
provider "azurerm" {
features {}
subscription_id = "b7200e1d-3736-4c80-8e7a-8105f4ba4646"
}
resource "azurerm_resource_group" "rg" {
name = "myTerra-rg"
location = "North Europe"
}
#Generate a random vm name
resource "random_string" "vm-name" {
length = 12
upper = false
number = false
lower = true
special = false
}
# Machine Name
locals{
vm-name = "${random_string.vm-name.result}-vm"
}
# Create virtual network
resource "azurerm_virtual_network" "myterraformnetwork" {
name = "myTerraVnet"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
}
# Create subnet
resource "azurerm_subnet" "myterraformsubnet" {
name = "myTerraSubnet"
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.myterraformnetwork.name
address_prefixes = ["10.0.1.0/24"]
}
# Create public IPs
resource "azurerm_public_ip" "myterraformpublicip" {
name = "myTerraPublicIP"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
allocation_method = "Dynamic"
}
# Create Network Security Group and rule
resource "azurerm_network_security_group" "myterraformnsg" {
name = "myTerraNetworkSecurityGroup"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.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 = "*"
}
}
# Create network interface
resource "azurerm_network_interface" "myterraformnic" {
name = "myNIC"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
ip_configuration {
name = "myNicConfiguration"
subnet_id = azurerm_subnet.myterraformsubnet.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.myterraformpublicip.id
}
}
# Connect the security group to the network interface
resource "azurerm_network_interface_security_group_association" "example" {
network_interface_id = azurerm_network_interface.myterraformnic.id
network_security_group_id = azurerm_network_security_group.myterraformnsg.id
}
# Create (and display) an SSH key
resource "tls_private_key" "linux_key" {
algorithm = "RSA"
rsa_bits = 4096
}
# Create virtual machine
resource "azurerm_linux_virtual_machine" "myterraformvm" {
name = local.vm-name
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
network_interface_ids = [azurerm_network_interface.myterraformnic.id]
size = "Standard_D2as_v4"
computer_name = "mylinuxvm"
admin_username = "linuxusr"
# admin_password = "G@tormaster123"
disable_password_authentication = true
tags = {
environment = "dev"
created_by = "sthottem"
id = "nisum_ascend"
}
admin_ssh_key {
username = "linuxusr"
public_key = tls_private_key.linux_key.public_key_openssh
}
os_disk {
name = "${local.vm-name}-os-disk"
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "18.04-LTS"
version = "latest"
}
depends_on = [
azurerm_network_interface.myterraformnic,
tls_private_key.linux_key
]
provisioner "remote-exec" {
inline = [
"sudo apt-get -y install apache2"
]
#Connection Settings
connection {
type = "ssh"
user = "linuxusr"
private_key = tls_private_key.linux_key.private_key_openssh
host = self.public_ip_address
agent = false
}
}
}
resource "random_string" "resource_code" {
length = 5
special = false
upper = false
}
resource "azurerm_storage_account" "tfstate" {
name = "tfstate${random_string.resource_code.result}"
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
account_tier = "Standard"
account_replication_type = "LRS"
allow_blob_public_access = true
tags = {
environment = "staging"
}
}
resource "azurerm_storage_container" "tfstate" {
name = "tfstate"
storage_account_name = azurerm_storage_account.tfstate.name
container_access_type = "blob"
}
#Output
output "resource_group_name" {
value = azurerm_resource_group.rg.name
}
output "public_ip_address" {
value = azurerm_linux_virtual_machine.myterraformvm.public_ip_address
}
output "tls_private_key" {
value = tls_private_key.linux_key.private_key_pem
sensitive = true
}
\ No newline at end of file
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment