Terraform Artifact Registry

Managing build artifacts and infrastructure with Terraform requires a clear understanding of two distinct registry concepts: Google Cloud Artifact Registry as a destination for Docker images, npm packages, Python packages, Maven artifacts and more, and the Terraform Registry as a source for reusable modules and providers. Using Terraform to create and manage Artifact Registry repositories on GCP ensures repositories are consistently configured, properly secured, and reproducible across environments.

Artifact Registry is Google Cloud's service for storing and managing build artifacts - Docker images, npm packages, Python packages, Maven artifacts, and more. If you are using GCP for your infrastructure, Artifact Registry is the natural place to store your container images and packages.

If you have been using Google Container Registry (GCR), it is time to switch. Container Registry is deprecated and Artifact Registry is its replacement

Why Artifact Registry Over Container Registry

Artifact Registry provides a unified storage location for multiple package formats within a single Google Cloud project. The service supports docker, apt, yum, go, pypi, npm, maven.

Managing Artifact Registry with Terraform ensures your repositories are consistently configured, properly secured, and reproducible across environments.

Terraform Provider for Google Cloud and Artifact Registry

HashiCorp Terraform is an infrastructure-as-code (IaC) tool that lets you provision and manage cloud infrastructure. Terraform provides plugins called providers that let you interact with cloud providers and other APIs. You can use the Terraform provider for Google Cloud to provision and manage Google Cloud resources, including Artifact Registry.

This page introduces you to using Terraform with Artifact Registry, including an introduction to how Terraform works and some resources to help you get started using Terraform with Google Cloud. You'll also find links to Terraform reference docs for Artifact Registry, code examples, and guides for using Terraform to provision Artifact Registry resources.

How Terraform works

Terraform has a declarative and configuration-oriented syntax, which you can use to describe the infrastructure that you want to provision in your Google Cloud project. After you author this configuration in one or more Terraform configuration files, you can use the Terraform CLI to apply this configuration to your Artifact Registry resources.

The following steps explain how Terraform works:

You describe the infrastructure you want to provision in a Terraform configuration file. You don't need to write code describing how to provision the infrastructure. Terraform provisions the infrastructure for you.

You run the terraform plan command, which evaluates your configuration and generates an execution plan. You can review the plan and make changes as needed

Each resource block describes one or more infrastructure objects, such as virtual networks or compute instances.

The following table lists the Terraform resources available for Artifact Registry:

Service Terraform resources Data sources
Artifact Registry

Terraform-Based Guides for Artifact Registry

Terraform-based guides for Artifact Registry

The following table lists Terraform-based how-to guides and tutorials for Artifact Registry:

Guide Details
Create a standard repository using Terraform This guide describes how to create a standard mode repository with Terraform.
Create a remote repository using Terraform This guide describes how to create a remote mode repository with Terraform.
Create a virtual mode repository using Terraform This guide describes how to create a virtual mode repository with Terraform.

Terraform modules and blueprints help you automate provisioning and managing of Google Cloud resources at scale. A module is a reusable set of Terraform configuration files that creates a logical abstraction of Terraform resources

Terraform modules and blueprints for Artifact Registry

Modules and blueprints help you automate provisioning and managing of Google Cloud resources at scale. A module is a reusable set of Terraform configuration files that creates a logical abstraction of Terraform resources. A blueprint is a package of deployable and reusable modules, and a policy that implements and documents a specific solution.

The following table lists modules and blueprints related to Artifact Registry:

Module or blueprint Details
artifact-registry Create and manage Artifact Registry repositories
secure-cicd Builds a secure CI/CD pipeline on Google Cloud
gcloud Executes gcloud CLI commands within Terraform

GoogleCloudPlatform Artifact Registry Terraform Module

The Terraform module handles the creation of repositories in Artifact Registry on Google Cloud.

The resources/services/activations/deletions that this module will create/trigger are:

  • Different repositories in Artifact Registry
  • docker, apt, yum, go, pypi, npm, maven
  • Provide Artifact Registry Reader or Writer roles to the users and service accounts

This module assumes that below mentioned prerequisites are in place before consuming the module.

  • To deploy this blueprint you must have an active billing account and billing permissions.
  • APIs are enabled
  • Permissions are available

Basic usage of this module is as follows:

```hcl
module "artifact_registry" {
source = "GoogleCloudPlatform/artifact-registry/google"
version = "~> 0.8"

Required variables

projectid = ""
location = ""
format = ""
repository
id = ""
}
```

Functional examples are included in the examples directory.

Name Description Type Default Required
cleanup_policies Cleanup policies for this repository. Cleanup policies indicate when certain package versions can be automatically deleted. Map keys are policy IDs supplied by users during policy creation. They must unique within a repository and be under 128 characters in length

Using Terraform Registry Artifacts

