This guide walks you through creating a Google Earth Engine service account for the SAR Flood Detection application.
- A Google account
- Access to Google Cloud Platform (GCP)
- Go to Google Cloud Console
- Click on the project dropdown at the top
- Click "New Project"
- Enter a project name (e.g., "sar-flood-detection")
- Click "Create"
- Wait for the project to be created and select it
- In the Cloud Console, go to "APIs & Services" > "Library"
- Search for and enable the following APIs:
- Earth Engine API
- Cloud Run API (for deployment)
- Secret Manager API (for credential storage)
- Go to Google Earth Engine signup
- Sign in with your Google account
- Select "Register a Cloud Project"
- Enter your GCP project ID (from Step 1)
- Accept the terms and submit
- Wait for approval (usually instant for Cloud Projects)
- In Cloud Console, go to "IAM & Admin" > "Service Accounts"
- Click "Create Service Account"
- Enter details:
- Name:
gee-sar-service-account - Description:
Service account for SAR flood detection app
- Name:
- Click "Create and Continue"
- In the "Grant this service account access to project" section:
- Add role: "Earth Engine Resource Admin"
- Add role: "Earth Engine Resource Writer" (if needed)
- Click "Continue"
- Click "Done" (skip optional user access)
- Find your newly created service account in the list
- Click on the service account email
- Go to the "Keys" tab
- Click "Add Key" > "Create new key"
- Select "JSON" format
- Click "Create"
- The JSON key file will download automatically
-
Create a
credentialsdirectory in thebackendfolder:mkdir backend/credentials
-
Move the downloaded JSON file to this directory:
mv ~/Downloads/gee-sar-service-account-*.json backend/credentials/gee-service-account.json
-
Create a
.envfile in thebackenddirectory:cd backend cp .env.example .env -
Edit
.envand set:GEE_SERVICE_ACCOUNT_PATH=./credentials/gee-service-account.json -
Verify the credentials are excluded from git:
# This should show backend/credentials/ in .gitignore cat ../.gitignore | grep credentials
-
Activate your Python virtual environment:
cd backend source venv/bin/activate # On Windows: venv\Scripts\activate
-
Run a simple test:
python -c " import ee import json import os from dotenv import load_dotenv load_dotenv() cred_path = os.getenv('GEE_SERVICE_ACCOUNT_PATH') with open(cred_path) as f: data = json.load(f) credentials = ee.ServiceAccountCredentials( data['client_email'], cred_path ) ee.Initialize(credentials) # Test query image = ee.Image('COPERNICUS/S1_GRD').first() print('✅ GEE Authentication successful!') print(f'Service account: {data[\"client_email\"]}') "
If you see "✅ GEE Authentication successful!", you're all set!
- In Cloud Console, go to "Security" > "Secret Manager"
- Click "Create Secret"
- Name:
gee-service-account - For the secret value, paste the entire contents of your JSON key file
- Click "Create Secret"
- Grant the Cloud Run service account access:
- Go to the secret's "Permissions" tab
- Click "Grant Access"
- Add principal:
{project-number}-compute@developer.gserviceaccount.com - Role: "Secret Manager Secret Accessor"
- When deploying to Cloud Run, pass the JSON content as an environment variable:
gcloud run deploy sar-flood-api \ --set-env-vars GEE_SERVICE_ACCOUNT="$(cat backend/credentials/gee-service-account.json)"
Recommendation: Use Secret Manager (Option A) for production deployments.
The backend code should automatically detect the environment:
# backend/gee_processing.py
def initialize_gee():
"""Initialize Google Earth Engine with service account credentials"""
import os
import json
import ee
# Check if running locally or on Cloud Run
if os.path.exists(os.getenv('GEE_SERVICE_ACCOUNT_PATH', '')):
# Local development
cred_path = os.getenv('GEE_SERVICE_ACCOUNT_PATH')
with open(cred_path) as f:
data = json.load(f)
credentials = ee.ServiceAccountCredentials(
data['client_email'],
cred_path
)
ee.Initialize(credentials)
else:
# Cloud Run with Secret Manager
# The secret is mounted as an environment variable
cred_json = os.getenv('GEE_SERVICE_ACCOUNT')
if cred_json:
data = json.loads(cred_json)
credentials = ee.ServiceAccountCredentials(
data['client_email'],
key_data=cred_json
)
ee.Initialize(credentials)
else:
raise ValueError("GEE credentials not found")- Make sure you completed Step 3 (Register for GEE)
- Wait a few minutes after registration
- Verify your project ID matches in GEE and GCP
- Ensure the service account has "Earth Engine Resource Admin" role
- Check that the service account email ends with
.gserviceaccount.com - Verify the JSON key is not corrupted
- Check that the JSON file path is correct in
.env - Verify the JSON file is valid (can be opened and parsed)
- Ensure you're using the service account credentials, not OAuth credentials
- Verify Secret Manager permissions
- Check that the secret name matches in your deployment script
- Ensure the Cloud Run service account has access to the secret
-
Never commit credentials to git
- Always keep
credentials/in.gitignore - Use Secret Manager for production
- Always keep
-
Rotate keys periodically
- Create new keys every 90 days
- Delete old keys after rotation
-
Use least-privilege permissions
- Only grant necessary Earth Engine roles
- Avoid "Owner" or "Editor" roles
-
Monitor usage
- Check GEE quota usage regularly
- Set up billing alerts in GCP
- Google Earth Engine Authentication
- GCP Service Accounts
- Secret Manager Documentation
- Earth Engine Python API
Need Help? Open an issue on GitHub or consult the GEE Community Forum.