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
- production:
- 複数のサービス(例: 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プロジェクト作成
- GCPにDatastream用の新規プロジェクトを作成
- プロジェクト名(環境ごと):
- production:
fd-datastream-prd - staging:
fd-datastream-stg
- production:
- 管理や分析を優先する場合に専用の新規プロジェクトを作成する
- コストを重視する場合は既存のプロジェクトを利用することも可能
- プロジェクト名(環境ごと):
2. GCPのVPCピアリングの準備
/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回目以降: 既存モジュールに追記
- production:
VPC(
google_compute_network)を作成- ネットワーク設計を参考にする
設定ルール
- name
${var.service}-${var.env}-vpc
- routing_mode
GLOBAL
- bgp_best_path_selection_mode
STANDARD- 推奨値
- auto_create_subnetworks
false
プライベートサブネット(
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
- aggregation_interval
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を指定
- range
- asn
- name
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接続先と同じリージョンを指定
- name
Secrets Manager作成(VPN事前共有キー用)
- VPNトンネルの事前共有キーを格納するためのシークレット
- 以下、それぞれ
google_secret_manager_secret,google_secret_manager_secret_versionを作成する- 対象
- VPNのトンネル1の事前共有キー用
- secret_id:
aws-vpn0-tunnel1
- secret_id:
- VPNのトンネル2の事前共有キー用
- secret_id:
aws-vpn0-tunnel2
- secret_id:
- VPNのトンネル1の事前共有キー用
- 対象
設定ルール
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]- 構築後に差分が設定するため
- ignore_changes
3. AWS VPN設定
/terraform_for_aws/fastdoctor-template/対象サービスモジュールで以下の設定を追加しapply- AWS のVPN設定
template_modules/options/site-to-site-vpn-dynamic-routeモジュールを使用設定ルール
- destination_ip_address
- GCP側で作成したプライベートサブネットのCIDRを指定
- GCP側で指定した変数
gcp_private_subnet_ip_range
- GCP側で指定した変数
- GCP側で作成したプライベートサブネットのCIDRを指定
- customer_gateway_ip_address
- GCP のHA VPN ゲートウェイのIPをoutput参照する
- outputの名前は
gcp_vpn_gw_ip[0]
- outputの名前は
- GCP のHA VPN ゲートウェイのIPをoutput参照する
- amazon_side_asn
- GCPで指定したasnに10を足した値を指定
- bgp_asn
google_compute_routerのbgpのasnの値を指定
- destination_ip_address
- DBセキュリティグループの設定
- GCP側で作成したプライベートサブネットのCIDRで5432から許可追加
- GCP側で指定した変数
gcp_private_subnet_ip_range
- GCP側で指定した変数
- GCP側で作成したプライベートサブネットのCIDRで5432から許可追加
- AWS ルートテーブルの伝搬有効化
- プライベートサブネットのコンソールにアクセス
- ルート伝播 > ルート伝播の編集 > 伝播の有効化にチェック
4. AWSの事前共有キーの確認
- AWSコンソールにログインする
VPN接続に遷移し${project名}-vpn-connectionのVPNを選択設定をダウンロードをクリックし、ベンダー・プラットフォームをGenericに設定ダウンロードをクリック- ダウンロードしたファイルを開く
- 以下2つの事前共有キーを確認する
- IPsec Tunnel #1
- #1: Internet Key Exchange Configuration
- Pre-shared Key
- #1: Internet Key Exchange Configuration
- IPsec Tunnel #2
- #2: Internet Key Exchange Configuration
- Pre-shared Key
- #2: Internet Key Exchange Configuration
- IPsec Tunnel #1
5. GCP Secrets Managerに事前共有キーを格納
GCPのコンソール上で手動操作
- Secrets Managerに以下を格納する
- 設定ルール
新しいバージョンで追加する- 過去のバージョンは破棄せず無効にすること(terraformで新規作成の差分が出るため)
- 設定対象と値
- AWSのトンネル1の事前共有キー(
aws-vpn0-tunnel1)- 前の手順で確認した
IPsec Tunnel #1のPre-shared Keyを格納
- 前の手順で確認した
- AWSのトンネル2の事前共有キー(
aws-vpn0-tunnel2)- 前の手順で確認した
IPsec Tunnel #2のPre-shared Keyを格納
- 前の手順で確認した
- AWSのトンネル1の事前共有キー(
- 設定ルール
6. VPN設定ファイルの破棄
ダウンロードしたVPNの設定ファイルを破棄する(セキュリティのため)
7. GCP トンネル設定
External VPN Gateway (
google_compute_external_vpn_gateway) を作成設定ルール
- name
${var.service}-${var.env}-ex-gw
- redundancy_type
TWO_IPS_REDUNDANCY- コストと可用性のバランスを考慮してトンネル数2とする
- interface
- content
- id
- トンネルのNo.
- ip_address
- AWS側のVPNトンネルのアドレスを指定
- output名
aws_vpn_interface0_tunnel1_address
- output名
- AWS側のVPNトンネルのアドレスを指定
- id
- content
- name
dataで事前共有キーを参照
参照内容
事前共有キー分for_eachで作成
- secret
google_secret_manager_secretのIDを指定
- version
- latest
- secret
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
- dataで参照した事前共有キーを指定
- ike_version
2
- name
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
- AWS側のoutputsの
- vpn_tunnel
google_compute_vpn_tunnel.main[each.key].id- VPNトンネルのIDを指定
- name
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参照
- AWS側のoutputの
- peer_asn
- AWS側のasnを指定
- 変数
amazon_side_asn
- 変数
- AWS側のasnを指定
- interface
google_compute_router_interface.main[each.key].name- Cloud Router Interfaceのname指定
- name
8. Datastream Private Connection作成
プライベート接続構成(
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
- 参考
- datastreamのサブネットのCIDR指定
- vpc
- display_name
9. Firewall設定
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/16130.211.0.0/22
- allow
- protocol
tcp
- ports
22
- protocol
- target_tags
allow-lb-health-check
- name
次のステップ
初回構築手順が完了したら、次は最初のサービスのDatastreamを追加します。
サービス追加手順: datastream-add-service.md を参照してください。