How to Create a Deployment Environment Tag in Python
Generate a standardized deployment tag string by combining service and environment names with an f-string.
Python code
8 linesdef mock_env_tag(service, environment):
return f"{service}-{environment}"
if __name__ == "__main__":
service = "api-gateway"
environment = "production"
tag = mock_env_tag(service, environment)
print(f"Deployment tag: {tag}")
Output
Deployment tag: api-gateway-production
How it works
This function uses an f-string to interpolate the service and environment arguments directly into a single string, creating a predictable naming convention. The separator - keeps the tag compact and readable. Because the logic is isolated in a function, you can unit-test it easily and reuse it across environments. The if __name__ == "__main__" guard ensures the demo only runs when executed directly, not when imported as a module.
Common mistakes
- Forgetting the hyphen separator, which breaks downstream parsing of the tag
- Hardcoding the tag instead of using a function, making it un-testable
- Using `+` string concatenation instead of an f-string, reducing readability
Variations
- Use `service.upper()` to produce uppercase tags like `API-GATEWAY-PRODUCTION`
- Read environments from an env var with `os.getenv("ENV", "development")` for dynamic generation
Real-world use cases
- Tagging AWS or Kubernetes resources for cost allocation and filtering in dashboards.
- Generating metric names or log prefixes that route observability data per environment.
- Building container image names like `api-gateway-production:v1` in CI/CD pipelines.
Sponsored
More from Observability & SRE
- Adding a Correlation ID to Log Context in Python medium
- Calculate Error Rate from Log Stream in Python easy
- Check if a Timestamp Falls in a Daily Maintenance Window in Python easy
- Export Metrics with OTLP Mock in Python medium
- Generate Mock CPU and Memory Metrics in Python easy
- Generate Prometheus Text Exposition Format in Python easy
Keep learning
Related tutorials and quizzes for this topic.