Terraform help with the user data

Hello,
I was trying to convert AWS cloudformation template to terraform code for deploying an instance of cisco dna center on AWS. The cloudformation stack contains the below data for setting up the user data. Could someone please help with the terraform code for adding this user data in aws_launch_template in terraform?

UserData:
        'Fn::Base64': !Sub |
           #cloud-config
           write_files:
            - content: |
                {
                  "IPaddress": "${DnacInstanceIP}",
                  "netmask": "${DnacNetmask}",
                  "gateway": "${DnacGateway}",
                  "dns_servers": ["${DnacDnsServer}"],
                  "fqdn" : "${DnacFQDN}",
                  "https_proxy" : "${DnacHttpsProxy}",
                  "https_proxy_username" : "${DnacHttpsProxyUsername}",
                  "https_proxy_password" : "${DnacHttpsProxyPassword}",
                  "ntp": ["169.254.169.123"],
                  "password" : "${DnacPassword}"
                }
              path: /etc/cloud.json

[Cisco CCNP]

To add user data to an aws_launch_template in Terraform, you can use the templatefile function to render your user data script from a template file and then encode it with base64encode. Here’s a simplified example:

hclCopy code

resource "aws_launch_template" "example" {
  name_prefix   = "example-"
  image_id      = "ami-123456"
  instance_type = "t2.micro"

  user_data = base64encode(templatefile("user_data.tpl", {
    DnacInstanceIP        = "your_instance_ip",
    DnacNetmask           = "your_netmask",
    // Add more variables as needed
  }))
}

In your user_data.tpl file, structure your user data script with placeholders for variables:

yamlCopy code

#cloud-config
write_files:
  - content: |
      {
        "IPaddress": "${DnacInstanceIP}",
        "netmask": "${DnacNetmask}",
        // Add more configuration as needed
      }
    path: /etc/cloud.json

This setup dynamically injects the variable values into the user data script and encodes it in base64 for the launch template.

Here’s an example Terraform configuration that demonstrates how you can include the user data in an aws_launch_template:

hclCopy code

variable "DnacInstanceIP" {}
variable "DnacNetmask" {}
variable "DnacGateway" {}
variable "DnacDnsServer" {}
variable "DnacFQDN" {}
variable "DnacHttpsProxy" {}
variable "DnacHttpsProxyUsername" {}
variable "DnacHttpsProxyPassword" {}
variable "DnacPassword" {}

resource "aws_launch_template" "dnac_template" {
  name = "dnac-launch-template"

  # Other configuration for the launch template...

  user_data = base64encode(templatefile("user_data.tpl", {
    DnacInstanceIP        = var.DnacInstanceIP
    DnacNetmask           = var.DnacNetmask
    DnacGateway           = var.DnacGateway
    DnacDnsServer         = var.DnacDnsServer
    DnacFQDN              = var.DnacFQDN
    DnacHttpsProxy        = var.DnacHttpsProxy
    DnacHttpsProxyUsername= var.DnacHttpsProxyUsername
    DnacHttpsProxyPassword= var.DnacHttpsProxyPassword
    DnacPassword          = var.DnacPassword
  }))
}

In this example, user_data.tpl is a separate file containing your user data script template. It will look something like this:

yamlCopy code

#cloud-config
write_files:
  - content: |
      {
        "IPaddress": "${DnacInstanceIP}",
        "netmask": "${DnacNetmask}",
        "gateway": "${DnacGateway}",
        "dnsservers": ["${DnacDnsServer}"],
        "fqdn": "${DnacFQDN}",
        "https_proxy": "${DnacHttpsProxy}",
        "https_proxy_username": "${DnacHttpsProxyUsername}",
        "https_proxy_password": "${DnacHttpsProxyPassword}",
        "ntp": ["169.254.169.123"],
        "password": "${DnacPassword}"
      }
    path: /etc/cloud.json

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.