Deploying and managing Google Artifact Registry for container images and packages
Google Artifact Registry is a universal package manager that lets you store and manage container images and language packages (such as Maven and npm). It's an evolution of Container Registry, offering better management and security features.
Key Features
Multi-format support: Container images, language packages (Maven, npm, Python, etc.)
Regional storage: Store artifacts close to your deployments
VPC Service Controls: Restrict access to your artifacts
Integration with IAM: Fine-grained access control
Container Analysis: Vulnerability scanning
CMEK support: Customer-managed encryption keys
Artifact dependencies: View artifact dependencies
Binary Authorization: Enforce security policies
Deploying Artifact Registry with Terraform
Basic Repository Creation
resource "google_artifact_registry_repository" "my_repo" {
provider = google-beta
location = "us-central1"
repository_id = "my-repo"
description = "Docker repository for my applications"
format = "DOCKER"
}
# IAM policy for the repository
resource "google_artifact_registry_repository_iam_member" "repo_access" {
provider = google-beta
location = google_artifact_registry_repository.my_repo.location
repository = google_artifact_registry_repository.my_repo.name
role = "roles/artifactregistry.reader"
member = "serviceAccount:${google_service_account.service_account.email}"
}
# Service account that needs access
resource "google_service_account" "service_account" {
account_id = "artifact-user"
display_name = "Artifact Registry User"
}
Advanced Repository with CMEK
# Create a KMS keyring and key
resource "google_kms_key_ring" "keyring" {
name = "artifact-keyring"
location = "us-central1"
}
resource "google_kms_crypto_key" "key" {
name = "artifact-key"
key_ring = google_kms_key_ring.keyring.id
}
# Grant service account access to use the key
resource "google_kms_crypto_key_iam_binding" "crypto_key" {
crypto_key_id = google_kms_crypto_key.key.id
role = "roles/cloudkms.cryptoKeyEncrypterDecrypter"
members = [
"serviceAccount:service-${data.google_project.project.number}@gcp-sa-artifactregistry.iam.gserviceaccount.com",
]
}
# Create repository with CMEK
resource "google_artifact_registry_repository" "secure_repo" {
provider = google-beta
location = "us-central1"
repository_id = "secure-repo"
description = "Secure Docker repository with CMEK"
format = "DOCKER"
kms_key_name = google_kms_crypto_key.key.id
# Wait for KMS permissions to propagate
depends_on = [google_kms_crypto_key_iam_binding.crypto_key]
}
# Get project information
data "google_project" "project" {}
# Grant read access to a service account
gcloud artifacts repositories add-iam-policy-binding docker-repo \
--location=us-central1 \
--member=serviceAccount:my-sa@my-project.iam.gserviceaccount.com \
--role=roles/artifactregistry.reader
# Grant write access to a specific user
gcloud artifacts repositories add-iam-policy-binding docker-repo \
--location=us-central1 \
--member=user:user@example.com \
--role=roles/artifactregistry.writer
# Grant admin access to a group
gcloud artifacts repositories add-iam-policy-binding docker-repo \
--location=us-central1 \
--member=group:devops@example.com \
--role=roles/artifactregistry.admin
Working with Docker Images
# Configure Docker to use Artifact Registry
gcloud auth configure-docker us-central1-docker.pkg.dev
# Tag an image for Artifact Registry
docker tag my-image:latest us-central1-docker.pkg.dev/my-project/docker-repo/my-image:latest
# Push an image
docker push us-central1-docker.pkg.dev/my-project/docker-repo/my-image:latest
# Pull an image
docker pull us-central1-docker.pkg.dev/my-project/docker-repo/my-image:latest
# List images in a repository
gcloud artifacts docker images list us-central1-docker.pkg.dev/my-project/docker-repo
# Delete an image
gcloud artifacts docker images delete \
us-central1-docker.pkg.dev/my-project/docker-repo/my-image:latest