Skip to content

Datastream初回構築手順書

対象: 初めてDatastream環境を構築する場合のみ実行

このドキュメントは、GCPプロジェクト、VPN、ネットワーク等の共有リソースを初回のみ作成する手順です。

2回目以降のサービス追加: datastream-add-service.md を参照してください。

構成

Aurora → VGW → HA VPN → Proxy(VM) → Datastream → BigQuery

前提条件

プロジェクト構成

  • 1つのGCPプロジェクト内に複数のDatastreamが存在
    • プロジェクト名(環境ごと):
      • production: fd-datastream-prd
      • staging: fd-datastream-stg
    • 複数のサービス(例: online-ops, online-karte)が同一プロジェクト内に共存
  • VPNは1つのプロジェクトに対して1つ
    • 複数サービスで同じVPN接続を共有

このドキュメントで作成する共有リソース

以下のリソースは初回構築時のみ作成し、2回目以降のサービス追加では既存のものを参照:

  • GCPプロジェクト
  • VPC (google_compute_network)
  • プライベートサブネット (google_compute_subnetwork) - 全サービスで共有
  • Cloud Router (VPN用) (google_compute_router)
  • HA VPN Gateway (google_compute_ha_vpn_gateway)
  • External VPN Gateway (google_compute_external_vpn_gateway)
  • VPN Tunnel (google_compute_vpn_tunnel)
  • Cloud Router Interface/BGP (google_compute_router_interface, google_compute_router_peer)
  • AWS Site-to-Site VPN (Customer Gateway、Virtual Private Gateway)
  • VPN事前共有キー用Secrets Manager
  • Datastream Private Connection (google_datastream_private_connection)
  • 共有Firewall(Health Check用、Datastream→LB用)

初回構築手順

1. GCPプロジェクト作成

  1. GCPにDatastream用の新規プロジェクトを作成
    • プロジェクト名(環境ごと):
      • production: fd-datastream-prd
      • staging: fd-datastream-stg
    • 管理や分析を優先する場合に専用の新規プロジェクトを作成する
    • コストを重視する場合は既存のプロジェクトを利用することも可能

2. GCPのVPCピアリングの準備

参考

  1. /terraform_for_aws/gcp/services/fd-datastream/${env}配下にモジュールを作成

    • production: /terraform_for_aws/gcp/services/fd-datastream/production
    • staging: /terraform_for_aws/gcp/services/fd-datastream/staging
    • 初回: 新規作成
    • 2回目以降: 既存モジュールに追記
  2. VPC(google_compute_network)を作成

    設定ルール
    • name
      • ${var.service}-${var.env}-vpc
    • routing_mode
      • GLOBAL
    • bgp_best_path_selection_mode
      • STANDARD
        • 推奨値
    • auto_create_subnetworks
      • false
  3. プライベートサブネット(google_compute_subnetwork)を作成

    • 重要: 複数サービスで同じサブネットを共有します
    設定ルール
    • name
      • ${var.service}-${var.env}-private
    • network
      • google_compute_network.main.name
        • 作成したVPCのnameを指定
    • ip_cidr_range
      • 10.xx.0.0/24 とする
        • VPN接続先および他のGCPのCIDRとバッティングしないように指定
        • 複数サービスのGCEインスタンスが同一サブネット内に配置される
    • region
      • VPN接続先と同じリージョンを指定
    • private_ip_google_access
      • true
    • log_config
      • aggregation_interval
        • INTERVAL_1_MIN
      • flow_sampling
        • 1.0
  4. Cloud Router (google_compute_router) を作成(VPN用)

    設定ルール
    • name
      • ${var.service}-${var.env}-vpn-router
    • network
      • google_compute_network.main.id
        • 作成したVPCのIDを指定
    • region
      • VPN接続先と同じリージョンを指定
    • bgp
      • asn
        • 以下の範囲で他のasnとは異なる値を指定
        • 64512–65534(16bit) or 4200000000–4294967294(32bit)
      • advertise_mode
        • CUSTOM
      • advertised_ip_ranges
        • range
          • プライベートサブネットのCIDRを指定
  5. HA VPN Gateway (google_compute_ha_vpn_gateway) を作成

    設定ルール
    • name
      • ${var.service}-${var.env}-vpn-gw
    • network
      • google_compute_network.main.id
        • 作成したVPCのIDを指定
    • region
      • VPN接続先と同じリージョンを指定
  6. Secrets Manager作成(VPN事前共有キー用)

    • VPNトンネルの事前共有キーを格納するためのシークレット
    1. 以下、それぞれgoogle_secret_manager_secret, google_secret_manager_secret_version を作成する
      • 対象
        1. VPNのトンネル1の事前共有キー用
          • secret_id: aws-vpn0-tunnel1
        2. VPNのトンネル2の事前共有キー用
          • secret_id: aws-vpn0-tunnel2
    設定ルール

    google_secret_manager_secret

    • secret_id
      • 上記指定の通り
    • project
      • サービスのプロジェクト名を指定
    • replication
      • auto
        • 冗長化させる

    google_secret_manager_secret_version

    • secret
      • google_secret_manager_secretのID指定
    • secret_data
      • "dummy"
        • AWS側をoutput参照すると値が露出してしまうため、apply後に手動で登録する
    • lifecycle
      • ignore_changes
        • [secret_data, enabled]
          • 構築後に差分が設定するため

