Agent install via Terraform for AWS and GCP

Hello,

We have been asked by a few customers, how to slipstream the Agent install on builds through Terraform spec templates in Amazon and Google clouds. So thought will do a quick right up that might be useful to others.

Amazon Cloud:

You need to add the below user_data under the aws_instance resource of the terraform code itself.

<<-EOF
#cloud-config
runcmd:
- <%=instance.cloudConfig.agentInstall%>
- <%=instance.cloudConfig.finalizeServer%>
EOF

So, your code would look something like this:

########### Resources ###########
 
resource "aws_instance" "ec2" {
    instance_type          = var.instance_type
    ami                    = var.ami
    subnet_id              = data.aws_subnet.subnet.id
    vpc_security_group_ids = [var.security_groups]
    key_name               = var.key_name
    user_data = <<-EOF
    #cloud-config
    runcmd:
    - <%=instance.cloudConfig.agentInstall%>
    - <%=instance.cloudConfig.finalizeServer%>
    EOF
}

Google Cloud Platform:

Agent installation on GCP via Terraform uses a different module to add custom data. The module is called metadata_startup_script.

Example:

metadata_startup_script = <<EOF
    #cloud-config
    runcmd:
    sudo bash -c '<%=instance?.cloudConfig?.agentInstall%>'
    sudo bash -c '<%=instance?.cloudConfig?.finalizeServer%>'
    EOF

It needs to be added under the resource google_compute_instance like in the example below:

# Create VM
resource "google_compute_instance" "vm_instance_public" {
  name         = "${lower(var.company)}-${lower(var.app_name)}-${var.environment}-vm${random_id.instance_id.hex}"
  machine_type = var.linux_instance_type
  zone         = var.gcp_zone
  hostname     = "${var.app_name}-vm${random_id.instance_id.hex}.${var.app_domain}"
  tags         = ["ssh","http"]
  
  boot_disk {
    initialize_params {
      image = var.ubuntu_2004_sku
    }
  }
 
  metadata_startup_script = <<EOF
    #cloud-config
    runcmd:
    sudo bash -c '<%=instance?.cloudConfig?.agentInstall%>'
    sudo bash -c '<%=instance?.cloudConfig?.finalizeServer%>'
    EOF
 
  network_interface {
    network       = google_compute_network.vpc.name
    subnetwork    = google_compute_subnetwork.network_subnet.name
    access_config { }
  }
}

NOTE:
Morpheus Agent does not install via runcmd/exec for Terraform Apps for Azure clouds. The Morpheus Agent installation script is not being sent to the Azure runcmd/exec API during a Terraform App deployment. The agent installation is then failing back to SSH/WinRM to install the Morpheus Agent however, this fails as there is no route back to the host as the virtual machines do not have external IP addresses. This is a known issue and the fix will be available in future releases but there is no ETA that can be provided ATM.

Hope this is helpful. :slight_smile:

Thanks
Deepti

6 Likes