Skip to content

sousquared/practice-ip-restricted-cloud-run

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

概要

Terraform を使用して Google Cloud Run にサンプルコンテナの hello アプリケーションをデプロイするリポジトリ

zenn記事: https://zenn.dev/sousquared/articles/dcdeeeaefea6e1
参考: https://zenn.dev/hosimesi/articles/36fedaa5425c7b

構成図

graph LR
    subgraph ロードバランサー
        LB[HTTP Load Balancer] 
        CA[Cloud Armor Policy] <-.-> LB
        LB --> BS[Backend Service]
        BS --> NEG[Serverless NEG]
    end

    U[ユーザー] --> LB
    NEG --> CR[Cloud Run Service]
Loading

許可するIPアドレスの設定

変更が必要な箇所としては、cloud armor policyのsrc_ip_rangesに自宅のグローバルIPを設定することです。

# Load Balancerのcloud armor policy
resource "google_compute_security_policy" "hello_policy" {
  name        = "inference-policy"
  description = "Load Balancer用のcloud armor policy"
  rule {
    action   = "allow"
    priority = 1000
    match {
      versioned_expr = "SRC_IPS_V1"
      config {
        # FIXME: your ip address
        src_ip_ranges = ["YOUR_IP_ADDRESS"]
      }
    }
    description = "my home global ip address"
  }
  rule {
    action   = "deny(403)"
    priority = 2147483647
    match {
      versioned_expr = "SRC_IPS_V1"
      config {
        src_ip_ranges = ["*"]
      }
    }
    description = "default rule"
  }
  adaptive_protection_config {
    layer_7_ddos_defense_config {
      enable = true
    }
  }
}

自宅のPCのグローバルIPの調べ方は以下を参照しました。(mac) https://qiita.com/rimorimo/items/8052bcfb32ac640a8796

terraform実行

cd infra
terraform init
terraform plan
terraform apply

terraform planやterraform applyでvariablesの入力を求められるので、適切に入力します。

$ terraform plan
var.project
  The ID of the project in which resources will be managed.

  Enter a value:

※ロードバランサやCloud Armorの反映に少し時間がかかることがあります。

アクセス

作成されたIPにアクセスします。

$ terraform output
load_balancer_ip = {IP_ADDRESS}

http://{IP_ADDRESS} にアクセス

以下の画面が出てきたら成功です! success

クリーンアップ

Terraformで作成したリソースをすべて削除して、環境をクリーンな状態に戻すには、以下のコマンドを実行します。

terraform destroy

About

Terraform を使用してIP制限した Google Cloud Run アプリケーションをデプロイする練習

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages