背景
- インターネット接続制限下でCloud Run functionsをビルドできるようにしたいが、VPC Service Controlsによってうまくいかない部分があった。
目的
- Cloud Run functionsをインターネット接続制限下でビルドしたい
- このとき、Artifact Registryのリモートリポジトリを経由してプログラムのパッケージをプルしたい
前提
- VPC service controlsでArtifact Registryのサービスが保護されている
- インターネット接続を制限する制約があるため、Cloud Buildのプライベートワーカープールを利用する
つまづき
- VPC service controlsでArtifact Registryのサービスが保護されているとき、Artifact Registryのリモートリポジトリ経由でパッケージを取得できない。
- VPC service controlsのルールではこれを解消する外向きルールを設定できない。
解決策
アップストリーム ソースへのアクセスを許可する
指定したロケーションにあるアップストリーム ソースへのアクセスを許可するには、次のコマンドを実行します。
gcloud artifacts vpcsc-config allow \
    --project=PROJECT_ID \
    --location=LOCATION
再現方法
- VPC Service ControlsでArtifact Registryを保護する
- 組織レベルのアクセスポリシーを作成
- 境界ルールを作成し、保護するリソースで対象のプロジェクトを追加
   
- 制限付きサービスにArtifact Registryを追加
   
- プロジェクト内部のCloud Buildサービスアカウントからアクセスできるように内向きルールを設定
   
- cloud run functionsのビルド環境を構築
- Cloud build プライベートワーカープールを構築
resource "google_cloudbuild_worker_pool" "private_pool" { project = var.project_id name = "cb-workerpool" location = var.location_primary # --- ワーカーマシンの構成 --- worker_config { machine_type = "e2-medium" disk_size_gb = 100 # 外部IPを持たないプライベートなワーカー no_external_ip = true } # --- ネットワーク構成 --- # Service Networking APIを介して指定のVPCに接続する network_config { peered_network = data.google_compute_network.default.id peered_network_ip_range = var.cloudbuild_peered_network_ip_range } }
- Artifact Registryの構築
resource "google_artifact_registry_repository" "remote_pypi" { project = var.project_id location = var.location_primary repository_id = "ar-buildpython" description = "Cloud Build用のPython(PyPI)リモートリポジトリ" format = "PYTHON" mode = "REMOTE_REPOSITORY" cleanup_policy_dry_run = true remote_repository_config { python_repository { public_repository = "PYPI" } } vulnerability_scanning_config { enablement_config = "INHERITED" } }
- Cloud run functionsの実装コード
resource "google_cloudfunctions2_function" "alert_handler" { project = var.project_id name = "crf-sendtoevent" location = var.location_primary # --- ビルド設定: ソースコードからコンテナイメージをビルドする方法を定義 --- build_config { runtime = "python312" entry_point = "send_event" # GCSにアップロードしたソースコードのZipファイルを指定 source { storage_source { bucket = google_storage_bucket_object.source_archive.bucket object = google_storage_bucket_object.source_archive.name generation = google_storage_bucket_object.source_archive.generation } } docker_repository = google_artifact_registry_repository.cloud_run_functions.id worker_pool = google_cloudbuild_worker_pool.private_pool.id # ビルド時に使用するサービスアカウント service_account = data.google_service_account.function_build_sa.id } # --- 実行時設定: Functionがどのように動作するかを定義 --- service_config { # ランタイム(実行時)に使用するサービスアカウント service_account_email = data.google_service_account.function_runtime_sa.email # コンテナのリソース設定 available_memory = "512Mi" timeout_seconds = 540 min_instance_count = 1 max_instance_count = 100 max_instance_request_concurrency = 1 # 環境変数 environment_variables = { LOG_EXECUTION_ID = true } # ネットワーク設定 ingress_settings = "ALLOW_INTERNAL_ONLY" vpc_connector = google_vpc_access_connector.serverless_connector.id vpc_connector_egress_settings = "ALL_TRAFFIC" } }
- requirements.txtでArtifact Registryを指定する。
requirements.txt--index-url https://asia-northeast1-python.pkg.dev/[project_id]/ar-buildpython/simple functions-framework==3.9.2 pysnmp==7.1.21 google-cloud-error-reporting==1.12.0
- 
terraform apply実行後Artifact Registryの保護下のため失敗 
 エラー的にはバージョンが見つからない旨のエラーで失敗
  
- 
Artifact Registryのリモートリポジトリでアップストリーム ソースへのアクセスを許可する 
gcloud artifacts vpcsc-config allow \
    --project=PROJECT_ID \
    --location=LOCATION

