Created
July 20, 2023 10:42
-
-
Save ulidtko/2199e485f5d405b1fd67e4573ddaaa40 to your computer and use it in GitHub Desktop.
Fetch env-file from AWS ECS Service TD (Task Definition)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import argparse | |
import datetime | |
import sys | |
import os.path | |
import boto3 | |
import jinja2 | |
MAIN_TEMPLATE = """ | |
# Env-file for AWS ECS service {{ service }} in cluster {{ cluster }} | |
# describing Task Definition {{ td.taskDefinitionArn }} | |
# | |
# Generated by {{ myname }} on {{ timestamp }} -- DO NOT EDIT | |
{% for container in td.containerDefinitions %} | |
#-- Container {{ container.name }} | |
#-- from image {{ container.image }} | |
{% for envitem in container.environment|sort(attribute="name") %} | |
{{ envitem.name }}={{ envitem.value }} | |
{% endfor %} | |
{% endfor %} | |
""".strip() | |
def main(opts): | |
ecs = boto3.client("ecs") | |
svc = ecs.describe_services(cluster=opts.cluster, services=[opts.service]) | |
svc = svc["services"] | |
if not svc: | |
return "Empty response, check service name" | |
assert len(svc) == 1 | |
[svc] = svc | |
td = ecs.describe_task_definition(taskDefinition=svc["taskDefinition"]) | |
td = td["taskDefinition"] | |
tmpl = jinja2.Environment(trim_blocks=True).from_string(MAIN_TEMPLATE) | |
print( | |
tmpl.render( | |
myname=os.path.basename(__file__), | |
timestamp=datetime.datetime.now().isoformat(), | |
cluster=opts.cluster, | |
service=opts.service, | |
td=td, | |
) | |
) | |
if __name__ == "__main__": | |
argP = argparse.ArgumentParser( | |
description="Fetch ECS service environment as an env-file" | |
) | |
argP.add_argument("cluster", help="which ECS cluster") | |
argP.add_argument("service", help="which service in the cluster") | |
opts = argP.parse_args() | |
sys.exit(main(opts)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment