BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Terraform 1.8 Adds Provider-Defined Functions, Improves AWS, GCP, and Kubernetes Providers

Terraform 1.8 Adds Provider-Defined Functions, Improves AWS, GCP, and Kubernetes Providers

This item in japanese

HashiCorp has released version 1.8 of Terraform, their infrastructure-as-code language. The release introduces provider-defined functions. This enables the creation of custom functions within a given provider that handle computational-style tasks. Several providers, including AWS, GCP, and Kubernetes, have introduced new provider-defined functions alongside this release. Version 1.8 also introduces improvements to refactoring across resource types.

Provider-defined functions can be used in any Terraform expression with the following calling syntax: provider::provider_name::function_name(). These functions can perform several tasks, including data transformation, parsing data, assembling data, and simplifying validations and assertions.

Coinciding with the release of Terraform 1.8, several Terraform providers have been updated to include provider-defined functions. The 5.40 release of the Terraform AWS provider now has provider-defined functions to parse and build ARNs (Amazon Resource Names). For example, arn_parse can be used to retrieve the account ID for a given resource:

# create an ECR repository
resource "aws_ecr_repository" "hashicups" {
  name = "hashicups"
  
  image_scanning_configuration {
    scan_on_push = true
  }
}
 
# output the account ID of the ECR repository
output "hashicups_ecr_repository_account_id" {
  value = provider::aws::arn_parse(aws_ecr_repository.hashicups.arn).account_id
}

Included in the 5.23 release of the Terraform Google Cloud provider is a function to parse regions, zones, names, and projects from resource IDs that are not managed within the Terraform configuration.

resource "google_cloud_run_service_iam_member" "example_run_invoker_jane" {
  member   = "user:jane@example.com"
  role     = "run.invoker"
  service  = provider::google::name_from_id(var.example_cloud_run_service_id)
  location = provider::google::location_from_id(var.example_cloud_run_service_id)
  project  = provider::google::project_from_id(var.example_cloud_run_service_id)
}

The 2.28 release of the Terraform Kubernetes provider includes a provider-defined function for encoding and decoding Kubernetes manifests into Terraform.

resource "kubernetes_manifest" "example" {
  manifest = provider::kubernetes::manifest_decode(file("${path.module}/manifest.yaml"))
}

Version 2.30.0 of the HashiCorp Terraform extension for Visual Studio Code includes syntax highlighting and auto-completion support for provider-defined functions.

OpenTofu, the recent fork of Terraform, has indicated that they will be adding support for provider-defined functions. User janosdebugs posted in the OpenTofu GitHub repo that "provider-implemented functions have been presented to the TSC [Technical Steering Committee] and is being planned for OpenTofu 1.8.". At the time of writing, OpenTofu is on version 1.6.2.

The release also introduces new functionality to move supported resources between resource types in a faster and less error-prone manner. This enhances the moved block behavior to support moving between resources of different types if the target resource type declares it can be converted from the source resource type. Providers can add this support to handle various use cases such as renaming a provider or splitting a resource.

Terraform 1.8 is available now from GitHub or within Terraform Cloud. More details about the release can be found on the HashiCorp blog, the upgrade guide, and in the changelog.

About the Author

Rate this Article

Adoption
Style

BT