Use Terraform Actions in Terraform Developer Portal
Terraform actions are predefined operations built into providers that let Terraform perform day-two operations. For example, you can create an action to invoke an AWS Lambda function or to shut down an EC2 instance. When you define an action in a Terraform no-code module that you use to create a Terraform Developer Portal template or add-on, developers can invoke them in the same interface that they use to manage their applications.
In this tutorial, you will review a Terraform action defined in the no-code module you used to create the add-on definition in the previous tutorial, then invoke the action in Terraform Developer Portal.
Prerequisites
For this tutorial, you will need the following:
- An HCP Terraform account organization on the Standard edition or higher to use HCP Terraform no-code modules and the private module registry.
- A deployed instance of the terramino template and s3-deployment add-on created in the earlier tutorials.
Review the Terraform action
The no-code module you used to create the s3-deployment add-on in the previous tutorial already defines a Terraform action. Open the aws.tf file in the terraform-aws-s3-deployment repository and review the following action block:
action "aws_cloudfront_create_invalidation" "full" {
config {
distribution_id = aws_cloudfront_distribution.s3_distribution.id
paths = ["/*"]
}
}
This block defines an action that developers can invoke themselves. In this case, it defines the aws_cloudfront_create_invalidation action, which is implemented in the AWS provider to invalidate a CloudFront cache. If CloudFront continues to serve an older cached version of a file, developers can invoke the action to manually clear the cache and force CloudFront to fetch the latest version of the file.
Actions in Terraform can define optional configuration with the config block. In this case, the action sets the distribution_id to the ID of the CloudFront distribution this module creates and sets the path argument to ["/*"]. The configuration instructs the AWS provider to invalidate all cached paths.
Refer to the AWS provider documentation for more information on this action.
Review the application
Open the S3 deployment URL of the Terramino application you created in the previous tutorial. Under the Terramino title and description, note that the application reports if the page caused a cache hit. This indicates that either CloudFront served a cached version of the page to your browser or that a cache missed. As a result, CloudFront had to fetch the file from S3.
Refresh the page several times until the page reports that the response resulted in a cache hit. Note that depending on your browser, you may need to force refresh the page. If you are on Windows, you can typically press Ctrl + F5 to force refresh the browser tab. If you are on Mac, press Command + Shift + R.
After a few refreshes, each request should eventually result in a cache hit, indicating that CloudFront served up the request from its cache.
Invoke the action
Next, complete the following steps to invoke the action in your add-on and invalidate the CloudFront cache:
- Sign in to your HCP Terraform organization and click Developer Portal.
- Click Applications in the left navigation panel.
- Click the terramino-get-started application you created in the previous tutorial.
- Click Installed add-ons in the left navigation panel.
- Click the name of your deployed add-on.
- Click Actions in the left navigation panel.
- Click aws_cloudfront_create_invalidation.full from the list of actions.
- Click Invoke.
- Under Select run type, leave the default choice of Plan and apply, then click Invoke.
Terraform Developer Portal automatically redirects you to the run in the HCP Terraform workspace for your add-on to display the progress as it invokes the action.
After a few moments, HCP Terraform reports that it invoked the action.
Return to the S3 deployment of Terramino in your browser and refresh the page again. This time, the application reports a cache miss, indicating that CloudFront did not have the page currently cached to serve the request. After a few more requests, the application again reports cache hits.
Clean up resources
When you delete add-ons and applications in Terraform Developer Portal, it automatically starts destroy runs in each associated workspace in HCP Terraform to destroy the resources it created. Complete the following steps to uninstall the S3 add-on first:
- Sign in to your HCP Terraform organization and click Developer Portal.
- Click Applications in the left navigation panel.
- Click the terramino-get-started application you created in the previous tutorial.
- Click Installed add-ons in the left navigation panel.
- Next to your installed add-on, click the ellipsis (...), then click Uninstall.
- Terraform Developer Portal prompts you to confirm that you want to uninstall the add-on. Enter
UNINSTALLin the Confirm uninstall textbox, then click Uninstall.
Terraform Developer Portal begins a destroy run in the add-on's workspace. Optionally, you can navigate to the workspace in HCP Terraform to follow the destruction run.
Once HCP Terraform completes the destroy run, return to your application's Overview page in Terraform Developer Portal and complete the following steps to delete your application:
- Click the Manage dropdown.
- Click Delete.
- Terraform Developer Portal prompts you to confirm that you want to delete the application. Enter
deletein the Confirm textbox, then click Delete.
Similar to how Terraform Developer Portal queued a destroy operation when you uninstalled the add-on, it also queues a destroy operation on the application's workspace.
If you delete an application without uninstalling its add-ons, Terraform Developer Portal automatically queues a destroy operation on every add-on workspace.
Next steps
In these tutorials, you learned how to create and deploy Terraform Developer Portal templates and add-ons. You created a template to deploy the scaffold code for a new application to GitHub, then created an add-on the deploy the application to AWS using S3 and CloudFront. You also learned how to define and invoke Terraform actions in your templates and add-ons, and used an action to invalidate the CloudFront cache.
For more information on Terraform Developer Portal, no-code modules, and Terraform actions, refer to the following resources:
- Read the Terraform Developer Portal documentation.
- Learn how to Design no-code ready modules.
- Learn how to Configure and invoke Terraform actions.
- Reference the action block reference.