0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

VPC Service Controls保護下でArtifact Registryのリモートリポジトリを利用する

Posted at

背景

  • インターネット接続制限下で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を保護する
  1. 組織レベルのアクセスポリシーを作成
  2. 境界ルールを作成し、保護するリソースで対象のプロジェクトを追加
    image.png
  3. 制限付きサービスにArtifact Registryを追加
    image.png
  4. プロジェクト内部のCloud Buildサービスアカウントからアクセスできるように内向きルールを設定
    image.png
  • cloud run functionsのビルド環境を構築
  1. 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
      }
    }
    
  2. 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"
      }
    }
    
    
  3. 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"
      }
    
    }
    
    
    
  4. 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の保護下のため失敗
    エラー的にはバージョンが見つからない旨のエラーで失敗
    image.png

  • Artifact Registryのリモートリポジトリでアップストリーム ソースへのアクセスを許可する

gcloud artifacts vpcsc-config allow \
    --project=PROJECT_ID \
    --location=LOCATION
  • 再度ビルド実行!
    image.png
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?