Firebase מתחיל לתמוך ב- Terraform . אם אתה בצוות שרוצה לבצע אוטומציה וסטנדרטיזציה של יצירת פרויקטים של Firebase עם משאבים ספציפיים שסופקו ושירותים מופעלים, אז השימוש ב-Terraform עם Firebase יכול להתאים לך.
זרימת העבודה הבסיסית לשימוש ב- Terraform עם Firebase כוללת את הדברים הבאים:
יצירה והתאמה אישית של קובץ תצורה של Terraform (קובץ
.tf
) המציין את התשתית שברצונך לספק (כלומר, משאבים שברצונך לספק והשירותים שברצונך להפעיל).שימוש בפקודות gCloud CLI המתממשקות עם Terraform כדי לספק את התשתית המצוינת בקובץ
.tf
.
מה אתה יכול לעשות עם Terraform ו-Firebase?
זרימת העבודה המוכללת לדוגמה במדריך זה היא יצירת פרויקט Firebase חדש עם אפליקציית אנדרואיד. אבל אתה יכול לעשות הרבה יותר עם Terraform, כגון:
מחק ושנה תשתית קיימת באמצעות Terraform.
נהל תצורה ומשימות ספציפיות למוצר באמצעות Terraform, כמו:
- הפעלת ספקי כניסה של Firebase Authentication.
- יצירת דליים של Cloud Storage או מופעי מסד נתונים ופריסה של כללי אבטחה של Firebase עבורם.
אתה יכול להשתמש בקבצי התצורה והפקודות הסטנדרטיים של Terraform כדי לבצע את כל המשימות הללו. וכדי לעזור לך בכך, סיפקנו קובצי תצורה לדוגמה של Terraform עבור מספר מקרי שימוש נפוצים.
זרימת עבודה כללית לשימוש ב- Terraform עם Firebase
דרישות מוקדמות
מדריך זה הוא מבוא לשימוש ב- Terraform עם Firebase, ולכן הוא מניח מיומנות בסיסית עם Terraform. ודא שהשלמת את התנאים המוקדמים הבאים לפני התחלת זרימת עבודה זו.
התקן את Terraform והכר את Terraform באמצעות המדריכים הרשמיים שלהם.
התקן את Google Cloud CLI (gCloud CLI). התחבר באמצעות חשבון משתמש או חשבון שירות .
- אם אתה משתמש בחשבון משתמש, עליך לקבל את התנאים וההגבלות של Firebase (ToS Firebase). קיבלת את ה-Firebase ToS אם אתה יכול להציג פרויקט Firebase במסוף Firebase
- כדי ש-Terraform תבצע פעולות מסוימות (לדוגמה, יצירת פרויקטים), הדברים הבאים חייבים להיות נכונים:
- למשתמש או לחשבון השירות חייבת להיות גישת IAM המתאימה לפעולות אלו.
- אם המשתמש או חשבון השירות הם חלק מארגון Google Cloud, מדיניות הארגון חייבת לאפשר לחשבון לבצע פעולות אלו.
שלב 1: צור והתאם אישית קובץ תצורה של Terraform
קובץ תצורה של Terraform צריך שני חלקים עיקריים (אשר מתוארים בפירוט להלן):
- קטע הגדרת
provider
המכתיב לאילו משאבי Terraform ניתן לגשת - קטע של בלוקים של
resource
בודדים שמציינים איזו תשתית ליצור
הגדר את provider
שלך
נדרשת הגדרת provider
לא משנה באילו מוצרים או שירותים של Firebase מדובר.
צור קובץ תצורה של Terraform (כמו קובץ
main.tf
) בספרייה המקומית שלך.במדריך זה, תשתמש בקובץ התצורה הזה כדי לציין גם את הגדרת
provider
וגם את כל התשתית שברצונך ש- Terraform תיצור. עם זאת, שים לב שיש לך אפשרויות כיצד לכלול את הגדרת הספק.יש לך את האפשרויות הבאות כיצד לכלול הגדרת
provider
לשאר תצורת Terraform שלך:אפשרות 1: כלול אותו בחלק העליון של קובץ תצורה יחיד של Terraform
.tf
(כפי שמוצג במדריך זה).- השתמש באפשרות זו אם אתה רק התחלת עם 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 = "~> 4.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 repo .
ניהול פרויקטים ואפליקציות של Firebase
google_firebase_project
— אפשר שירותי Firebase בפרויקט Google Cloud קייםgoogle_firebase_project_location
- הגדר את המיקום עבור משאבי ברירת המחדל של Google Cloud של הפרויקטאפליקציות Firebase
-
google_firebase_apple_app
- צור או נהל אפליקציית Firebase Apple platforms -
google_firebase_android_app
— צור או נהל אפליקציית Firebase Android -
google_firebase_web_app
— צור או נהל יישום אינטרנט של Firebase
-
אימות Firebase
google_identity_platform_config
- הפעל את Google Cloud Identity Platform (GCIP) (שהוא הקצה האחורי של אימות Firebase) וספק הגדרות אימות ברמת הפרויקטהגדרת אימות Firebase באמצעות Terraform מחייבת הפעלת GCIP. הקפד לעיין בקובץ
.tf
לדוגמה כדי להגדיר את אימות Firebase .הפרויקט שבו Terraform יאפשר GCIP ו/או Firebase Authentication חייב להיות בתוכנית התמחור של Blaze (כלומר, לפרויקט חייב להיות חשבון Cloud Billing משויך). אתה יכול לעשות זאת באופן פרוגרמטי על ידי הגדרת התכונה
billing_account
במשאבgoogle_project
.משאב זה גם מאפשר יותר תצורות, כמו שיטות כניסה מקומיות, כגון אימות אנונימי, דוא"ל/סיסמה וטלפון, כמו גם חסימת פונקציות ודומיינים מורשים.
google_identity_platform_default_supported_idp_config
- הגדר ספקי זהות מאוחדים נפוצים, כמו גוגל, פייסבוק או אפלidentity_platform_oauth_idp_config
- הגדר מקורות שרירותיים של OAuth Identity Provider (IdP)google_identity_platform_inbound_saml_config
— הגדר שילובי SAML
עדיין לא נתמך:
- הגדרת אימות רב-גורמי (MFA) באמצעות Terraform
מסד נתונים בזמן אמת של Firebase
-
google_firebase_database_instance
- צור מופע של מסד נתונים בזמן אמת
עדיין לא נתמך:
- פריסת כללי אבטחת מסד נתונים של Firebase בזמן אמת באמצעות Terraform (למד כיצד לפרוס כללים אלה באמצעות כלים אחרים, כולל אפשרויות פרוגרמטיות)
Cloud Firestore
google_firestore_database
- צור מופע Cloud Firestoregoogle_firestore_index
— אפשר שאילתות יעילות עבור Cloud Firestoregoogle_firestore_document
- ראה מופע של Cloud Firestore עם מסמך ספציפי באוסףחשוב: אל תשתמש בנתוני משתמש קצה או ייצור אמיתיים במסמך המקור הזה.
אחסון בענן עבור Firebase
google_firebase_storage_bucket
- הפוך דלי Cloud Storage קיים לנגיש עבור ערכות SDK של Firebase, אימות וכללי אבטחה של Firebase- הגדרת דלי ברירת מחדל של Cloud Storage עבור פרויקט Firebase דורשת הקצאה ראשונה
google_app_engine_application
. הקפד לסקור את קובץ.tf
לדוגמה עבור אופן הקצאת דליים של Cloud Storage .
- הגדרת דלי ברירת מחדל של Cloud Storage עבור פרויקט Firebase דורשת הקצאה ראשונה
google_storage_bucket_object
- הוסף אובייקט לדלי של Cloud Storageחשוב: אל תשתמש בנתוני משתמש קצה או ייצור אמיתיים בקובץ זה.
כללי אבטחה של Firebase (עבור Cloud Firestore ו-Cloud Storage)
שים לב ש-Firebase Realtime Database משתמש במערכת הקצאה אחרת עבור כללי האבטחה של Firebase.
google_firebaserules_ruleset
- הגדר כללי אבטחה של Firebase החלים על מופע Cloud Firestore או דלי Cloud Storagegoogle_firebaserules_release
- פרוס ערכות כללים ספציפיות למופע של Cloud Firestore או לדלי של Cloud Storage
בדיקת אפליקציית Firebase
-
google_firebase_app_check_service_config
— אפשר אכיפת בדיקת אפליקציות עבור שירות -
google_firebase_app_check_app_attest_config
- רשום אפליקציית פלטפורמות של אפל אצל ספק ה-App Attest -
google_firebase_app_check_device_check_config
— רשום אפליקציית פלטפורמות של אפל אצל ספק DeviceCheck -
google_firebase_app_check_play_integrity_config
— רשום אפליקציית אנדרואיד אצל ספק Play Integrity -
google_firebase_app_check_recaptcha_enterprise_config
- רשום אפליקציית אינטרנט אצל ספק reCAPTCHA Enterprise -
google_firebase_app_check_recaptcha_v3_config
— רשום אפליקציית אינטרנט אצל ספק reCAPTCHA v3 -
google_firebase_app_check_debug_token
- השתמש באסימוני ניפוי באגים לבדיקה
הרחבות Firebase
-
google_firebase_extensions_instance
- התקן או עדכן מופע של תוסף Firebase
דגימת קובצי תצורה של Terraform למקרי שימוש נפוצים
תצורה זו יוצרת פרויקט Google Cloud חדש, משייכת את הפרויקט לחשבון Cloud Billing (תוכנית התמחור של Blaze נדרשת עבור אימות Firebase עם GCIP), מאפשרת שירותי Firebase עבור הפרויקט, מגדירה את Firebase Authentication עם GCIP ורושם שלוש אפליקציות שונות סוגים עם הפרויקט.
שים לב שהפעלת GCIP נדרשת כדי להגדיר אימות Firebase באמצעות 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, ] }
תצורה זו יוצרת פרויקט Google Cloud חדש, מאפשרת שירותי Firebase עבור הפרויקט, מספקת את מופע ברירת המחדל של Realtime Database של הפרויקט ורושם שלושה סוגי אפליקציות שונים בפרויקט.
# 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/projects/locations#rtdb-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, ] }
תצורה זו יוצרת פרויקט Google Cloud חדש, משייכת את הפרויקט לחשבון Cloud Billing (תוכנית התמחור של Blaze נדרשת למספר מופעי Realtime Database), מאפשרת שירותי Firebase עבור הפרויקט, מספקת מופעים מרובים של Realtime Database (כולל ברירת המחדל של מסד הנתונים בזמן אמת של הפרויקט מופע), ורושם שלושה סוגי אפליקציות שונים עם הפרויקט.
# 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/projects/locations#rtdb-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, ] }
תצורה זו יוצרת פרויקט Google Cloud חדש, מאפשרת שירותי Firebase עבור הפרויקט, מספקת את מופע Cloud Firestore של הפרויקט ורושם שלושה סוגי אפליקציות שונים בפרויקט.
הוא גם מספק כללי אבטחה של Firebase עבור מופע 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/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.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 שאמורים להיות בקובץ מקומי בשם firestore.rules
.
rules_version = '2'; service cloud.firestore { match /databases/{database}/documents { allow read: if request.auth != null; allow create: if request.auth != null; allow update: if request.auth != null; } }
תצורה זו יוצרת פרויקט Google Cloud חדש, מאפשרת שירותי Firebase עבור הפרויקט, מספקת את דלי ברירת המחדל של Cloud Storage של הפרויקט ורושם שלושה סוגי אפליקציות שונים בפרויקט.
הוא גם מספק כללי אבטחה של Firebase עבור דלי Cloud Storage, ומעלה קובץ לדלי.
# Creates a new Google Cloud project. resource "google_project" "storage" { 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" "storage" { provider = google-beta.no_user_project_override project = google_project.storage.project_id for_each = toset([ "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" { provider = google-beta project = google_project.storage.project_id } # Provisions the default Cloud Storage bucket for the project via Google App Engine. resource "google_app_engine_application" "default" { provider = google-beta project = google_project.storage.project_id # See available locations: https://firebase.google.com/docs/projects/locations#default-cloud-location # This will set the location for the default Storage bucket and the App Engine App. location_id = "name-of-region-for-default-bucket" # If you use Firestore, uncomment this to make sure Firestore is provisioned first. # depends_on = [ # google_firestore_database.firestore # ] } # Makes the default Storage bucket accessible for Firebase SDKs, authentication, and Firebase Security Rules. resource "google_firebase_storage_bucket" "default-bucket" { provider = google-beta project = google_project.storage.project_id bucket_id = google_app_engine_application.default.default_bucket } # Creates a ruleset of Cloud Storage Security Rules from a local file. resource "google_firebaserules_ruleset" "storage" { provider = google-beta project = google_project.storage.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 default Storage bucket to be provisioned before creating this ruleset. depends_on = [ google_firebase_project.storage, ] } # Releases the ruleset to the default Storage bucket. resource "google_firebaserules_release" "default-bucket" { provider = google-beta name = "firebase.storage/${google_app_engine_application.default.default_bucket}" ruleset_name = "projects/${google_project.storage.project_id}/rulesets/${google_firebaserules_ruleset.storage.name}" project = google_project.storage.project_id } # Uploads a new file to the default Storage bucket. # Don't use real end-user or production data in this file. resource "google_storage_bucket_object" "cat-picture" { provider = google-beta name = "cat.png" source = "path/to/cat.png" bucket = google_app_engine_application.default.default_bucket } # Creates a Firebase Android App in the new project created above. resource "google_firebase_android_app" "storage" { provider = google-beta project = google_project.storage.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, ] } # Creates a Firebase Apple-platforms App in the new project created above. resource "google_firebase_apple_app" "storage" { provider = google-beta project = google_project.storage.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, ] } # Creates a Firebase Web App in the new project created above. resource "google_firebase_web_app" "storage" { provider = google-beta project = google_project.storage.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.storage, ] }
זוהי מערכת הכללים של כללי האבטחה של Cloud Storage שאמורים להיות בקובץ מקומי בשם storage.rules
.
rules_version = '2'; service firebase.storage { match /b/{bucket}/o { match /{allPaths=**} { allow read, write: if request.auth != null; } } }
תצורה זו יוצרת פרויקט Google Cloud חדש, משייכת את הפרויקט לחשבון Cloud Billing (תוכנית התמחור של Blaze נדרשת עבור דליים מרובים), מאפשרת שירותי Firebase עבור הפרויקט, מספקת דליים מרובים של Cloud Storage (כולל דלי ברירת המחדל של ה-Cloud Storage של הפרויקט) , ורושם שלושה סוגי אפליקציות שונים בפרויקט.
הוא גם מספק כללי אבטחה של Firebase עבור דליים של 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 the default Cloud Storage bucket for the project via Google App Engine. resource "google_app_engine_application" "default-multi" { provider = google-beta project = google_project.storage-multi.project_id # See available locations: https://firebase.google.com/docs/projects/locations#default-cloud-location # This will set the location for the default Storage bucket and the App Engine App. location_id = "name-of-region-for-default-bucket" # If you use Firestore, uncomment this to make sure Firestore is provisioned first. # depends_on = [ # google_firestore_database.firestore # ] } # Provisions an additional Cloud Storage bucket. # Additional Cloud Storage buckets are not provisioned via App Engine. resource "google_storage_bucket" "bucket-multi" { 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 default Storage bucket. location = "name-of-region-for-additional-bucket" } # Makes the default Storage bucket accessible for Firebase SDKs, authentication, and Firebase Security Rules. resource "google_firebase_storage_bucket" "default-bucket-multi" { provider = google-beta project = google_project.storage-multi.project_id bucket_id = google_app_engine_application.default-multi.default_bucket } # Makes the additional Storage bucket accessible for Firebase SDKs, authentication, and Firebase Security Rules. resource "google_firebase_storage_bucket" "bucket-multi" { provider = google-beta project = google_project.storage-multi.project_id bucket_id = google_storage_bucket.bucket-multi.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 default Storage bucket. resource "google_firebaserules_release" "default-bucket-multi" { provider = google-beta name = "firebase.storage/${google_app_engine_application.default-multi.default_bucket}" 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-multi" { provider = google-beta name = "firebase.storage/${google_storage_bucket.bucket-multi.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 default 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_app_engine_application.default-multi.default_bucket } # 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" # 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.storage-multi, ] }
זוהי מערכת הכללים של כללי האבטחה של Cloud Storage שאמורים להיות בקובץ מקומי בשם storage.rules
.
rules_version = '2'; service firebase.storage { match /b/{bucket}/o { match /{allPaths=**} { allow read, write: if request.auth != null; } } }
תצורה זו יוצרת פרויקט Google Cloud חדש, מאפשרת שירותי Firebase עבור הפרויקט, מספקת את המופע של Cloud Firestore ולאחר מכן מספקת את דלי ברירת המחדל של Cloud Storage.
זה גם מספק כללי אבטחה של Firebase עבור מופע Cloud Firestore ודלי ברירת המחדל של Cloud Storage.
# Creates a new Google Cloud project. resource "google_project" "fs" { # fs = Firestore + Storage 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" "fs" { provider = google-beta.no_user_project_override project = google_project.fs.project_id for_each = toset([ "serviceusage.googleapis.com", "cloudresourcemanager.googleapis.com", "firebaserules.googleapis.com", "firebasestorage.googleapis.com", "storage.googleapis.com", "firestore.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" "fs" { provider = google-beta project = google_project.fs.project_id } #### Set up Firestore before default Cloud Storage bucket #### # Provisions the Firestore database instance. resource "google_firestore_database" "firestore-fs" { provider = google-beta project = google_project.fs.project_id 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.fs, ] } # Creates a ruleset of Firestore Security Rules from a local file. resource "google_firebaserules_ruleset" "firestore-fs" { provider = google-beta project = google_project.fs.project_id source { files { # Write security rules in a local file named "firestore.rules". # Learn more: https://firebase.google.com/docs/firestore/security/get-started name = "firestore.rules" content = file("firestore.rules") } } # Wait for Firestore to be provisioned before creating this ruleset. depends_on = [ google_firestore_database.firestore-fs ] } # Releases the ruleset for the Firestore instance. resource "google_firebaserules_release" "firestore-fs" { provider = google-beta name = "cloud.firestore" # must be cloud.firestore ruleset_name = google_firebaserules_ruleset.firestore-fs.name project = google_project.fs.project_id # Wait for Firestore to be provisioned before releasing the ruleset. depends_on = [ google_firestore_database.firestore-fs, ] } #### Set up default Cloud Storage default bucket after Firestore #### # Provisions the default Cloud Storage bucket for the project via Google App Engine. resource "google_app_engine_application" "default-bucket-fs" { provider = google-beta project = google_project.fs.project_id # See available locations: https://firebase.google.com/docs/projects/locations#default-cloud-location # This will set the location for the default Storage bucket and the App Engine App. location_id = "name-of-region" # Must be in the same location as Firestore (above) # Wait for Firestore to be provisioned first. # Otherwise, the Firestore instance will be provisioned in Datastore mode (unusable by Firebase). depends_on = [ google_firestore_database.firestore-fs, ] } # Makes the default Storage bucket accessible for Firebase SDKs, authentication, and Firebase Security Rules. resource "google_firebase_storage_bucket" "default-bucket-fs" { provider = google-beta project = google_project.fs.project_id bucket_id = google_app_engine_application.default-bucket-fs.default_bucket } # Creates a ruleset of Cloud Storage Security Rules from a local file. resource "google_firebaserules_ruleset" "default-bucket-fs" { provider = google-beta project = google_project.fs.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 Cloud Storage bucket to be provisioned before creating this ruleset. depends_on = [ google_firebase_project.fs, ] } # Releases the ruleset to the default Storage bucket. resource "google_firebaserules_release" "default-bucket-fs" { provider = google-beta name = "firebase.storage/${google_app_engine_application.default-bucket-fs.default_bucket}" ruleset_name = "projects/${google_project.fs.project_id}/rulesets/${google_firebaserules_ruleset.default-bucket-fs.name}" project = google_project.fs.project_id }
זוהי מערכת הכללים של כללי האבטחה של Cloud Firestore שאמורים להיות בקובץ מקומי בשם firestore.rules
.
rules_version = '2'; service cloud.firestore { match /databases/{database}/documents { allow read: if request.auth != null; allow create: if request.auth != null; allow update: if request.auth != null; } }
זוהי מערכת הכללים של כללי האבטחה של Cloud Storage שאמורים להיות בקובץ מקומי בשם storage.rules
.
rules_version = '2'; service firebase.storage { match /b/{bucket}/o { match /{allPaths=**} { allow read, write: if request.auth != null; } } }
תצורה זו יוצרת פרויקט Google Cloud חדש, מאפשרת שירותי Firebase עבור הפרויקט, ומגדירה ומאפשרת אכיפה של Firebase App Check for 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] }
תצורה זו יוצרת פרויקט Google Cloud חדש, מאפשרת שירותי Firebase עבור הפרויקט, ומתקין מופע חדש של תוסף Firebase בפרויקט. אם המופע כבר קיים, הפרמטרים שלו מתעדכנים על סמך הערכים שסופקו בתצורה.
# 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" } } }
פתרון בעיות ושאלות נפוצות
מדריך זה משתמש בתכונות 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
.
ודא שחשבון המשתמש שבו אתה משתמש להפעלת פקודות gCloud CLI קיבל את התנאים וההגבלות של Firebase (ToS Firebase).
אתה יכול לבצע בדיקה זו על ידי שימוש בדפדפן המחובר לחשבון המשתמש וניסיון להציג פרויקט Firebase קיים במסוף Firebase . אם אתה יכול להציג פרויקט Firebase קיים, אז חשבון המשתמש קיבל את ה-Firebase ToS.
אם אינך יכול להציג אף פרויקט Firebase קיים, כנראה שחשבון המשתמש לא קיבל את ה-Firebase ToS. כדי לתקן זאת, צור פרויקט Firebase חדש דרך מסוף Firebase וקבל את ה-Firebase ToS כחלק מיצירת הפרויקט. אתה יכול למחוק מיד את הפרויקט הזה דרך הגדרות הפרויקט במסוף.
המתן מספר דקות ולאחר מכן נסה להפעיל שוב terraform apply
.
זה יכול להיות בגלל עיכוב התפשטות במערכות שונות. נסה לפתור בעיה זו על ידי ייבוא המשאב למצב Terraform על ידי הפעלת terraform import
. לאחר מכן נסה להפעיל terraform apply
שוב.
תוכל ללמוד כיצד לייבא כל משאב בקטע "ייבוא" בתיעוד ה-Terraform שלו (לדוגמה, תיעוד "ייבוא" עבור Cloud Firestore ).
כפי שהשגיאה מרמזת, ייתכן ש- Terraform מנסה לספק מספר מדדים ו/או ליצור מסמך בו-זמנית ונתקל בשגיאת במקביל. נסה להפעיל terraform apply
שוב.
שגיאה זו פירושה ש- Terraform לא יודע מול איזה פרויקט לבדוק מכסה. כדי לפתור בעיות, בדוק את הדברים הבאים בבלוק resource
:
- ודא שציינת ערך עבור תכונת
project
. - ודא שאתה משתמש בספק עם
user_project_override = true
(ללא כינוי), שבדוגמאות של Firebase הואgoogle-beta
.
להלן הסיבות האפשריות לכך שמזהה הפרויקט כבר קיים:
הפרויקט המשויך למזהה זה שייך למישהו אחר.
- כדי לפתור: בחר מזהה פרויקט אחר.
הפרויקט המשויך למזהה זה נמחק לאחרונה (במצב מחיקה רכה).
- כדי לפתור: אם אתה חושב שהפרויקט המשויך למזהה שייך לך, בדוק את מצב הפרויקט באמצעות ה-
projects.get
REST API .
- כדי לפתור: אם אתה חושב שהפרויקט המשויך למזהה שייך לך, בדוק את מצב הפרויקט באמצעות ה-
הפרויקט המשויך למזהה זה קיים כהלכה תחת המשתמש הנוכחי. סיבה אפשרית לשגיאה יכולה להיות
terraform apply
קודם הופסק.- כדי לפתור: הפעל את הפקודות הבאות:
terraform import google_project.default PROJECT_ID
ואז
terraform import google_firebase_project.default PROJECT_ID
- כדי לפתור: הפעל את הפקודות הבאות:
יישום App Engine דורש מופע של Cloud Firestore, אבל אתה יכול לקבל רק מופע אחד של Cloud Firestore לכל פרויקט. לכן, כפי שמציעה הודעת השגיאה, אם כבר הקבעת את מופע Cloud Firestore של הפרויקט במיקום אחד, App Engine ישיג שגיאה אם תנסה לספק מופע של Cloud Firestore במיקום אחר. App Engine חושב שאתה מנסה "לספק מחדש" את מופע Cloud Firestore שכבר קיים.
כדי לפתור שגיאה זו, השתמש באותו מיקום גם עבור Cloud Firestore וגם עבור אפליקציית App Engine. אם אתה צריך דלי של Cloud Storage במיקום שונה מ-Cloud Firestore, אתה יכול לספק דליים נוספים (ראה את התצורה לדוגמה ליצירת דליים מרובים של Cloud Storage ).
כאשר אתה מקצה את דלי ברירת המחדל של Cloud Storage של פרויקט (דרך google_app_engine_application
) ולפרויקט אין עדיין מופע של Cloud Firestore, אז google_app_engine_application
מספק אוטומטית את מופע Cloud Firestore של הפרויקט.
לכן, אם המופע של Cloud Firestore של הפרויקט שלך כבר מסודר, google_firestore_database
ישיג שגיאה אם תנסה להקצות במפורש מופע של Cloud Firestore.
לאחר הקצאת מופע Cloud Firestore של הפרויקט, לא תוכל "להקצות מחדש" אותו או לשנות את מיקומו. כדי למנוע מהשגיאה להתרחש, הסר את בלוק המשאבים google_firestore_database
מקובץ התצורה שלך. עם זאת, שים לב שאנו ממליצים להקצות את Cloud Firestore לפני דלי ברירת המחדל של Cloud Storage של הפרויקט (עיין בצד להלן מדוע).