3. AWS VPN設定

参考

  1. /terraform_for_aws/fastdoctor-template/対象サービスモジュールで以下の設定を追加しapply
  2. AWS のVPN設定
    1. template_modules/options/site-to-site-vpn-dynamic-route モジュールを使用

      設定ルール
      1. destination_ip_address
        1. GCP側で作成したプライベートサブネットのCIDRを指定
          1. GCP側で指定した変数gcp_private_subnet_ip_range
      2. customer_gateway_ip_address
        1. GCP のHA VPN ゲートウェイのIPをoutput参照する
          1. outputの名前はgcp_vpn_gw_ip[0]
      3. amazon_side_asn
        1. GCPで指定したasnに10を足した値を指定
      4. bgp_asn
        1. google_compute_router のbgpのasnの値を指定
  3. DBセキュリティグループの設定
    1. GCP側で作成したプライベートサブネットのCIDRで5432から許可追加
      1. GCP側で指定した変数gcp_private_subnet_ip_range
  4. AWS ルートテーブルの伝搬有効化
    1. プライベートサブネットのコンソールにアクセス
    2. ルート伝播 > ルート伝播の編集 > 伝播の有効化にチェック

4. AWSの事前共有キーの確認

  1. AWSコンソールにログインする
  2. VPN接続 に遷移し${project名}-vpn-connectionのVPNを選択
  3. 設定をダウンロード をクリックし、ベンダー・プラットフォームをGenericに設定
  4. ダウンロードをクリック
  5. ダウンロードしたファイルを開く
  6. 以下2つの事前共有キーを確認する
    1. IPsec Tunnel #1
      1. #1: Internet Key Exchange Configuration
        1. Pre-shared Key
    2. IPsec Tunnel #2
      1. #2: Internet Key Exchange Configuration
        1. Pre-shared Key

5. GCP Secrets Managerに事前共有キーを格納

