How to use a Workflow in Cortex to update an EC2 instance with Terraform
Prerequisite
Before getting started, you should have already followed the guide to Provision an EC2 instance with Terraform.Step 1: Start creating the Workflow
Follow the steps in the documentation to create a Workflow and configure its basic settings. In the Workflow’s basic settings, set its scope to apply to the entity typeAWS::EC2::Instance and the group terraform. The prerequisite Workflow Provision an EC2 instance with Terraform automatically adds the group to the entities it creates.
Step 2: Add blocks to the Workflow
The instructions on this page describe how to create this Workflow in the Cortex UI, but it is also possible to copy the Workflow YAML and add it to your workspace via the Cortex CLI. This allows you to quickly set up the example configuration then iterate on it for your own use case. Expand the tile below to learn more.Workflow YAML instructions
Workflow YAML instructions
To upload the Workflow example YAML into your workspace:
- Save the Workflow example YAML file below:
- Use the Cortex CLI to run this command, using the path to your Workflow YAML file:
cortex workflows create -f <path-to-your-workflow.yaml>
Data transformation
Data transformation
Get the entity descriptor with a data transformation block.
- Click + in the center of the page. In the block library modal, choose Data transformation.
- In the block configuration side panel, enter a name and unique slug for this block.
- In this example, we use the name
Get Descriptorand the slugget-descriptor.
- In this example, we use the name
- Add a jq expression to get the entity descriptor:
- At the bottom of the side panel, click Save.
GitHub get file
GitHub get file
This block gets the Terraform file you want to update.
- Click + in the center of the page. In the block library modal, choose GitHub > Get file.
- In the block configuration side panel, enter a name and unique slug for this block.
- In this example, we use the name
Getting Terraform fileand the slugget-terraform-file.
- In this example, we use the name
- For the Required parameters, configure the following:
- Repository:
{{actions.get-descriptor.outputs.result.info.x-cortex-git.github.repository}} - Path:
{{actions.get-descriptor.outputs.result.info.x-cortex-git.github.basepath}}/main.tf - Ref:
main
- Repository:
- Save the block.
Data transformation
Data transformation
Take the output from the previous block and decode its file contents.
- Click + in the center of the page. In the block library modal, choose Data transformation.
- In the block configuration side panel, enter a name and unique slug for this block.
- In this example, we use the name
Decoding file contentand the slugdecoding-file-content.
- In this example, we use the name
- Add a jq expression:
- At the bottom of the side panel, click Save.
JavaScript
JavaScript
Run a script to get the current properties from the file you got in previous steps of the Workflow.
- Click + in the center of the page. In the block library modal, choose GitHub > Get file.
- In the block configuration side panel, enter a name and unique slug for this block.
- In this example, we use the name
Getting current propertiesand the sluggetting-current-properties.
- In this example, we use the name
- Add a JavaScript expression:
- Save the block.
Data transformations
Data transformations
Add four Data transformation blocks to reformat the output from previous blocks:Get current region
- Click + in the center of the page. In the block library modal, choose Data transformation.
- In the block configuration side panel, enter a name and unique slug for this block.
- In this example, we use the name
Get current regionand the slugget-current-region.
- In this example, we use the name
- Add a jq expression:
- At the bottom of the side panel, click Save.
- Click + in the center of the page. In the block library modal, choose Data transformation.
- In the block configuration side panel, enter a name and unique slug for this block.
- In this example, we use the name
Get current instance typeand the slugget-current-instance-type.
- In this example, we use the name
- Add a jq expression:
- At the bottom of the side panel, click Save.
- Click + in the center of the page. In the block library modal, choose Data transformation.
- In the block configuration side panel, enter a name and unique slug for this block.
- In this example, we use the name
Get current instance nameand the slugget-current-instance-name.
- In this example, we use the name
- Add a jq expression:
- At the bottom of the side panel, click Save.
- Click + in the center of the page. In the block library modal, choose Data transformation.
- In the block configuration side panel, enter a name and unique slug for this block.
- In this example, we use the name
Setting GitHub fieldsand the slugsetting-github-fields.
- In this example, we use the name
- Add a jq expression:
- At the bottom of the side panel, click Save.
User input
User input
In this example, we add a User Input block to gather resource details from the output of previous blocks and prompt the user to select the instance type and new region.
- Click + in the center of the page. In the block library modal, choose User input.
- In the block configuration side panel, enter a name and unique slug for this block.
- In this example, we use the name
User inputand the sluguser-input.
- In this example, we use the name
- Click +Add user input. Add the following:
- Name: Current instance type
- Key: current-instance-type
- Type: Text
- Path to override value:
actions.get-current-instance-type.outputs.result - Click Add input.
- Click +Add user input. Add the following:
- Name: Instance type
- Key: instance-type
- Type: Select
- Data source: Manual
- Placeholder: In this example, the resource size options are t2.micro, t2.medium, and t2.large.
- Click Add input.
- Click +Add user input. Add the following:
- Name: Current region
- Key: get-current-region
- Type: Text
- Path to override value:
actions.get-current-region.outputs.result - Click Add input.
- Click +Add user input. Add the following:
- Name: New region
- Key: new-region
- Type: Select
- Placeholder: In this example, the options are us-west-2, us-west-1, us-east-1, and us-east-2.
- Click Add input.
- Save the block.
Data transformations
Data transformations
Use two data transformations to format the new instance type and region that were entered during the previous step:Get new instance type
- Click + in the center of the page. In the block library modal, choose Data transformation.
- In the block configuration side panel, enter a name and unique slug for this block.
- In this example, we use the name
Get new instance typeand the slugget-new-instance-type.
- In this example, we use the name
- Add a jq expression:
- At the bottom of the side panel, click Save.
- Click + in the center of the page. In the block library modal, choose Data transformation.
- In the block configuration side panel, enter a name and unique slug for this block.
- In this example, we use the name
Get new regionand the slugget-new-region.
- In this example, we use the name
- Add a jq expression:
- At the bottom of the side panel, click Save.
Scaffolder
Scaffolder
Run a Scaffolder to make modifications. In this block, you will configure override variables to pre-fill parts of the Scaffolder template based on the output of previous steps.
- Click + in the center of the page. In the block library modal, select the Scaffolder block.
- In the side panel, enter a name and unique slug for the block.
- In this example, we use the name
Modifyand the slugscaffolder.
- In this example, we use the name
- Under Scaffolder template, select your template. In this example, we use the
New EC2 Instancetemplate. - Select the option to Open a pull request.
- Add override variables. The Variable dropdown pulls in all the variables that are able to be overridden in the Git provider and in the Scaffolder template. In this example, we apply the following overrides:
- instance_type:
actions.get-new-instance-type.outputs.result- This overrides it with the output of the “Get new instance type” block earlier in the Workflow.
- resource_name:
actions.get-current-instance-name.outputs.result- This overrides it with the output of the “Get current instance name” block earlier in the Workflow.
- project_name:
actions.get-current-instance-name.outputs.result- This overrides it with the output of the “Get current instance name” block earlier in the Workflow.
- publisherRepoFullName:
actions.setting-github-fields.outputs.result.repo_name- This overrides it with the output of the “Setting GitHub fields” block earlier in the Workflow.
- publisherBranch:
actions.setting-github-fields.outputs.result.branch_name- This overrides it with the branch_name output of the “Setting GitHub fields” block earlier in the Workflow.
- publisherPullRequestTitle:
actions.setting-github-fields.outputs.result.pr_name- This overrides it with the pr_name output of the “Setting GitHub fields” block earlier in the Workflow.
- publisherPullRequestBody:
actions.setting-github-fields.outputs.result.pr_name- This overrides it with the pr_name output of the “Setting GitHub fields” block earlier in the Workflow.
- publisherCommitMessage:
actions.setting-github-fields.outputs.result.commit_msg- This overrides it with the commit_msg output of the “Setting GitHub fields” block earlier in the Workflow.
- region:
actions.get-new-region.outputs.result- This overrides it with the output of the resource region chosen during the “Get new region” block earlier in the Workflow.
- instance_type:
- Set a timeout of 5 minutes.
- At the bottom of the side panel, click Save.
Step 3: Run the Workflow
When you run the Workflow, the following events happen:- The Workflow obtains the entity descriptor and gets the Terraform file.
- The Data transformation steps decode the file contents, reshape the data, then use that reformatted data to set fields in GitHub.
- The User input step collects the current instance type, new instance type, and current region.
- The data is transformed, and the reformatted data is used to apply modifications to the EC2 instance.