terraformterraform-provider-azurenewrelicazure-vm-scale-setnewrelic-platform

Terraform script for provisioning azure Virtual machine scale set along with custom data or cloud init


I would like to know the terraform script for provisioning azure Virtual machine scale set along with custom data or cloud init.

I tried many ways to run my script against VMSS but its not working.As per my understanding during provisioning of VMSS I should run some shell scripts so that It can install necessary agents (New relic) into all VMSS instances.

Looking for terraform script for VMSS along with custom data or cloudinit configuration.


Solution

  • This is the below solution finally I could able to accomplish via terraform using custom data.

    terraform {
    required_version = ">=0.12"
    
    required_providers {
    azurerm = {
    source = "hashicorp/azurerm"
    version = "~>2.0"
    }
    }
    }
    
    
    
    provider "azurerm" {
    features {}
    }
    
    
    
    resource "azurerm_resource_group" "vmss" {
    name = var.resource_group_name
    location = var.location
    tags = var.tags
    }
    
    
    
    resource "random_string" "fqdn" {
    length = 6
    special = false
    upper = false
    number = false
    }
    
    
    
    resource "azurerm_virtual_network" "vmss" {
    name = "vmss-vnet"
    address_space = ["10.0.0.0/16"]
    location = var.location
    resource_group_name = azurerm_resource_group.vmss.name
    tags = var.tags
    }
    
    
    
    resource "azurerm_subnet" "vmss" {
    name = "vmss-subnet"
    resource_group_name = azurerm_resource_group.vmss.name
    virtual_network_name = azurerm_virtual_network.vmss.name
    address_prefixes = ["10.0.2.0/24"]
    }
    
    
    
    resource "azurerm_public_ip" "vmss" {
    name = "vmss-public-ip"
    location = var.location
    resource_group_name = azurerm_resource_group.vmss.name
    allocation_method = "Static"
    domain_name_label = random_string.fqdn.result
    tags = var.tags
    }
    
    
    
    
    resource "azurerm_virtual_machine_scale_set" "vmss" {
    name = "vmscaleset"
    location = var.location
    resource_group_name = azurerm_resource_group.vmss.name
    upgrade_policy_mode = "Manual"
    
    
    
    sku {
    name = "Standard_DS1_v2"
    tier = "Standard"
    capacity = 2
    }
    
    
    
    storage_profile_image_reference {
    publisher = "Canonical"
    offer = "UbuntuServer"
    sku = "16.04-LTS"
    version = "latest"
    }
    
    
    
    storage_profile_os_disk {
    name = ""
    caching = "ReadWrite"
    create_option = "FromImage"
    managed_disk_type = "Standard_LRS"
    
    }
    
    
    
    os_profile {
    computer_name_prefix = "vmlab"
    admin_username = var.admin_user
    admin_password = var.admin_password
    **custom_data = file("test.sh") **// This is the key line to pass any custom data to VMSS so that during VM spin up each time automatically script will be invoked and will be executed.**
    }
    
    
    
    os_profile_linux_config {
    disable_password_authentication = false
    }
    network_profile {
    name = "terraformnetworkprofile"
    primary = true
    
    
    
    ip_configuration {
    name = "IPConfiguration"
    subnet_id = azurerm_subnet.vmss.id
    #load_balancer_backend_address_pool_ids = [azurerm_lb_backend_address_pool.bpepool.id]
    primary = true
    }
    }
    tags = var.tags
    }