Data engineers often need reliable infrastructure management and best practices for Terraform. Using artifacts from the Terraform Registry simplifies module reuse and accelerates Terraform production deployment. This tutorial shows how to fetch registry artifacts in Terraform and integrate them into an ELT Airflow DAG.

Understanding Terraform Registry Artifacts

The Terraform Registry hosts modules and providers that you can incorporate into your configurations. Benefits include:

  • Standardized module versions
  • Community-vetted patterns
  • Easy upgrades and maintenance

For organizations considering Terraform enterprise, review Terraform support plans and enterprise features to align with SLAs.

When evaluating Terraform pricing, Terraform enterprise features, or comparing Terraform vs Pulumi and Terraform vs CloudFormation, leveraging registry artifacts reduces development time. It also aligns with Terraform managed service models and aids in cost control such as Terraform Cloud cost. Exploring Terraform alternatives may also highlight different approaches to infrastructure as code.

Public Namespace Artifacts in Terraform Cloud

You can create new versions of public registry artifacts using the same workflow as individual GitHub accounts. Add a properly formatted tag for modules, or release for providers, to your artifact's GitHub repository. GitHub notifies HCP Terraform that a new version or release is available and instructs the public registry to create a new version.

Once the public registry creates the new version it appears in your namespace in HCP Terraform. You can view artifact versions by clicking the ellipsis … next to an artifact and selecting View version history. New versions are also visible on your artifact's page in the public registry.

If the new version creates errors, a notification appears on your organization's Public namespaces page. The notification includes an error, and after you fix the problem we recommend creating a new tag or release.

On the Public namespaces page, you can click the ellipsis … next to an artifact to list the available actions you can take on that artifact:

  • View in public registry links to the artifact’s page on https://registry.terraform.io

If you navigate away from the page before processing is complete, it continues in the background.

Practical Workflow for GCP Artifact Registry with Terraform

Provisioning Artifact Registry repositories with Terraform follows the declarative workflow.

  • You describe the infrastructure you want to provision in a Terraform configuration file. You don't need to write code describing how to provision the infrastructure. Terraform provisions the infrastructure for you.
  • You run the terraform plan command, which evaluates your configuration and generates an execution plan. You can review the plan and make changes as needed

A minimal provider configuration for Google Cloud is required before resources can be created.

```hcl
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "~> 5.0"
}
}
}

provider "google" {
project = var.project_id
region = var.location
}
```

Repository creation can be expressed with the artifactregistryrepository resource. Format selection controls which package types are allowed in the repository.

Supported formats include docker, apt, yum, go, pypi, npm, maven.

The module GoogleCloudPlatform/artifact-registry/google abstracts these resources and also provides Artifact Registry Reader or Writer roles to the users and service accounts.

Prerequisites for module consumption are:

  • To deploy this blueprint you must have an active billing account and billing permissions.
  • APIs are enabled
  • Permissions are available

Cleanup policies can be attached to the repository. Cleanup policies indicate when certain package versions can be automatically deleted. Map keys are policy IDs supplied by users during policy creation. They must unique within a repository and be under 128 characters in length.

Integrating Registry Artifacts into Pipelines

Tutorial: Using Registry Artifacts with Airflow

1

The integration of Terraform Registry artifacts with ELT pipelines demonstrates how module reuse accelerates production deployment.

Standardized module versions reduce drift. Community-vetted patterns improve reliability. Easy upgrades and maintenance reduce technical debt.

When evaluating Terraform pricing, Terraform enterprise features, or comparing Terraform vs Pulumi and Terraform vs CloudFormation, leveraging registry artifacts reduces development time. It also aligns with Terraform managed service models and aids in cost control such as Terraform Cloud cost.

What's Next

  • Terraform code samples for Artifact Registry
  • Terraform on Google Cloud documentation
  • Google Cloud provider documentation in HashiCorp
  • Infrastructure as code for Google Cloud

Conclusion

Terraform provides a coherent path to manage Artifact Registry repositories on Google Cloud and to consume Terraform Registry artifacts for module reuse. Using the Terraform provider for Google Cloud, teams can provision Artifact Registry repositories with declarative configuration, apply cleanup policies, and assign Reader or Writer roles through modules such as GoogleCloudPlatform/artifact-registry/google.

For organizations migrating from Google Container Registry, Artifact Registry is the replacement and Terraform offers reproducible migration and management.

Separately, Terraform Registry artifacts provide standardized modules and providers that simplify infrastructure reuse. The public namespace workflow in HCP Terraform allows automated version creation from GitHub tags and releases, with error notifications and version history visibility.

Together, these two artifact concepts enable consistent artifact storage in GCP and consistent infrastructure delivery via Terraform.

Sources

  1. oneuptime.com
  2. getorchestra.io
  3. docs.cloud.google.com
  4. github.com
  5. developer.hashicorp.com

Related Posts