GCPのコンソール上で手動操作

  1. Secrets Managerに以下を格納する
    • 設定ルール
      • 新しいバージョンで追加する
      • 過去のバージョンは破棄せず無効にすること(terraformで新規作成の差分が出るため)
    • 設定対象と値
      1. AWSのトンネル1の事前共有キー(aws-vpn0-tunnel1
        • 前の手順で確認したIPsec Tunnel #1Pre-shared Keyを格納
      2. AWSのトンネル2の事前共有キー(aws-vpn0-tunnel2
        • 前の手順で確認したIPsec Tunnel #2Pre-shared Keyを格納

6. VPN設定ファイルの破棄

ダウンロードしたVPNの設定ファイルを破棄する(セキュリティのため)

7. GCP トンネル設定

  1. External VPN Gateway (google_compute_external_vpn_gateway) を作成

    設定ルール
    1. name
      1. ${var.service}-${var.env}-ex-gw
    2. redundancy_type
      1. TWO_IPS_REDUNDANCY
        1. コストと可用性のバランスを考慮してトンネル数2とする
    3. interface
      1. content
        1. id
          1. トンネルのNo.
        2. ip_address
          1. AWS側のVPNトンネルのアドレスを指定
            1. output名aws_vpn_interface0_tunnel1_address
  2. dataで事前共有キーを参照

    • 参照内容

      事前共有キー分for_eachで作成

      • secret
        • google_secret_manager_secretのIDを指定
      • version
        • latest
  3. VPN トンネル(google_compute_vpn_tunnel)を作成

    設定ルール

    トンネル数分for_eachで作成

    • name
      • ${var.service}-${var.env}-vpn-tunnel${each.key}
    • region
      • VPN接続先と同じリージョンを指定
    • vpn_gateway
      • google_compute_ha_vpn_gateway.main.id
        • VPN HA GatewayのID指定
    • router
      • google_compute_router.vpn.id
        • CloudRouterのID
    • vpn_gateway_interface
      • 0を指定
        • AWSカスタマーゲートウェイが複数で冗長化される場合はインクリメントしていく
    • peer_external_gateway
      • google_compute_external_vpn_gateway.main.id
        • External VPN GatewayのID指定
    • peer_external_gateway_interface
      • 0からトンネル数ごとにインクリメント
    • shared_secret
      • dataで参照した事前共有キーを指定
        • data.google_secret_manager_secret_version.aws_vpn_pre_shared_key[each.key].secret_data
    • ike_version
      • 2
  4. Cloud Router Interface(google_compute_router_interface)を作成

    設定ルール

    トンネル数分for_eachで作成

    • name
      • ${var.service}-${var.env}-router-interface${each.key}
    • router
      • google_compute_router.vpn.name
        • CloudRouterのname指定
    • ip_range
      • AWS側のoutputsのaws_vpn_interface0_tunnel1_cgw_inside_address/30
    • vpn_tunnel
      • google_compute_vpn_tunnel.main[each.key].id
        • VPNトンネルのIDを指定
  5. Cloud Router BGP(google_compute_router_peer)を作成

    設定ルール

    トンネル数分for_eachで作成

    • name
      • ${var.service}-${var.env}-router-peer${each.key}
    • router
      • google_compute_router.vpn.name
        • CloudRouterのname指定
    • peer_ip_address
      • AWS側のoutputのaws_vpn_interface0_tunnel1_vgw_inside_address 参照
    • peer_asn
      • AWS側のasnを指定
        • 変数amazon_side_asn
    • interface
      • google_compute_router_interface.main[each.key].name
        • Cloud Router Interfaceのname指定

8. Datastream Private Connection作成

  1. プライベート接続構成(google_datastream_private_connection)の作成

    設定ルール
    • display_name
      • ${service}を指定
    • location
      • VPN接続先と同じリージョンを指定
    • private_connection_id
      • ${service}を指定
    • vpc_peering_config
      • vpc
        • google_compute_network.main.id
          • VPCのID指定
      • subnet
        • datastreamのサブネットのCIDR指定
          • プライベートサブネットの第3オクテットに1インクリメントした/20
          • 参考

9. Firewall設定

  1. GoogleのネットワークからALBに対するヘルスチェックのルールを作成

    設定ルール
    • name
      • ${var.service}-${var.env}-allow-lb-health-check
    • direction
      • INGRESS
    • network
      • google_compute_network.main.id
        • VPCのIDを指定
    • source_ranges
      • 35.191.0.0/16
      • 130.211.0.0/22
    • allow
      • protocol
        • tcp
      • ports
        • 22
    • target_tags
      • allow-lb-health-check

次のステップ

初回構築手順が完了したら、次は最初のサービスのDatastreamを追加します。

サービス追加手順: datastream-add-service.md を参照してください。