Search code examples
terraformjiraassets

Terraform server deployment and upload into Jira assets


we create our infrastructure with Terraform which works great. For our CMDB we are moving towards Jira Assets. Unfortunately no out of the box integration between Terraform and Jira Assets.

I managed to create aserver in Jira with curl command, but how to trigger that after cretaing the server in Terraform.

I tried using an external provider to run the curl command in a shell script, but this is also executed when I run "terraform plan", resulting in createing an asset entry every time (instead only when running apply to create a new server.

data "external" "CreateJiraAsset" {
  program = ["bash","./CreateAsset.sh", "myServerName", "1.1.1.1"]
}

The server is created via a module which is called for every server:

resource "oci_core_instance" "server" {
  availability_domain = var.availability_domain
  compartment_id      = var.compartment_id
  display_name        = "${var.display_prefix}-${var.name}"

  source_details {
    source_type = "image"
    source_id   = var.server_image
  }
  shape = var.server_shape
  create_vnic_details {
    subnet_id              = var.network_subnet
    assign_public_ip       = false
    private_ip             = var.private_ip
    hostname_label         = var.name
    nsg_ids                = var.network_securitygroups
    skip_source_dest_check = var.skip_source_dest_check
  }
  metadata = {
    user_data = base64encode(file(var.server_template))
  }
  timeouts {
    create = "10m"
  }

Any suggestions? How do you handle creating entry in CMDB after deploying infra as code?


Solution

  • The easy answer would be to use the new terraform_data provisioner for that:

    resource "terraform_data" "CreateJiraAsset" {
      provisioner "local-exec" {
        command = "${path.root}/CreateAsset.sh myServerName 1.1.1.1"
      }
    }
    

    If you are using terraform version <1.4.x, then you could use null_resource for that:

    resource "null_resource" "CreateJiraAsset" {
      provisioner "local-exec" {
        command = "${path.root}/CreateAsset.sh myServerName 1.1.1.1"
      }
    }
    

    If needed, you could tie the execution of the script to a particular attribute/variable and execute the null_resource/terraform_data based on that. For example:

    resource "terraform_data" "CreateJiraAsset" {
      triggers_replace = [
        oci_core_instance.server.id # or any other attribute/argument that is fitting the purpose
      ]
    
      provisioner "local-exec" {
        command = "${path.root}/CreateAsset.sh myServerName 1.1.1.1"
      }
    }
    

    And for the null_resource:

    resource "null_resource" "CreateJiraAsset" {
      triggers = {
        server_id = oci_core_instance.server.id # or any other attribute/argument that is fitting the purpose
      }
    
      provisioner "local-exec" {
        command = "${path.root}/CreateAsset.sh myServerName 1.1.1.1"
      }
    }
    

    Lastly, depending on the use case, you could use terraform interpolation to pass the server name and IP address to the script if needed.