Terraform - Build Azure VM and provision with shell script
Below will be performed
1. Create linux virtual machine
2. Provision virtual machine with shell script using remote-exec
Below is terraform code - main.tf
main.tf
provider "azurerm" {
# The "feature" block is required for AzureRM provider 2.x.
# If you're using version 1.x, the "features" block is not allowed.
version = "=2.0"
features {}
subscription_id = "xxxxxx"
client_id = "xxxxxxx"
client_secret = "xxxxxxxxxxxxx"
tenant_id = "xxxxxxxxxxxxxxxxx"
}
resource "azurerm_resource_group" "resgrp1" {
name = "ajdocker2"
location = "eastus"
tags = {
environment = "Aj project1"
}
}
resource "azurerm_virtual_network" "vnet1" {
name = "ajvnet1"
address_space = ["10.0.0.0/16"]
location = "eastus"
resource_group_name = azurerm_resource_group.resgrp1.name
tags = {
environment = "Aj project1"
}
}
resource "azurerm_subnet" "subnet1" {
name = "ajsubnet1"
resource_group_name = azurerm_resource_group.resgrp1.name
virtual_network_name = azurerm_virtual_network.vnet1.name
address_prefix = "10.0.2.0/24"
}
resource "azurerm_public_ip" "publicip1" {
name = "ajpub1"
location = "eastus"
resource_group_name = azurerm_resource_group.resgrp1.name
allocation_method = "Dynamic"
tags = {
environment = "Aj project1"
}
}
resource "azurerm_network_security_group" "nsg1" {
name = "ajnsg1"
location = "eastus"
resource_group_name = azurerm_resource_group.resgrp1.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 = "HTTPD"
priority = 1003
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "80"
source_address_prefix = "*"
destination_address_prefix = "*"
}
tags = {
environment = "Aj project1"
}
}
resource "azurerm_network_interface" "nic1" {
name = "ajnic1"
location = "eastus"
resource_group_name = azurerm_resource_group.resgrp1.name
ip_configuration {
name = "ajNicConfiguration"
subnet_id = "${azurerm_subnet.subnet1.id}"
private_ip_address_allocation = "Dynamic"
public_ip_address_id = "${azurerm_public_ip.publicip1.id}"
}
tags = {
environment = "Aj project1"
}
}
resource "azurerm_network_interface_security_group_association" "nsg_assoc" {
network_interface_id = azurerm_network_interface.nic1.id
network_security_group_id = azurerm_network_security_group.nsg1.id
}
resource "azurerm_linux_virtual_machine" "vm1" {
name = "ajvm1"
location = "eastus"
resource_group_name = azurerm_resource_group.resgrp1.name
network_interface_ids = [azurerm_network_interface.nic1.id]
size = "Standard_DS1_v2"
os_disk {
name = "myOsDisk"
caching = "ReadWrite"
storage_account_type = "Premium_LRS"
}
source_image_reference {
publisher = "openLogic"
offer = "CentOS"
sku = "7.3"
version = "latest"
}
computer_name = "ajvm1"
admin_username = "azureuser"
disable_password_authentication = true
admin_ssh_key {
username = "azureuser"
public_key = file("my_key.pub")
}
tags = {
environment = "Aj project1"
}
}
resource "null_resource" "ajprovision" {
provisioner "file" {
source = "deploy.sh"
destination = "/tmp/deploy1.sh"
connection {
type = "ssh"
user = "azureuser"
private_key = "${file("my_key")}" # private key id_rsa file
host = "${azurerm_linux_virtual_machine.vm1.public_ip_address}"
}
}
provisioner "remote-exec" {
inline = [
"chmod +x /tmp/deploy1.sh",
"/tmp/deploy1.sh"
]
connection {
type = "ssh"
user = "azureuser"
private_key = "${file("my_key")}" # private key id_rsa file
host = "${azurerm_linux_virtual_machine.vm1.public_ip_address}"
}
}
}
terraform {
backend "azurerm" {
storage_account_name = "tstate14248"
container_name = "tstate"
key = "docker1v1.tfstate"
access_key = "xxxxxxxxxxx"
}
}
Below will be performed
1. Create linux virtual machine
2. Provision virtual machine with shell script using remote-exec
Below is terraform code - main.tf
main.tf
provider "azurerm" {
# The "feature" block is required for AzureRM provider 2.x.
# If you're using version 1.x, the "features" block is not allowed.
version = "=2.0"
features {}
subscription_id = "xxxxxx"
client_id = "xxxxxxx"
client_secret = "xxxxxxxxxxxxx"
tenant_id = "xxxxxxxxxxxxxxxxx"
}
resource "azurerm_resource_group" "resgrp1" {
name = "ajdocker2"
location = "eastus"
tags = {
environment = "Aj project1"
}
}
resource "azurerm_virtual_network" "vnet1" {
name = "ajvnet1"
address_space = ["10.0.0.0/16"]
location = "eastus"
resource_group_name = azurerm_resource_group.resgrp1.name
tags = {
environment = "Aj project1"
}
}
resource "azurerm_subnet" "subnet1" {
name = "ajsubnet1"
resource_group_name = azurerm_resource_group.resgrp1.name
virtual_network_name = azurerm_virtual_network.vnet1.name
address_prefix = "10.0.2.0/24"
}
resource "azurerm_public_ip" "publicip1" {
name = "ajpub1"
location = "eastus"
resource_group_name = azurerm_resource_group.resgrp1.name
allocation_method = "Dynamic"
tags = {
environment = "Aj project1"
}
}
resource "azurerm_network_security_group" "nsg1" {
name = "ajnsg1"
location = "eastus"
resource_group_name = azurerm_resource_group.resgrp1.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 = "HTTPD"
priority = 1003
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "80"
source_address_prefix = "*"
destination_address_prefix = "*"
}
tags = {
environment = "Aj project1"
}
}
resource "azurerm_network_interface" "nic1" {
name = "ajnic1"
location = "eastus"
resource_group_name = azurerm_resource_group.resgrp1.name
ip_configuration {
name = "ajNicConfiguration"
subnet_id = "${azurerm_subnet.subnet1.id}"
private_ip_address_allocation = "Dynamic"
public_ip_address_id = "${azurerm_public_ip.publicip1.id}"
}
tags = {
environment = "Aj project1"
}
}
resource "azurerm_network_interface_security_group_association" "nsg_assoc" {
network_interface_id = azurerm_network_interface.nic1.id
network_security_group_id = azurerm_network_security_group.nsg1.id
}
resource "azurerm_linux_virtual_machine" "vm1" {
name = "ajvm1"
location = "eastus"
resource_group_name = azurerm_resource_group.resgrp1.name
network_interface_ids = [azurerm_network_interface.nic1.id]
size = "Standard_DS1_v2"
os_disk {
name = "myOsDisk"
caching = "ReadWrite"
storage_account_type = "Premium_LRS"
}
source_image_reference {
publisher = "openLogic"
offer = "CentOS"
sku = "7.3"
version = "latest"
}
computer_name = "ajvm1"
admin_username = "azureuser"
disable_password_authentication = true
admin_ssh_key {
username = "azureuser"
public_key = file("my_key.pub")
}
tags = {
environment = "Aj project1"
}
}
resource "null_resource" "ajprovision" {
provisioner "file" {
source = "deploy.sh"
destination = "/tmp/deploy1.sh"
connection {
type = "ssh"
user = "azureuser"
private_key = "${file("my_key")}" # private key id_rsa file
host = "${azurerm_linux_virtual_machine.vm1.public_ip_address}"
}
}
provisioner "remote-exec" {
inline = [
"chmod +x /tmp/deploy1.sh",
"/tmp/deploy1.sh"
]
connection {
type = "ssh"
user = "azureuser"
private_key = "${file("my_key")}" # private key id_rsa file
host = "${azurerm_linux_virtual_machine.vm1.public_ip_address}"
}
}
}
terraform {
backend "azurerm" {
storage_account_name = "tstate14248"
container_name = "tstate"
key = "docker1v1.tfstate"
access_key = "xxxxxxxxxxx"
}
}
Comments
Post a Comment