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.

Easy Python 3.9+ Aug 9, 2026 Observability & SRE 13 views 0 copies

Python code

8 lines
Python 3.9+
def 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

stdout
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

  1. Use `service.upper()` to produce uppercase tags like `API-GATEWAY-PRODUCTION`
  2. 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

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Observability & SRE

Related tutorials and quizzes for this topic.