- 概要
- チュートリアル
- エラー
- Provider “registry.terraform.io/hashicorp/google-beta” requires explicit configuration. Add a provider block to the root module and│ configure the provider’s required arguments as described in the provider documentation.
- denied: Permission “artifactregistry.repositories.downloadArtifacts” denied on resource “projects/PROJECT/locations/us-central1/repositories/my-repository” (or it may not exist)
- 参考
概要
Terraform で Arififact Registry にリモートリポジトリ作成する。
CLI で Docker イメージもアップする。
Artifact Registry は Container Registry の進化形のコンテナイメージを管理できる場所。
環境
- terraform v1.1.7
チュートリアル
Terraform 準備
下記参照。
google-beta provider 追加
Artifact Registry を Terraform で利用する場合、Google Cloud プロバイダのベータ版を使用する必要がある。
vi main.tf
google-beta を追加。
provider "google" {
credentials = "自分のクレデンシャル"
project = "自分のプロジェクト"
zone = "us-central1-c"
}
# 追加
provider "google-beta" {
credentials = "自分のクレデンシャル"
project = "自分のプロジェクト"
}
Terraform 初期化できることを確認。
terraform init
Artifact Repository (リモートリポジトリ) 作成
Artifact Repository にからのリポジトリを作成していく。
公式からサンプルを拝借。
vi artifact_registry.tf
resource "google_artifact_registry_repository" "my-repo" {
provider = google-beta
location = "us-central1"
repository_id = "my-repository"
description = "example docker repository"
format = "DOCKER"
}
Terraform でデプロイ。
terraform apply
GCP コンソールより my-repository
というリモートリポジトリが作成されたことを確認。

中身は空。

Docker イメージを CLI でアップ
公式を参照。
イメージのアップには認証が必要。us-central1
の Docker リポジトリに対する認証を設定。
gcloud auth configure-docker us-central1-docker.pkg.dev
Docker イメージをローカルに用意。
docker pull us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0
ローカルイメージにタグを付ける。PROJECT
は自分のプロジェクト ID。
docker tag us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0 \ us-central1-docker.pkg.dev/PROJECT/my-repository/quickstart-image:tag1
push する。ROJECT
は自分のプロジェクト ID。
docker push us-central1-docker.pkg.dev/PROJECT/my-repository/quickstart-image:tag1
quickstart-image
というイメージがアップされた。

中身もちゃんとある。

エラー
Provider “registry.terraform.io/hashicorp/google-beta” requires explicit configuration. Add a provider block to the root module and│ configure the provider’s required arguments as described in the provider documentation.
2022年3月時点で Artifact Registry は google-beta の provider を利用する必要あり。
google-beta の provider を追加。
provider "google-beta" {
credentials = "自分のクレデンシャル"
project = "自分のプロジェクト"
region = "us-central1"
}
denied: Permission “artifactregistry.repositories.downloadArtifacts” denied on resource “projects/PROJECT/locations/us-central1/repositories/my-repository” (or it may not exist)
us-central1
の Docker リポジトリに対する認証を設定していなかった。
gcloud auth configure-docker us-central1-docker.pkg.dev
参考
Terraform Registry

標準リポジトリを作成する | Artifact Registry のドキュメント | Google Cloud

Terraform を使って Google Cloud のリソースを管理することはじめ(gloud authを利用)
コメント