Creating Terraform Infrastructure

Terraform is an open-source tool used to build, change and version infrastructure. It provides infrastructure as a code to give you a blueprint of your datacenter. You can define an execution plan to set the target state of your infrastructure and then deploy those changes.

With this workflow we will use Terraform to create infrastructure. When a Jira ticket is created with the Terraform Workspace ID in the description, the infrastructure is created.

To ensure structured permissioning, we ask for a user approval, and when the approval is granted, plan and build the infrastructure

The workflow being created

Setting Up The Workflow

  1. The Trigger Node is edited, and the HTTP trigger is enabled. This is done so that the workflow can be triggered by a Jira ticket.

2. Next we add a Custom Node. This helps us extract the Workspace ID from the Jira Ticket description.

  • Select the input from the Trigger Node, and set the type to Custom

  • Paste the following code inside the node editor

async function process(){
let result = 0;
let {collection,region} = context;
let description = collection[0].issue.fields.description;
let Input = JSON.parse(description)
exit(null, Input)
}
process().catch(exit);

3. Add an HTTP Node. This will be used to...

  • Set input to None and Method to Post

  • Set the endpoint URL to https://app.terraform.io/api/v2/runs

  • Set two headers:

    1. Content-Type to application/vnd.api+json

    2. Authorization to Bearer <Terraform Authentication Token>

  • In the body enter the following code. Change the node ID to that of the first Custom Node.

{
"data": {
"attributes": {
"is-destroy": false,
"message": "Testing terraform with Totalcloud"
},
"type": "runs",
"relationships": {
"workspace": {
"data": {
"type": "workspaces",
"id": "$.node-10903612-custom.data.wsId"
}
}
}
}
}

4. Add a Custom Node. This will be used to generate URLs for the next two HTTP calls (Nodes) - one for Plan, and one for Apply.

  • Select the input from the HTTP Node, and set the type to Custom

  • Paste the following code in the node editor:

async function process(){ let {collection,region} = context;
let runId= collection[0].response.data.id
let applyId = collection[0].response.data.relationships.apply.data.id
let URL1= "https://app.terraform.io/api/v2/runs/"+runId+"/actions/apply"
let URL2="https://app.terraform.io/api/v2/applies/"+applyId
exit(null,{URL1, URL2})
}
process().catch(exit);

5. Add a User Approval Node, and set it to notify on email or Slack. This ensures that there is a validation from the required party before the planning and applying of the architecture.

6. Add an HTTP Node. This will be used to Plan the Terraform infrastructure.

  • Set input to None and Method to Post

  • Set the endpoint URL to $.<Previous Custom Node ID>.URL1 - this ensures that custom node gets the first Terraform link from the previous Custom Node.

  • For the Headers, enter the same as from Point 4

  • Leave the Body empty

7. Add an HTTP Node. This will be used to Apply the Terraform Infrastructure.

  • Set input to None and Method to Get

  • Set the endpoint URL to $.<Previous Custom Node ID>.URL2 - this ensures that custom node gets the second Terraform link from the previous Custom Node.

  • For the Headers, enter the same as from Point 4

  • Leave the Body empty

8. Add a Delay Node and set it to 120 seconds. This is to wait for the status to be updated from Terraform.

9. Add a Custom Node. This will be used to extract the status response from the Apply HTTP Node.

  • Select the input from the third HTTP Node, and set the type to Custom

  • Paste the following code in the node editor:

async function process(){
let {collection,region} = context;
let Status = collection[0].response.data.attributes.status
exit(null,{Status})
}
process().catch(exit);

10. Add a Report Node. This will be used to send the status of the Terraform action.

  • Send the report to Slack or email of your choice

11. Save and Deploy the workflow

12. Open the Trigger Node and copy the generated URL. This will be used in creating the Jira Webhook.

Setting Up The Jira Webhook

  1. Login to Jira, and create a new webhook

    • Set the name to Terraform

    • Paste the URL from from the workflow trigger node

    • For the Summary, enter summary~ "Create Terraform Infrastructure"

    • Check Issue Created

2. Whenever a Jira Ticket with this summary is created, the Workspaces with IDs in the description will be Planned and Applied.