import os from jinja2 import Template from jinja2.ext import i18n from yaml import safe_load import boto3 import logging import json logging.basicConfig(level=logging.INFO) logging.getLogger("botocore").setLevel(logging.CRITICAL) def _get_abs_path(path): return os.path.join(os.path.dirname(os.path.realpath(__file__)), path) def list_for_yml(iterable): return "\n".join(f"- {it}" for it in iterable) def render_template(template): with open(_get_abs_path("service.yml")) as f: service_config = safe_load(f) return Template(template).render({**service_config, **dict(os.environ), 'list': list_for_yml}) def create_default_tags(): tags_template = """[ {"Key": "Owner","Value": "{{ owner }}"}, {"Key": "Service", "Value": "{{ serviceName }}"}, {"Key": "Environment", "Value": "{{ ENVIRONMENT }}"} ]""" tags = json.loads(render_template(tags_template)) return tags def get_aws_account_id(): client = boto3.client("sts") return client.get_caller_identity()["Account"] def check_environment_variables(): for env_var in ["AWS_REGION", "ENVIRONMENT"]: try: env_var_value = os.environ[env_var] except KeyError: raise KeyError( f"Environment variable {env_var} not found. Please set it up before starting." )