What if your app's secrets could stay safe and update themselves without you lifting a finger?
Why Environment variables and secrets in GCP? - Purpose & Use Cases
Imagine you have to manually type sensitive information like passwords or API keys into your app every time you deploy it. You write them in your code or config files, then share those files with your team.
This feels risky and messy, especially when you want to keep secrets safe and change them often.
Manually handling secrets is slow and risky. You might accidentally share passwords in public code or forget to update them everywhere.
It's easy to make mistakes that expose your data or break your app when secrets change.
Using environment variables and secret managers lets you keep sensitive info separate from your code.
You store secrets securely and your app reads them automatically when it runs. This keeps secrets safe and makes updates easy.
const apiKey = "12345-secret"; // hardcoded in code
const apiKey = process.env.API_KEY; // loaded from environmentYou can safely manage and update secrets without touching your code, making your apps more secure and easier to maintain.
A developer uses Google Cloud Secret Manager to store database passwords. The app fetches these secrets at runtime, so passwords never appear in code or logs.
Manual secret handling risks exposure and errors.
Environment variables separate config from code.
Secret managers keep sensitive data safe and easy to update.
Practice
Solution
Step 1: Understand environment variables
Environment variables hold configuration values like API URLs or feature flags that apps read when running.Step 2: Eliminate distractor options
Permanent data storage, network encryption, and VM creation are unrelated to environment variables.Final Answer:
To store configuration values that the app can use at runtime -> Option DQuick Check:
Environment variables = config values [OK]
- Confusing environment variables with data storage
- Thinking environment variables encrypt data
- Mixing environment variables with cloud resource creation
API_KEY with value 12345 in a Google Cloud Run service YAML?Solution
Step 1: Recall Cloud Run YAML env syntax
Cloud Run uses theenvlist withnameandvaluekeys to set environment variables.Step 2: Check options for correct YAML format
env: - name: API_KEY value: '12345'matches the correct syntax. Other options use incorrect keys or formats not supported by Cloud Run.Final Answer:
env: - name: API_KEY value: '12345' -> Option AQuick Check:
Cloud Run env uses name and value keys [OK]
- Using incorrect keys like environment_variables or variables
- Assigning values with '=' instead of colon
- Not using a list for env variables
import os
api_key = os.getenv('API_KEY')
print(f"Key: {api_key}")If the environment variable
API_KEY is not set, what will be printed?Solution
Step 1: Understand os.getenv behavior
When the environment variable is missing,os.getenvreturnsNoneby default.Step 2: Analyze the print output
The f-string convertsNoneto the string 'None', so the output is 'Key: None'.Final Answer:
Key: None -> Option CQuick Check:
Missing env var returns None [OK]
- Assuming empty string is returned
- Expecting an error to be raised
- Thinking variable name is printed
Solution
Step 1: Check permissions for Secret Manager access
Cloud Run uses a service account that must have Secret Manager access roles to read secrets.Step 2: Evaluate other options
While empty secret values, misspelled variable names, or secrets in different projects can cause issues, the most common cause is missing permissions blocking secret access.Final Answer:
The Cloud Run service account lacks permission to access the secret -> Option AQuick Check:
Missing permissions block secret access [OK]
- Ignoring IAM roles for service accounts
- Assuming secret value is always the problem
- Not verifying project alignment for secrets
Solution
Step 1: Identify secure secret storage
Google Secret Manager is designed to store sensitive info like passwords securely.Step 2: Use environment variables for app access
Mounting secrets as environment variables in Cloud Run keeps secrets out of code and config files.Step 3: Evaluate other options
Hardcoding, public storage, or sending passwords in URLs expose secrets and are insecure.Final Answer:
Store the password in Secret Manager and mount it as an environment variable in Cloud Run -> Option BQuick Check:
Secret Manager + env vars = secure best practice [OK]
- Hardcoding secrets in config files
- Using public storage for sensitive data
- Exposing secrets in URLs or logs
Ready to prove it?
Pass the test and this pattern moves unknown
