Firebase กำลังเริ่มรองรับ Terraform หากคุณอยู่ในทีมที่ต้องการทำให้การสร้างโปรเจ็กต์ Firebase เป็นแบบอัตโนมัติและมาตรฐานด้วยการจัดสรรทรัพยากรที่เฉพาะเจาะจงและเปิดใช้บริการต่างๆ การใช้ Terraform กับ Firebase อาจเหมาะกับคุณ
เวิร์กโฟลว์พื้นฐานในการใช้ Terraform กับ Firebase มีดังนี้
การสร้างและปรับแต่งไฟล์การกําหนดค่า Terraform (ไฟล์
.tf
) ซึ่งระบุโครงสร้างพื้นฐานที่ต้องการจัดสรร (นั่นคือ ทรัพยากรที่ต้องการจัดสรรและบริการที่ต้องการเปิดใช้)ใช้คำสั่ง gcloud CLI ที่อินเทอร์เฟซกับ Terraform เพื่อจัดสรรโครงสร้างพื้นฐานที่ระบุไว้ในไฟล์
.tf
คุณใช้ Terraform และ Firebase ทำอะไรได้บ้าง
เวิร์กโฟลว์ทั่วไปตัวอย่างในคู่มือนี้คือการสร้างโปรเจ็กต์ Firebase ใหม่ด้วยแอป Android แต่คุณทําสิ่งอื่นๆ ได้อีกมากมายด้วย Terraform เช่น
ลบและแก้ไขโครงสร้างพื้นฐานที่มีอยู่โดยใช้ Terraform
จัดการการกําหนดค่าและงานเฉพาะผลิตภัณฑ์โดยใช้ Terraform เช่น
- การเปิดใช้ผู้ให้บริการการลงชื่อเข้าใช้ Firebase Authentication
- การสร้างที่เก็บข้อมูล Cloud Storage หรืออินสแตนซ์ฐานข้อมูลและทำให้ Firebase Security Rules พร้อมใช้งาน
คุณสามารถใช้ไฟล์การกําหนดค่าและคําสั่ง Terraform มาตรฐานเพื่อทํางานเหล่านี้ทั้งหมดได้ เราได้จัดทำตัวอย่างไฟล์การกําหนดค่า Terraform สําหรับกรณีการใช้งานทั่วไปหลายกรณีเพื่อช่วยคุณในเรื่องนี้
เวิร์กโฟลว์ทั่วไปสำหรับการใช้ Terraform กับ Firebase
ข้อกำหนดเบื้องต้น
คู่มือนี้เป็นข้อมูลเบื้องต้นเกี่ยวกับการใช้ Terraform กับ Firebase ดังนั้นจึงถือว่าผู้ใช้มีความเชี่ยวชาญพื้นฐานเกี่ยวกับ Terraform โปรดตรวจสอบว่าคุณได้ดำเนินการตามข้อกําหนดเบื้องต้นต่อไปนี้แล้วก่อนเริ่มเวิร์กโฟลว์นี้
ติดตั้ง Terraform และทำความคุ้นเคยกับ Terraform โดยใช้บทแนะนำอย่างเป็นทางการ
ติดตั้ง Google Cloud CLI (gcloud CLI) เข้าสู่ระบบโดยใช้บัญชีผู้ใช้หรือบัญชีบริการ
ดูข้อกำหนดสำหรับบัญชีผู้ใช้และบัญชีบริการ
- หากใช้บัญชีผู้ใช้ คุณต้องยอมรับข้อกำหนดในการให้บริการของ Firebase (ข้อกำหนดในการให้บริการของ Firebase) ถือว่าคุณยอมรับข้อกําหนดในการให้บริการของ Firebase หากดูโปรเจ็กต์ Firebase ในคอนโซล Firebase ได้
- หากต้องการให้ Terraform ดำเนินการบางอย่าง (เช่น สร้างโปรเจ็กต์) เงื่อนไขต่อไปนี้จะต้องเป็นจริง
- ผู้ใช้หรือบัญชีบริการต้องมีสิทธิ์เข้าถึง IAM ที่เกี่ยวข้องสําหรับการดำเนินการเหล่านั้น
- หากบัญชีผู้ใช้หรือบัญชีบริการเป็นส่วนหนึ่งขององค์กร Google Cloud นโยบายขององค์กรต้องอนุญาตให้บัญชีดำเนินการเหล่านั้น
ขั้นตอนที่ 1: สร้างและปรับแต่งไฟล์การกําหนดค่า Terraform
ไฟล์การกําหนดค่า Terraform ต้องมี 2 ส่วนหลัก (ซึ่งอธิบายไว้อย่างละเอียดด้านล่าง)
- ส่วน
provider
การตั้งค่าซึ่งกำหนดว่าทรัพยากร Terraform ใดบ้างที่เข้าถึงได้ - ส่วนบล็อก
resource
แต่ละรายการที่ระบุโครงสร้างพื้นฐานที่จะสร้างขึ้น
ตั้งค่า provider
คุณต้องตั้งค่า provider
ไม่ว่าผลิตภัณฑ์หรือบริการ Firebase ใดจะเกี่ยวข้อง
สร้างไฟล์การกําหนดค่า Terraform (เช่น ไฟล์
main.tf
) ในไดเรกทอรีในเครื่องในคู่มือนี้ คุณจะใช้ไฟล์การกําหนดค่านี้เพื่อระบุทั้ง
provider
การตั้งค่าและโครงสร้างพื้นฐานทั้งหมดที่คุณต้องการให้ Terraform สร้าง อย่างไรก็ตาม โปรดทราบว่าคุณมีตัวเลือกในการรวมการตั้งค่าผู้ให้บริการดูตัวเลือกวิธีรวมการตั้งค่า
provider
คุณมีตัวเลือกต่อไปนี้ในการรวมการตั้งค่า
provider
เข้ากับการกำหนดค่า Terraform ที่เหลือตัวเลือกที่ 1: รวมไว้ที่ด้านบนของไฟล์การกําหนดค่า
.tf
ของ Terraform ไฟล์เดียว (ตามที่แสดงในคู่มือนี้)- ใช้ตัวเลือกนี้หากคุณเพิ่งเริ่มต้นใช้งาน Terraform หรือเพิ่งลองใช้ Terraform กับ Firebase
ตัวเลือกที่ 2: รวมไว้ในไฟล์
.tf
แยกต่างหาก (เช่น ไฟล์provider.tf
) นอกเหนือจากไฟล์.tf
ที่คุณระบุโครงสร้างพื้นฐานที่จะสร้าง (เช่น ไฟล์main.tf
)- ใช้ตัวเลือกนี้หากคุณเป็นส่วนหนึ่งของทีมขนาดใหญ่ที่ต้องตั้งค่าให้เป็นมาตรฐาน
- เมื่อเรียกใช้คำสั่ง Terraform ทั้งไฟล์
provider.tf
และไฟล์main.tf
จะต้องอยู่ในไดเรกทอรีเดียวกัน
ใส่การตั้งค่า
provider
ต่อไปนี้ที่ด้านบนของไฟล์main.tf
คุณต้องใช้บริการของ
google-beta
เนื่องจากนี่เป็นรุ่นเบต้าของการใช้ Firebase กับ Terraform โปรดใช้ความระมัดระวังเมื่อใช้ในเวอร์ชันที่ใช้งานจริง# Terraform configuration to set up providers by version. terraform { required_providers { google-beta = { source = "hashicorp/google-beta" version = "~> 5.0" } } } # Configures the provider to use the resource block's specified project for quota checks. provider "google-beta" { user_project_override = true } # Configures the provider to not use the resource block's specified project for quota checks. # This provider should only be used during project creation and initializing services. provider "google-beta" { alias = "no_user_project_override" user_project_override = false }
ดูข้อมูลเพิ่มเติมเกี่ยวกับแอตทริบิวต์ที่เกี่ยวข้องกับโปรเจ็กต์ประเภทต่างๆ (รวมถึงสิ่งที่คู่มือนี้เรียกว่า "โปรเจ็กต์ตรวจสอบโควต้า") เมื่อใช้ Terraform กับ Firebase
ไปที่ส่วนถัดไปเพื่อกรอกข้อมูลในไฟล์การกําหนดค่าให้เสร็จสมบูรณ์และระบุโครงสร้างพื้นฐานที่จะสร้าง
ระบุโครงสร้างพื้นฐานที่จะสร้างโดยใช้บล็อก resource
ในไฟล์การกําหนดค่า Terraform (ไฟล์ main.tf
ในคู่มือนี้) คุณต้องระบุโครงสร้างพื้นฐานทั้งหมดที่ต้องการให้ Terraform สร้าง (หมายถึงทรัพยากรทั้งหมดที่ต้องการจัดสรรและบริการทั้งหมดที่ต้องการเปิดใช้) ดูรายการทรัพยากร Firebase ที่รองรับ Terraform ทั้งหมดได้ในคู่มือนี้
เปิดไฟล์
main.tf
ในการตั้งค่า
provider
ให้ใส่การกําหนดค่าresource
ต่อไปนี้ บล็อกตัวอย่างพื้นฐานนี้จะสร้างโปรเจ็กต์ Firebase ใหม่ จากนั้นสร้างแอป Firebase Android ภายในโปรเจ็กต์นั้น
# Terraform configuration to set up providers by version. ... # Configures the provider to use the resource block's specified project for quota checks. ... # Configures the provider to not use the resource block's specified project for quota checks. ... # Creates a new Google Cloud project. resource "google_project" "default" { provider = google-beta.no_user_project_override name = "Project Display Name" project_id = "project-id-for-new-project" # Required for any service that requires the Blaze pricing plan # (like Firebase Authentication with GCIP) billing_account = "000000-000000-000000" # Required for the project to display in any list of Firebase projects. labels = { "firebase" = "enabled" } } # Enables required APIs. resource "google_project_service" "default" { provider = google-beta.no_user_project_override project = google_project.default.project_id for_each = toset([ "cloudbilling.googleapis.com", "cloudresourcemanager.googleapis.com", "firebase.googleapis.com", # Enabling the ServiceUsage API allows the new project to be quota checked from now on. "serviceusage.googleapis.com", ]) service = each.key # Don't disable the service if the resource block is removed by accident. disable_on_destroy = false } # Enables Firebase services for the new project created above. resource "google_firebase_project" "default" { provider = google-beta project = google_project.default.project_id # Waits for the required APIs to be enabled. depends_on = [ google_project_service.default ] } # Creates a Firebase Android App in the new project created above. resource "google_firebase_android_app" "default" { provider = google-beta project = google_project.default.project_id display_name = "My Awesome Android app" package_name = "awesome.package.name" # Wait for Firebase to be enabled in the Google Cloud project before creating this App. depends_on = [ google_firebase_project.default, ] }
ดูไฟล์การกําหนดค่าตัวอย่างเวอร์ชันที่มีคำอธิบายประกอบอย่างละเอียด
หากคุณไม่คุ้นเคยกับโครงสร้างพื้นฐานของโปรเจ็กต์และแอปในฐานะทรัพยากร โปรดอ่านเอกสารประกอบต่อไปนี้
- ทําความเข้าใจโปรเจ็กต์ Firebase
- เอกสารอ้างอิงสําหรับการจัดการโปรเจ็กต์ Firebase
# Terraform configuration to set up providers by version. ... # Configures the provider to use the resource block's specified project for quota checks. ... # Configures the provider to not use the resource block's specified project for quota checks. ... # Creates a new Google Cloud project. resource "google_project" "default" { # Use the provider that enables the setup of quota checks for a new project provider = google-beta.no_user_project_override name = "Project Display Name" // learn more about the project name project_id = "project-id-for-new-project" // learn more about the project ID # Required for any service that requires the Blaze pricing plan # (like Firebase Authentication with GCIP) billing_account = "000000-000000-000000" # Required for the project to display in any list of Firebase projects. labels = { "firebase" = "enabled" // learn more about the Firebase-enabled label } } # Enables required APIs. resource "google_project_service" "default" { # Use the provider without quota checks for enabling APIS provider = google-beta.no_user_project_override project = google_project.default.project_id for_each = toset([ "cloudbilling.googleapis.com", "cloudresourcemanager.googleapis.com", "firebase.googleapis.com", # Enabling the ServiceUsage API allows the new project to be quota checked from now on. "serviceusage.googleapis.com", ]) service = each.key # Don't disable the service if the resource block is removed by accident. disable_on_destroy = false } # Enables Firebase services for the new project created above. # This action essentially "creates a Firebase project" and allows the project to use # Firebase services (like Firebase Authentication) and # Firebase tooling (like the Firebase console). # Learn more about the relationship between Firebase projects and Google Cloud. resource "google_firebase_project" "default" { # Use the provider that performs quota checks from now on provider = google-beta project = google_project.default.project_id # Waits for the required APIs to be enabled. depends_on = [ google_project_service.default ] } # Creates a Firebase Android App in the new project created above. # Learn more about the relationship between Firebase Apps and Firebase projects. resource "google_firebase_android_app" "default" { provider = google-beta project = google_project.default.project_id display_name = "My Awesome Android app" # learn more about an app's display name package_name = "awesome.package.name" # learn more about an app's package name # Wait for Firebase to be enabled in the Google Cloud project before creating this App. depends_on = [ google_firebase_project.default, ] }
ขั้นตอนที่ 2: เรียกใช้คำสั่ง Terraform เพื่อสร้างโครงสร้างพื้นฐานที่ระบุ
หากต้องการจัดสรรทรัพยากรและเปิดใช้บริการที่ระบุไว้ในไฟล์ main.tf
ให้เรียกใช้คำสั่งต่อไปนี้จากไดเรกทอรีเดียวกับไฟล์ main.tf
ดูรายละเอียดเกี่ยวกับคําสั่งเหล่านี้ได้ที่เอกสารประกอบของ Terraform
หากนี่เป็นการใช้งาน Terraform ครั้งแรกในไดเรกทอรี คุณจะต้องเริ่มต้นไดเรกทอรีการกําหนดค่าและติดตั้งผู้ให้บริการ Google Terraform ซึ่งทำได้โดยการเรียกใช้คำสั่งต่อไปนี้
terraform init
สร้างโครงสร้างพื้นฐานที่ระบุไว้ในไฟล์
main.tf
โดยเรียกใช้คำสั่งต่อไปนี้terraform apply
ตรวจสอบว่าทุกอย่างได้รับการจัดสรรหรือเปิดใช้ตามที่คาดไว้
ตัวเลือกที่ 1: ดูการกําหนดค่าที่พิมพ์ในเทอร์มินัลโดยเรียกใช้คําสั่งต่อไปนี้
terraform show
ตัวเลือกที่ 2: ดูโปรเจ็กต์ Firebase ในคอนโซล Firebase
ทรัพยากร Firebase ที่รองรับ Terraform
ทรัพยากร Firebase และ Google ต่อไปนี้รองรับ Terraform และเราจะเพิ่มแหล่งข้อมูลเพิ่มเติมอยู่เรื่อยๆ ดังนั้นหากไม่เห็นทรัพยากรที่ต้องการจัดการด้วย Terraform โปรดกลับมาตรวจสอบในเร็วๆ นี้เพื่อดูว่าทรัพยากรพร้อมใช้งานหรือยัง หรือจะขอทรัพยากรดังกล่าวโดยส่งคำขอแจ้งปัญหาในรีโป GitHub ก็ได้
การจัดการโปรเจ็กต์และแอป Firebase
google_firebase_project
— เปิดใช้บริการ Firebase ในโปรเจ็กต์ Google Cloud ที่มีอยู่แอป Firebase
google_firebase_apple_app
— สร้างหรือจัดการแอป Firebase สำหรับแพลตฟอร์ม Applegoogle_firebase_android_app
— สร้างหรือจัดการแอป Firebase สำหรับ Androidgoogle_firebase_web_app
— สร้างหรือจัดการเว็บแอป Firebase
Firebase Authentication
google_identity_platform_config
— เปิดใช้ Google Cloud Identity Platform (GCIP) (ซึ่งเป็นแบ็กเอนด์ของ Firebase Authentication) และระบุการตั้งค่าการตรวจสอบสิทธิ์ระดับโปรเจ็กต์การกำหนดค่า Firebase Authentication ผ่าน Terraform จำเป็นต้องเปิดใช้ GCIP โปรดอ่านไฟล์
.tf
ตัวอย่างเพื่อดูวิธีตั้งค่า Firebase Authenticationโปรเจ็กต์ที่ Terraform จะเปิดใช้ GCIP และ/หรือ Firebase Authentication ต้องอยู่ในแพ็กเกจราคา Blaze (กล่าวคือ โปรเจ็กต์ต้องมีบัญชี Cloud Billing ที่เชื่อมโยง) ซึ่งทําได้แบบเป็นโปรแกรมโดยการตั้งค่าแอตทริบิวต์
billing_account
ในทรัพยากรgoogle_project
แหล่งข้อมูลนี้ยังเปิดใช้การกำหนดค่าเพิ่มเติม เช่น วิธีการลงชื่อเข้าใช้ในเครื่อง เช่น การตรวจสอบสิทธิ์แบบไม่ระบุตัวตน อีเมล/รหัสผ่าน และโทรศัพท์ รวมถึงการบล็อกฟังก์ชันและโดเมนที่ได้รับอนุญาต
google_identity_platform_default_supported_idp_config
— กำหนดค่าผู้ให้บริการข้อมูลประจำตัวแบบรวมศูนย์ทั่วไป เช่น Google, Facebook หรือ Appleidentity_platform_oauth_idp_config
— กำหนดค่าแหล่งที่มาของผู้ให้บริการข้อมูลประจำตัว (IdP) OAuth ที่กำหนดเองgoogle_identity_platform_inbound_saml_config
— กำหนดค่าการผสานรวม SAML
ยังไม่รองรับ
- การกำหนดค่าการตรวจสอบสิทธิ์แบบหลายปัจจัย (MFA) ผ่าน Terraform
Firebase Data Connect
google_firebase_data_connect_service
— สร้างบริการ Data Connect
Firebase Realtime Database
google_firebase_database_instance
— สร้างอินสแตนซ์ Realtime Database
ยังไม่รองรับ
- การติดตั้งใช้งาน Firebase Realtime Database Security Rules ผ่าน Terraform (ดูวิธีติดตั้งใช้งาน Rules เหล่านี้โดยใช้เครื่องมืออื่นๆ รวมถึงตัวเลือกแบบเป็นโปรแกรม)
Cloud Firestore
google_firestore_database
— สร้างอินสแตนซ์ Cloud Firestoregoogle_firestore_index
— เปิดใช้การค้นหาที่มีประสิทธิภาพสําหรับ Cloud Firestoregoogle_firestore_document
— สร้างอินสแตนซ์ Cloud Firestore ด้วยเอกสารที่เฉพาะเจาะจงในคอลเล็กชันสำคัญ: อย่าใช้ข้อมูลผู้ใช้ปลายทางหรือข้อมูลเวอร์ชันที่ใช้งานจริงในเอกสารข้อมูลเริ่มต้นนี้
Cloud Storage for Firebase
google_firebase_storage_bucket
— ทำให้ที่เก็บข้อมูล Cloud Storage ที่มีอยู่เข้าถึงได้สำหรับ Firebase SDK, การตรวจสอบสิทธิ์ และ Firebase Security Rulesgoogle_storage_bucket_object
— เพิ่มออบเจ็กต์ลงในที่เก็บข้อมูล Cloud Storageสำคัญ: อย่าใช้ข้อมูลผู้ใช้ปลายทางหรือข้อมูลเวอร์ชันที่ใช้งานจริงในไฟล์นี้
Firebase Security Rules (สำหรับ Cloud Firestore และ Cloud Storage)
โปรดทราบว่า Firebase Realtime Database ใช้ระบบการจัดสรรอื่นสําหรับ Firebase Security Rules
google_firebaserules_ruleset
— กำหนด Firebase Security Rules ที่ใช้กับอินสแตนซ์ Cloud Firestore หรือที่เก็บข้อมูล Cloud Storagegoogle_firebaserules_release
- ติดตั้งใช้งานชุดกฎที่เฉพาะเจาะจงในอินสแตนซ์ Cloud Firestore หรือที่เก็บข้อมูล Cloud Storage
Firebase App Check
google_firebase_app_check_service_config
— เปิดใช้การบังคับใช้ App Check สําหรับบริการgoogle_firebase_app_check_app_attest_config
— ลงทะเบียนแอปแพลตฟอร์ม Apple กับผู้ให้บริการ App Attestgoogle_firebase_app_check_device_check_config
— ลงทะเบียนแอปแพลตฟอร์ม Apple กับผู้ให้บริการ DeviceCheckgoogle_firebase_app_check_play_integrity_config
— ลงทะเบียนแอป Android กับผู้ให้บริการ Play Integritygoogle_firebase_app_check_recaptcha_enterprise_config
— ลงทะเบียนเว็บแอปกับผู้ให้บริการ reCAPTCHA Enterprisegoogle_firebase_app_check_recaptcha_v3_config
— ลงทะเบียนเว็บแอปกับผู้ให้บริการ reCAPTCHA v3google_firebase_app_check_debug_token
— ใช้โทเค็นการแก้ไขข้อบกพร่องสำหรับการทดสอบ
Firebase Extensions
google_firebase_extensions_instance
— ติดตั้งหรืออัปเดตอินสแตนซ์ของ Firebase Extension
ตัวอย่างไฟล์การกําหนดค่า Terraform สําหรับกรณีการใช้งานทั่วไป
ตั้งค่า Firebase Authentication ด้วย GCIP
การกําหนดค่านี้จะสร้างโปรเจ็กต์ Google Cloud ใหม่, เชื่อมโยงโปรเจ็กต์กับบัญชี Cloud Billing (ต้องใช้แพ็กเกจราคา Blaze สําหรับ Firebase Authentication ที่มี GCIP), เปิดใช้บริการ Firebase สําหรับโปรเจ็กต์, ตั้งค่า Firebase Authentication ด้วย GCIP และลงทะเบียนแอป 3 ประเภทที่แตกต่างกันกับโปรเจ็กต์
โปรดทราบว่าคุณต้องเปิดใช้ GCIP เพื่อตั้งค่า Firebase Authentication ผ่าน Terraform
# Creates a new Google Cloud project. resource "google_project" "auth" { provider = google-beta.no_user_project_override folder_id = "folder-id-for-new-project" name = "Project Display Name" project_id = "project-id-for-new-project" # Associates the project with a Cloud Billing account # (required for Firebase Authentication with GCIP). billing_account = "000000-000000-000000" # Required for the project to display in a list of Firebase projects. labels = { "firebase" = "enabled" } } # Enables required APIs. resource "google_project_service" "auth" { provider = google-beta.no_user_project_override project = google_project.auth.project_id for_each = toset([ "cloudbilling.googleapis.com", "cloudresourcemanager.googleapis.com", "serviceusage.googleapis.com", "identitytoolkit.googleapis.com", ]) service = each.key # Don't disable the service if the resource block is removed by accident. disable_on_destroy = false } # Enables Firebase services for the new project created above. resource "google_firebase_project" "auth" { provider = google-beta project = google_project.auth.project_id depends_on = [ google_project_service.auth, ] } # Creates an Identity Platform config. # Also enables Firebase Authentication with Identity Platform in the project if not. resource "google_identity_platform_config" "auth" { provider = google-beta project = google_project.auth.project_id # Auto-deletes anonymous users autodelete_anonymous_users = true # Configures local sign-in methods, like anonymous, email/password, and phone authentication. sign_in { allow_duplicate_emails = true anonymous { enabled = true } email { enabled = true password_required = false } phone_number { enabled = true test_phone_numbers = { "+11231231234" = "000000" } } } # Sets an SMS region policy. sms_region_config { allowlist_only { allowed_regions = [ "US", "CA", ] } } # Configures blocking functions. blocking_functions { triggers { event_type = "beforeSignIn" function_uri = "https://us-east1-${google_project.auth.project_id}.cloudfunctions.net/before-sign-in" } forward_inbound_credentials { refresh_token = true access_token = true id_token = true } } # Configures a temporary quota for new signups for anonymous, email/password, and phone number. quota { sign_up_quota_config { quota = 1000 start_time = "" quota_duration = "7200s" } } # Configures authorized domains. authorized_domains = [ "localhost", "${google_project.auth.project_id}.firebaseapp.com", "${google_project.auth.project_id}.web.app", ] # Wait for identitytoolkit.googleapis.com to be enabled before initializing Authentication. depends_on = [ google_project_service.auth, ] } # Creates a Firebase Android App in the new project created above. resource "google_firebase_android_app" "auth" { provider = google-beta project = google_project.auth.project_id display_name = "My Android app" package_name = "android.package.name" # Wait for Firebase to be enabled in the Google Cloud project before creating this App. depends_on = [ google_firebase_project.auth, ] } # Creates a Firebase Apple-platforms App in the new project created above. resource "google_firebase_apple_app" "auth" { provider = google-beta project = google_project.auth.project_id display_name = "My Apple app" bundle_id = "apple.app.12345" # Wait for Firebase to be enabled in the Google Cloud project before creating this App. depends_on = [ google_firebase_project.auth, ] } # Creates a Firebase Web App in the new project created above. resource "google_firebase_web_app" "auth" { provider = google-beta project = google_project.auth.project_id display_name = "My Web app" # The other App types (Android and Apple) use "DELETE" by default. # Web apps don't use "DELETE" by default due to backward-compatibility. deletion_policy = "DELETE" # Wait for Firebase to be enabled in the Google Cloud project before creating this App. depends_on = [ google_firebase_project.auth, ] }
จัดสรรบริการ Firebase Data Connect
การกําหนดค่านี้จะสร้างโปรเจ็กต์ Google Cloud ใหม่ เปิดใช้บริการ Firebase สําหรับโปรเจ็กต์ และจัดสรรบริการ Data Connect
# Creates a new Google Cloud project. resource "google_project" "dataconnect" { provider = google-beta.no_user_project_override folder_id = "folder-id-for-new-project" name = "Project Display Name" project_id = "project-id-for-new-project" # Associates the project with a Cloud Billing account # (required to use Firebase Data Connect). billing_account = "000000-000000-000000" # Required for the project to display in a list of Firebase projects. labels = { "firebase" = "enabled" } } # Enables required APIs. resource "google_project_service" "services" { provider = google-beta.no_user_project_override project = google_project.dataconnect.project_id for_each = toset([ "serviceusage.googleapis.com", "cloudresourcemanager.googleapis.com", "firebasedataconnect.googleapis.com", ]) service = each.key # Don't disable the service if the resource block is removed by accident. disable_on_destroy = false } # Enables Firebase services for the new project created earlier. resource "google_firebase_project" "dataconnect" { provider = google-beta project = google_project.dataconnect.project_id depends_on = [google_project_service.services] } # Create a Firebase Data Connect service resource "google_firebase_data_connect_service" "dataconnect-default" { project = google_firebase_project.dataconnect.project location = "name-of-region-for-service" service_id = "${google_firebase_project.dataconnect.project}-default-fdc" deletion_policy = "DEFAULT" }
จัดสรรอินสแตนซ์ Firebase Realtime Database เริ่มต้น
การกําหนดค่านี้จะสร้างโปรเจ็กต์ Google Cloud ใหม่, เปิดใช้บริการ Firebase สําหรับโปรเจ็กต์, จัดสรรอินสแตนซ์ Realtime Database เริ่มต้นของโปรเจ็กต์ และลงทะเบียนแอป 3 ประเภทที่แตกต่างกันกับโปรเจ็กต์
# Creates a new Google Cloud project. resource "google_project" "rtdb" { provider = google-beta.no_user_project_override folder_id = "folder-id-for-new-project" name = "Project Display Name" project_id = "project-id-for-new-project" # Required for the project to display in a list of Firebase projects. labels = { "firebase" = "enabled" } } # Enables required APIs. resource "google_project_service" "rtdb" { provider = google-beta.no_user_project_override project = google_project.rtdb.project_id for_each = toset([ "serviceusage.googleapis.com", "cloudresourcemanager.googleapis.com", "firebasedatabase.googleapis.com", ]) service = each.key # Don't disable the service if the resource block is removed by accident. disable_on_destroy = false } # Enables Firebase services for the new project created above. resource "google_firebase_project" "rtdb" { provider = google-beta project = google_project.rtdb.project_id } # Provisions the default Realtime Database default instance. resource "google_firebase_database_instance" "database" { provider = google-beta project = google_project.rtdb.project_id # See available locations: https://firebase.google.com/docs/database/locations region = "name-of-region" # This value will become the first segment of the database's URL. instance_id = "${google_project.rtdb.project_id}-default-rtdb" type = "DEFAULT_DATABASE" # Wait for Firebase to be enabled in the Google Cloud project before initializing Realtime Database. depends_on = [ google_firebase_project.rtdb, ] } # Creates a Firebase Android App in the new project created above. resource "google_firebase_android_app" "rtdb" { provider = google-beta project = google_project.rtdb.project_id display_name = "My Android app" package_name = "android.package.name" # Wait for Firebase to be enabled in the Google Cloud project before creating this App. depends_on = [ google_firebase_project.rtdb, ] } # Creates a Firebase Apple-platforms App in the new project created above. resource "google_firebase_apple_app" "rtdb" { provider = google-beta project = google_project.rtdb.project_id display_name = "My Apple app" bundle_id = "apple.app.12345" # Wait for Firebase to be enabled in the Google Cloud project before creating this App. depends_on = [ google_firebase_project.rtdb, ] } # Creates a Firebase Web App in the new project created above. resource "google_firebase_web_app" "rtdb" { provider = google-beta project = google_project.rtdb.project_id display_name = "My Web app" # The other App types (Android and Apple) use "DELETE" by default. # Web apps don't use "DELETE" by default due to backward-compatibility. deletion_policy = "DELETE" # Wait for Firebase to be enabled in the Google Cloud project before creating this App. depends_on = [ google_firebase_project.rtdb, ] }
จัดสรรอินสแตนซ์ Firebase Realtime Database รายการ
การกําหนดค่านี้จะสร้างโปรเจ็กต์ Google Cloud ใหม่ เชื่อมโยงโปรเจ็กต์กับบัญชี Cloud Billing (ต้องใช้แพ็กเกจราคา Blaze สําหรับอินสแตนซ์ Realtime Database หลายรายการ) เปิดใช้บริการ Firebase สําหรับโปรเจ็กต์ จัดสรรอินสแตนซ์ Realtime Database หลายรายการ (รวมถึงอินสแตนซ์ Realtime Database เริ่มต้นของโปรเจ็กต์) และลงทะเบียนแอป 3 ประเภทที่แตกต่างกันกับโปรเจ็กต์
# Creates a new Google Cloud project. resource "google_project" "rtdb-multi" { provider = google-beta.no_user_project_override folder_id = "folder-id-for-new-project" name = "Project Display Name" project_id = "project-id-for-new-project" # Associate the project with a Cloud Billing account # (required for multiple Realtime Database instances). billing_account = "000000-000000-000000" # Required for the project to display in a list of Firebase projects. labels = { "firebase" = "enabled" } } # Enables required APIs. resource "google_project_service" "rtdb-multi" { provider = google-beta.no_user_project_override project = google_project.rtdb-multi.project_id for_each = toset([ "cloudbilling.googleapis.com", "serviceusage.googleapis.com", "cloudresourcemanager.googleapis.com", "firebasedatabase.googleapis.com", ]) service = each.key # Don't disable the service if the resource block is removed by accident. disable_on_destroy = false } # Enables Firebase services for the new project created above. resource "google_firebase_project" "rtdb-multi" { provider = google-beta project = google_project.rtdb-multi.project_id } # Provisions the default Realtime Database default instance. resource "google_firebase_database_instance" "database-default" { provider = google-beta project = google_project.rtdb-multi.project_id # See available locations: https://firebase.google.com/docs/database/locations region = "name-of-region" # This value will become the first segment of the database's URL. instance_id = "${google_project.rtdb-multi.project_id}-default-rtdb" type = "DEFAULT_DATABASE" # Wait for Firebase to be enabled in the Google Cloud project before initializing Realtime Database. depends_on = [ google_firebase_project.rtdb-multi, ] } # Provisions an additional Realtime Database instance. resource "google_firebase_database_instance" "database-additional" { provider = google-beta project = google_project.rtdb-multi.project_id # See available locations: https://firebase.google.com/docs/projects/locations#rtdb-locations # This location doesn't need to be the same as the default database instance. region = "name-of-region" # This value will become the first segment of the database's URL. instance_id = "name-of-additional-database-instance" type = "USER_DATABASE" # Wait for Firebase to be enabled in the Google Cloud project before initializing Realtime Database. depends_on = [ google_firebase_project.rtdb-multi, ] } # Creates a Firebase Android App in the new project created above. resource "google_firebase_android_app" "rtdb-multi" { provider = google-beta project = google_project.rtdb-multi.project_id display_name = "My Android app" package_name = "android.package.name" # Wait for Firebase to be enabled in the Google Cloud project before creating this App. depends_on = [ google_firebase_project.rtdb-multi, ] } # Creates a Firebase Apple-platforms App in the new project created above. resource "google_firebase_apple_app" "rtdb-multi" { provider = google-beta project = google_project.rtdb-multi.project_id display_name = "My Apple app" bundle_id = "apple.app.12345" # Wait for Firebase to be enabled in the Google Cloud project before creating this App. depends_on = [ google_firebase_project.rtdb-multi, ] } # Creates a Firebase Web App in the new project created above. resource "google_firebase_web_app" "rtdb-multi" { provider = google-beta project = google_project.rtdb-multi.project_id display_name = "My Web app" # The other App types (Android and Apple) use "DELETE" by default. # Web apps don't use "DELETE" by default due to backward-compatibility. deletion_policy = "DELETE" # Wait for Firebase to be enabled in the Google Cloud project before creating this App. depends_on = [ google_firebase_project.rtdb-multi, ] }
จัดสรรอินสแตนซ์ Cloud Firestore เริ่มต้น
การกําหนดค่านี้จะสร้างโปรเจ็กต์ Google Cloud ใหม่, เปิดใช้บริการ Firebase สําหรับโปรเจ็กต์, จัดสรรอินสแตนซ์ Cloud Firestore เริ่มต้นของโปรเจ็กต์ และลงทะเบียนแอป 3 ประเภทที่แตกต่างกันกับโปรเจ็กต์
นอกจากนี้ ยังจัดสรร Firebase Security Rules สําหรับอินสแตนซ์ Cloud Firestore เริ่มต้น สร้างดัชนี Cloud Firestore และเพิ่มเอกสาร Cloud Firestore ที่มีข้อมูลเริ่มต้น
# Creates a new Google Cloud project. resource "google_project" "firestore" { provider = google-beta.no_user_project_override folder_id = "folder-id-for-new-project" name = "Project Display Name" project_id = "project-id-for-new-project" # Required for the project to display in a list of Firebase projects. labels = { "firebase" = "enabled" } } # Enables required APIs. resource "google_project_service" "firestore" { provider = google-beta.no_user_project_override project = google_project.firestore.project_id for_each = toset([ "cloudresourcemanager.googleapis.com", "serviceusage.googleapis.com", "firestore.googleapis.com", "firebaserules.googleapis.com", ]) service = each.key # Don't disable the service if the resource block is removed by accident. disable_on_destroy = false } # Enables Firebase services for the new project created above. resource "google_firebase_project" "firestore" { provider = google-beta project = google_project.firestore.project_id } # Provisions the Firestore database instance. resource "google_firestore_database" "firestore" { provider = google-beta project = google_project.firestore.project_id name = "(default)" # See available locations: https://firebase.google.com/docs/firestore/locations location_id = "name-of-region" # "FIRESTORE_NATIVE" is required to use Firestore with Firebase SDKs, authentication, and Firebase Security Rules. type = "FIRESTORE_NATIVE" concurrency_mode = "OPTIMISTIC" # Wait for Firebase to be enabled in the Google Cloud project before initializing Firestore. depends_on = [ google_firebase_project.firestore, ] } # Creates a ruleset of Firestore Security Rules from a local file. resource "google_firebaserules_ruleset" "firestore" { provider = google-beta project = google_project.firestore.project_id source { files { name = "firestore.rules" # Write security rules in a local file named "firestore.rules". # Learn more: https://firebase.google.com/docs/firestore/security/get-started content = file("firestore.rules") } } # Wait for Firestore to be provisioned before creating this ruleset. depends_on = [ google_firestore_database.firestore, ] } # Releases the ruleset for the Firestore instance. resource "google_firebaserules_release" "firestore" { provider = google-beta name = "cloud.firestore" # must be cloud.firestore ruleset_name = google_firebaserules_ruleset.firestore.name project = google_project.firestore.project_id # Wait for Firestore to be provisioned before releasing the ruleset. depends_on = [ google_firestore_database.firestore, ] } # Adds a new Firestore index. resource "google_firestore_index" "indexes" { provider = google-beta project = google_project.firestore.project_id collection = "quiz" query_scope = "COLLECTION" fields { field_path = "question" order = "ASCENDING" } fields { field_path = "answer" order = "ASCENDING" } # Wait for Firestore to be provisioned before adding this index. depends_on = [ google_firestore_database.firestore, ] } # Adds a new Firestore document with seed data. # Don't use real end-user or production data in this seed document. resource "google_firestore_document" "doc" { provider = google-beta project = google_project.firestore.project_id collection = "quiz" document_id = "question-1" fields = "{\"question\":{\"stringValue\":\"Favorite Database\"},\"answer\":{\"stringValue\":\"Firestore\"}}" # Wait for Firestore to be provisioned before adding this document. depends_on = [ google_firestore_database.firestore, ] } # Creates a Firebase Android App in the new project created above. resource "google_firebase_android_app" "firestore" { provider = google-beta project = google_project.firestore.project_id display_name = "My Android app" package_name = "android.package.name" # Wait for Firebase to be enabled in the Google Cloud project before creating this App. depends_on = [ google_firebase_project.firestore, ] } # Creates a Firebase Apple-platforms App in the new project created above. resource "google_firebase_apple_app" "firestore" { provider = google-beta project = google_project.firestore.project_id display_name = "My Apple app" bundle_id = "apple.app.12345" # Wait for Firebase to be enabled in the Google Cloud project before creating this App. depends_on = [ google_firebase_project.firestore, ] } # Creates a Firebase Web App in the new project created above. resource "google_firebase_web_app" "firestore" { provider = google-beta project = google_project.firestore.project_id display_name = "My Web app" # The other App types (Android and Apple) use "DELETE" by default. # Web apps don't use "DELETE" by default due to backward-compatibility. deletion_policy = "DELETE" # Wait for Firebase to be enabled in the Google Cloud project before creating this App. depends_on = [ google_firebase_project.firestore, ] }
นี่คือชุดกฎของ Cloud Firestore Security Rules ที่ควรอยู่ในไฟล์ในเครื่องที่มีชื่อว่า firestore.rules
rules_version = '2'; service cloud.firestore { match /databases/{database}/documents { match /some_collection/{document} { allow read, create, update: if request.auth != null; } } }
จัดสรรที่เก็บข้อมูล Cloud Storage เริ่มต้น
จัดสรรที่เก็บข้อมูล Cloud Storage เพิ่มเติม
การกําหนดค่านี้จะสร้างโปรเจ็กต์ Google Cloud ใหม่ เชื่อมโยงโปรเจ็กต์กับบัญชี Cloud Billing (ต้องใช้แพ็กเกจราคา Blaze สําหรับที่เก็บข้อมูลเพิ่มเติม) เปิดใช้บริการ Firebase สําหรับโปรเจ็กต์ จัดสรรที่เก็บข้อมูล Cloud Storage เพิ่มเติมที่ไม่ใช่ค่าเริ่มต้น และลงทะเบียนแอป 3 ประเภทกับโปรเจ็กต์
รวมถึงจัดสรร Firebase Security Rules สำหรับที่เก็บข้อมูล Cloud Storage แต่ละรายการ และอัปโหลดไฟล์ไปยังที่เก็บข้อมูล Cloud Storage รายการใดรายการหนึ่ง
# Creates a new Google Cloud project. resource "google_project" "storage-multi" { provider = google-beta.no_user_project_override folder_id = "folder-id-for-new-project" name = "Project Display Name" project_id = "project-id-for-new-project" # Associates the project with a Cloud Billing account # (required for multiple Cloud Storage buckets). billing_account = "000000-000000-000000" # Required for the project to display in a list of Firebase projects. labels = { "firebase" = "enabled" } } # Enables required APIs. resource "google_project_service" "storage-multi" { provider = google-beta.no_user_project_override project = google_project.storage-multi.project_id for_each = toset([ "cloudbilling.googleapis.com", "serviceusage.googleapis.com", "cloudresourcemanager.googleapis.com", "firebaserules.googleapis.com", "firebasestorage.googleapis.com", "storage.googleapis.com", ]) service = each.key # Don't disable the service if the resource block is removed by accident. disable_on_destroy = false } # Enables Firebase services for the new project created above. resource "google_firebase_project" "storage-multi" { provider = google-beta project = google_project.storage-multi.project_id } # Provisions a Cloud Storage bucket. resource "google_storage_bucket" "bucket-1" { provider = google-beta project = google_project.storage-multi.project_id name = "name-of-storage-bucket" # See available locations: https://cloud.google.com/storage/docs/locations#available-locations location = "name-of-region-for-bucket" } # Provisions an additional Cloud Storage bucket. resource "google_storage_bucket" "bucket-2" { provider = google-beta project = google_project.storage-multi.project_id name = "name-of-additional-storage-bucket" # See available locations: https://cloud.google.com/storage/docs/locations#available-locations # This location does not need to be the same as the existing Storage bucket. location = "name-of-region-for-additional-bucket" } # Makes the first Storage bucket accessible for Firebase SDKs, authentication, and Firebase Security Rules. resource "google_firebase_storage_bucket" "bucket-1" { provider = google-beta project = google_project.storage-multi.project_id bucket_id = google_storage_bucket.bucket-1.name } # Makes the additional Storage bucket accessible for Firebase SDKs, authentication, and Firebase Security Rules. resource "google_firebase_storage_bucket" "bucket-2" { provider = google-beta project = google_project.storage-multi.project_id bucket_id = google_storage_bucket.bucket-2.name } # Creates a ruleset of Firebase Security Rules from a local file. resource "google_firebaserules_ruleset" "storage-multi" { provider = google-beta project = google_project.storage-multi.project_id source { files { # Write security rules in a local file named "storage.rules" # Learn more: https://firebase.google.com/docs/storage/security/get-started name = "storage.rules" content = file("storage.rules") } } # Wait for the Storage buckets to be provisioned before creating this ruleset. depends_on = [ google_firebase_project.storage-multi, ] } # Releases the ruleset to the first Storage bucket. resource "google_firebaserules_release" "bucket-1" { provider = google-beta name = "firebase.storage/${google_storage_bucket.bucket-1.name}" ruleset_name = "projects/${google_project.storage-multi.project_id}/rulesets/${google_firebaserules_ruleset.storage-multi.name}" project = google_project.storage-multi.project_id } # Releases the ruleset to the additional Storage bucket. resource "google_firebaserules_release" "bucket-2" { provider = google-beta name = "firebase.storage/${google_storage_bucket.bucket-2.name}" ruleset_name = "projects/${google_project.storage-multi.project_id}/rulesets/${google_firebaserules_ruleset.storage-multi.name}" project = google_project.storage-multi.project_id } # Uploads a new file to the first Storage bucket. # Do not use real end-user or production data in this file. resource "google_storage_bucket_object" "cat-picture-multi" { provider = google-beta name = "cat.png" source = "path/to/cat.png" bucket = google_storage_bucket.bucket-1.name } # Creates a Firebase Android App in the new project created above. resource "google_firebase_android_app" "storage-multi" { provider = google-beta project = google_project.storage-multi.project_id display_name = "My Android app" package_name = "android.package.name" # Wait for Firebase to be enabled in the Google Cloud project before creating this App. depends_on = [ google_firebase_project.storage-multi, ] } # Creates a Firebase Apple-platforms App in the new project created above. resource "google_firebase_apple_app" "storage-multi" { provider = google-beta project = google_project.storage-multi.project_id display_name = "My Apple app" bundle_id = "apple.app.12345" # Wait for Firebase to be enabled in the Google Cloud project before creating this App. depends_on = [ google_firebase_project.storage-multi, ] } # Creates a Firebase Web App in the new project created above. resource "google_firebase_web_app" "storage-multi" { provider = google-beta project = google_project.storage-multi.project_id display_name = "My Web app" # Wait for Firebase to be enabled in the Google Cloud project before creating this App. depends_on = [ google_firebase_project.storage-multi, ] }
นี่คือชุดกฎของ Cloud Storage Security Rules ที่ควรอยู่ในไฟล์ในเครื่องที่มีชื่อว่า storage.rules
rules_version = '2'; service firebase.storage { match /b/{bucket}/o { match /some_folder/{fileName} { allow read, write: if request.auth != null; } } }
ปกป้องทรัพยากร API ด้วย Firebase App Check
การกําหนดค่านี้จะสร้างโปรเจ็กต์ Google Cloud ใหม่, เปิดใช้บริการ Firebase สําหรับโปรเจ็กต์ และตั้งค่าและเปิดใช้การบังคับใช้ Firebase App Check สําหรับ Cloud Firestore เพื่อให้เข้าถึงได้จากแอป Android เท่านั้น
# Creates a new Google Cloud project. resource "google_project" "appcheck" { provider = google-beta.no_user_project_override folder_id = "folder-id-for-new-project" name = "Project Display Name" project_id = "project-id-for-new-project" # Required for the project to display in a list of Firebase projects. labels = { "firebase" = "enabled" } } # Enables required APIs. resource "google_project_service" "services" { provider = google-beta.no_user_project_override project = google_project.appcheck.project_id for_each = toset([ "cloudresourcemanager.googleapis.com", "firebase.googleapis.com", "firebaseappcheck.googleapis.com", "firestore.googleapis.com", "serviceusage.googleapis.com", ]) service = each.key # Don't disable the service if the resource block is removed by accident. disable_on_destroy = false } # Enables Firebase services for the new project created earlier. resource "google_firebase_project" "appcheck" { provider = google-beta project = google_project.appcheck.project_id depends_on = [google_project_service.services] } # Provisions the Firestore database instance. resource "google_firestore_database" "database" { provider = google-beta project = google_firebase_project.appcheck.project name = "(default)" # See available locations: https://firebase.google.com/docs/projects/locations#default-cloud-location location_id = "name-of-region" # "FIRESTORE_NATIVE" is required to use Firestore with Firebase SDKs, authentication, and Firebase Security Rules. type = "FIRESTORE_NATIVE" concurrency_mode = "OPTIMISTIC" # Wait for Firebase to be enabled in the Google Cloud project before initializing Firestore. depends_on = [ google_firebase_project.appcheck, ] } # Creates a Firebase Android App in the new project created earlier. resource "google_firebase_android_app" "appcheck" { provider = google-beta project = google_firebase_project.appcheck.project display_name = "Play Integrity app" package_name = "package.name.playintegrity" sha256_hashes = [ # TODO: insert your Android app's SHA256 certificate ] } # It takes a while for App Check to recognize the new app # If your app already exists, you don't have to wait 30 seconds. resource "time_sleep" "wait_30s" { depends_on = [google_firebase_android_app.appcheck] create_duration = "30s" } # Register the Android app with the Play Integrity provider resource "google_firebase_app_check_play_integrity_config" "appcheck" { provider = google-beta project = google_firebase_project.appcheck.project app_id = google_firebase_android_app.appcheck.app_id depends_on = [time_sleep.wait_30s, google_firestore_database.database] lifecycle { precondition { condition = length(google_firebase_android_app.appcheck.sha256_hashes) > 0 error_message = "Provide a SHA-256 certificate on the Android App to use App Check" } } } # Enable enforcement of App Check for Firestore resource "google_firebase_app_check_service_config" "firestore" { provider = google-beta project = google_firebase_project.appcheck.project service_id = "firestore.googleapis.com" depends_on = [google_project_service.services] }
ติดตั้ง Firebase Extension 1 อินสแตนซ์
การกําหนดค่านี้จะสร้างโปรเจ็กต์ Google Cloud ใหม่ เปิดใช้บริการ Firebase สําหรับโปรเจ็กต์ และติดตั้งอินสแตนซ์ใหม่ของ Firebase Extension ในโปรเจ็กต์ หากอินสแตนซ์มีอยู่แล้ว ระบบจะอัปเดตพารามิเตอร์ตามค่าที่ระบุในการกําหนดค่า
# Creates a new Google Cloud project. resource "google_project" "extensions" { provider = google-beta.no_user_project_override folder_id = "folder-id-for-new-project" name = "Project Display Name" project_id = "project-id-for-new-project" # Associates the project with a Cloud Billing account # (required to use Firebase Extensions). billing_account = "000000-000000-000000" # Required for the project to display in a list of Firebase projects. labels = { "firebase" = "enabled" } } # Enables required APIs. resource "google_project_service" "extensions" { provider = google-beta.no_user_project_override project = google_project.extensions.project_id for_each = toset([ "cloudbilling.googleapis.com", "cloudresourcemanager.googleapis.com", "serviceusage.googleapis.com", "firebase.googleapis.com", "firebaseextensions.googleapis.com", ]) service = each.key # Don't disable the service if the resource block is removed by accident. disable_on_destroy = false } # Enables Firebase services for the new project created above. resource "google_firebase_project" "extensions" { provider = google-beta project = google_project.extensions.project_id depends_on = [ google_project_service.extensions, ] } # Installs an instance of the "Translate Text in Firestore" extension. # Or updates the extension if the specified instance already exists. resource "google_firebase_extensions_instance" "translation" { provider = google-beta project = google_project.extensions.project_id instance_id = "translate-text-in-firestore" config { extension_ref = "firebase/firestore-translate-text" params = { COLLECTION_PATH = "posts/comments/translations" DO_BACKFILL = true LANGUAGES = "ar,en,es,de,fr" INPUT_FIELD_NAME = "input" LANGUAGES_FIELD_NAME = "languages" OUTPUT_FIELD_NAME = "translated" } system_params = { "firebaseextensions.v1beta.function/location" = "us-central1" "firebaseextensions.v1beta.function/memory" = "256" "firebaseextensions.v1beta.function/minInstances" = "0" "firebaseextensions.v1beta.function/vpcConnectorEgressSettings" = "VPC_CONNECTOR_EGRESS_SETTINGS_UNSPECIFIED" } } }
การแก้ปัญหาและคำถามที่พบบ่อย
คุณต้องการดูข้อมูลเพิ่มเติมเกี่ยวกับแอตทริบิวต์ต่างๆ ที่เกี่ยวข้องกับโปรเจ็กต์ (เช่น project
และ user_project_override
)
คู่มือนี้ใช้แอตทริบิวต์ Terraform ต่อไปนี้เมื่อทำงานกับ "โปรเจ็กต์"
project
ภายในบล็อกresource
แนะนํา: ใส่แอตทริบิวต์
project
ในแต่ละบล็อกresource
เมื่อเป็นไปได้การระบุแอตทริบิวต์โปรเจ็กต์จะทำให้ Terraform สร้างโครงสร้างพื้นฐานที่ระบุไว้ในบล็อกทรัพยากรภายในโปรเจ็กต์ที่ระบุ คู่มือนี้และไฟล์การกําหนดค่าตัวอย่างทั้งหมดใช้แนวทางนี้
ดูเอกสารประกอบอย่างเป็นทางการของ Terraform เกี่ยวกับ
project
user_project_override
ภายในบล็อกprovider
สำหรับการจองทรัพยากรส่วนใหญ่ คุณควรใช้
user_project_override = true
ซึ่งหมายถึงการตรวจสอบโควต้ากับโปรเจ็กต์ Firebase ของคุณเอง อย่างไรก็ตาม หากต้องการตั้งค่าโปรเจ็กต์ใหม่ให้ยอมรับการตรวจสอบโควต้า คุณต้องใช้user_project_override = false
ก่อนดูเอกสารประกอบอย่างเป็นทางการของ Terraform เกี่ยวกับ
user_project_override
คุณได้รับข้อผิดพลาดนี้
generic::permission_denied: Firebase Tos Not Accepted
ตรวจสอบว่าบัญชีผู้ใช้ที่คุณใช้เรียกใช้gcloud CLIคำสั่งได้ยอมรับข้อกำหนดในการให้บริการของ Firebase (ข้อกำหนดในการให้บริการของ Firebase)
คุณตรวจสอบได้โดยการใช้เบราว์เซอร์ที่ลงชื่อเข้าใช้บัญชีผู้ใช้แล้วพยายามดูโปรเจ็กต์ Firebase ที่มีอยู่ในคอนโซล Firebase หากดูโปรเจ็กต์ Firebase ที่มีอยู่ได้ แสดงว่าบัญชีผู้ใช้ยอมรับข้อกําหนดในการให้บริการของ Firebase แล้ว
หากดูโปรเจ็กต์ Firebase ที่มีอยู่ไม่ได้ แสดงว่าบัญชีผู้ใช้อาจยังไม่ได้ยอมรับข้อกําหนดในการให้บริการของ Firebase วิธีแก้ไขคือสร้างโปรเจ็กต์ Firebase ใหม่ผ่านคอนโซล Firebase และยอมรับข้อกําหนดในการให้บริการของ Firebase เป็นส่วนหนึ่งของการสร้างโปรเจ็กต์ คุณสามารถลบโปรเจ็กต์นี้ทันทีผ่านการตั้งค่าโปรเจ็กต์ในคอนโซล
หลังจากเรียกใช้
terraform apply
คุณได้รับข้อผิดพลาดนี้
generic::permission_denied: IAM authority does not have the
permission
โปรดรอสักครู่แล้วลองเรียกใช้ terraform apply
อีกครั้ง
การสร้างทรัพยากรไม่สําเร็จ แต่เมื่อคุณเรียกใช้ terraform apply
อีกครั้ง ระบบจะแสดงข้อความว่า ALREADY_EXISTS
ซึ่งอาจเกิดจากความล่าช้าในการนำไปใช้งานในระบบต่างๆ โปรดลองแก้ไขปัญหานี้ด้วยการนําเข้าทรัพยากรไปยังสถานะ Terraform โดยเรียกใช้ terraform import
จากนั้นลองเรียกใช้ terraform apply
อีกครั้ง
ดูวิธีนําเข้าทรัพยากรแต่ละรายการได้ในส่วน "นําเข้า" ของเอกสารประกอบ Terraform (เช่น เอกสารประกอบ"นําเข้า" สําหรับ Cloud Firestore)
เมื่อทำงานกับ Cloud Firestore คุณได้รับข้อผิดพลาด Error creating Index: googleapi:
Error 409;...Concurrent access -- try again
ตามข้อผิดพลาดที่แสดง Terraform อาจพยายามจัดสรรดัชนีหลายรายการ และ/หรือสร้างเอกสารพร้อมกัน และพบข้อผิดพลาดเกี่ยวกับการทำงานพร้อมกัน
ลองเรียกใช้ terraform apply
อีกครั้ง
คุณได้รับข้อผิดพลาดนี้
"you may need to specify 'X-Goog-User-Project' HTTP header for quota and
billing purposes"
ข้อผิดพลาดนี้หมายความว่า Terraform ไม่ทราบว่าต้องตรวจสอบโควต้ากับโปรเจ็กต์ใด หากต้องการแก้ปัญหา ให้ตรวจสอบรายการต่อไปนี้ในบล็อก resource
- ตรวจสอบว่าคุณได้ระบุค่าสำหรับแอตทริบิวต์
project
แล้ว - ตรวจสอบว่าคุณใช้ผู้ให้บริการที่มี
user_project_override = true
(ไม่มีอีเมลแทน) ซึ่งในตัวอย่าง Firebase คือgoogle-beta
เมื่อสร้างโปรเจ็กต์ Google Cloud ใหม่ คุณจะได้รับข้อผิดพลาดว่ารหัสโปรเจ็กต์ที่ระบุสำหรับโปรเจ็กต์ใหม่มีอยู่แล้ว
สาเหตุที่เป็นไปได้ที่รหัสโปรเจ็กต์อาจมีอยู่แล้วมีดังนี้
โปรเจ็กต์ที่เชื่อมโยงกับรหัสนั้นเป็นของบุคคลอื่น
- วิธีแก้ไข: เลือกรหัสโปรเจ็กต์อื่น
โปรเจ็กต์ที่เชื่อมโยงกับรหัสดังกล่าวเพิ่งถูกลบ (อยู่ในสถานะการลบแบบไม่ลบออกทั้งหมด)
- วิธีแก้ไข: หากคุณคิดว่าโปรเจ็กต์ที่เชื่อมโยงกับรหัสเป็นโปรเจ็กต์ของคุณ ให้ตรวจสอบสถานะของโปรเจ็กต์โดยใช้
projects.get
REST API
- วิธีแก้ไข: หากคุณคิดว่าโปรเจ็กต์ที่เชื่อมโยงกับรหัสเป็นโปรเจ็กต์ของคุณ ให้ตรวจสอบสถานะของโปรเจ็กต์โดยใช้
โปรเจ็กต์ที่เชื่อมโยงกับรหัสนั้นอยู่ภายใต้ผู้ใช้ปัจจุบันอย่างถูกต้อง สาเหตุที่เป็นไปได้ของข้อผิดพลาดนี้อาจเป็นเพราะ
terraform apply
ก่อนหน้าถูกขัดจังหวะ- วิธีแก้ไข: เรียกใช้คําสั่งต่อไปนี้
terraform import google_project.default PROJECT_ID
แล้ว
terraform import google_firebase_project.default PROJECT_ID
- วิธีแก้ไข: เรียกใช้คําสั่งต่อไปนี้
เหตุใดคุณจึงต้องจัดสรรอินสแตนซ์ Cloud Firestore เริ่มต้นก่อนถึงจะจัดสรรที่เก็บข้อมูล Cloud Storage เริ่มต้นได้
หากคุณจัดสรรที่เก็บข้อมูล Cloud Storage เริ่มต้น (ผ่าน google_app_engine_application
) ก่อนที่พยายามจัดสรรอินสแตนซ์ Cloud Firestore เริ่มต้น คุณจะเห็นว่ามีการจัดสรรอินสแตนซ์ Cloud Firestore เริ่มต้นแล้ว โปรดทราบว่าอินสแตนซ์ฐานข้อมูลที่จัดสรรอยู่ในโหมด Datastore ซึ่งหมายความว่า Firebase SDK, การตรวจสอบสิทธิ์ หรือ Firebase Security Rules ไม่สามารถเข้าถึงได้ หากต้องการใช้ Cloud Firestore กับบริการ Firebase เหล่านี้ คุณจะต้องล้างข้อมูลในฐานข้อมูล แล้วเปลี่ยนประเภทฐานข้อมูลในคอนโซล Google Cloud
เมื่อพยายามจัดสรร Cloud Storage (ผ่าน google_app_engine_application
) แล้วจัดสรรอินสแตนซ์ Cloud Firestore เริ่มต้น คุณจะได้รับข้อผิดพลาด Error: Error creating Database: googleapi: Error 409: Database already
exists. Please use another database_id
เมื่อคุณจัดสรรที่เก็บข้อมูล Cloud Storage เริ่มต้นของโปรเจ็กต์ (ผ่าน google_app_engine_application
) และโปรเจ็กต์ยังไม่มีอินสแตนซ์ Cloud Firestore เริ่มต้น google_app_engine_application
จะจัดสรรอินสแตนซ์ Cloud Firestore เริ่มต้นของโปรเจ็กต์โดยอัตโนมัติ
ดังนั้น เนื่องจากอินสแตนซ์ Cloud Firestore เริ่มต้นของโปรเจ็กต์ได้รับการจัดสรรแล้ว google_firestore_database
จึงจะแสดงข้อผิดพลาดหากคุณพยายามจัดสรรอินสแตนซ์เริ่มต้นนั้นอีกครั้งอย่างชัดเจน
เมื่อจัดสรรอินสแตนซ์ Cloud Firestore เริ่มต้นของโปรเจ็กต์แล้ว คุณจะ "จัดสรรใหม่" หรือเปลี่ยนตำแหน่งของอินสแตนซ์ไม่ได้ โปรดทราบว่าอินสแตนซ์ฐานข้อมูลที่จัดสรรอยู่ในโหมด Datastore ซึ่งหมายความว่า Firebase SDK, การรับรองความถูกต้อง หรือ Firebase Security Rules ไม่สามารถเข้าถึงได้ หากต้องการใช้ Cloud Firestore กับบริการ Firebase เหล่านี้ คุณจะต้องล้างข้อมูลฐานแล้วเปลี่ยนประเภทฐานข้อมูลในคอนโซล Google Cloud