Skip to content

Instantly share code, notes, and snippets.

@include
Forked from endofcake/get_image_tags.py
Created November 21, 2018 23:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save include/10bb65b9a706cd4ff97c086d24c1baad to your computer and use it in GitHub Desktop.
Save include/10bb65b9a706cd4ff97c086d24c1baad to your computer and use it in GitHub Desktop.
An external data source for Terraform that returns the versions of all images in an ECS task definition
''' Return image tags specified in ECS task definition '''
import json
import sys
import boto3
ECR_CLIENT = boto3.client('ecr')
ECS_CLIENT = boto3.client('ecs')
def main(cluster_name, service_name):
'''The main routine'''
image_versions = {}
service = ECS_CLIENT.describe_services(
cluster=cluster_name,
services=[
service_name
]
)
if not service['services']:
# service doesn't exist yet, return empty dict
return image_versions
task_definition_arn = service['services'][0]['taskDefinition']
task_definition = ECS_CLIENT.describe_task_definition(
taskDefinition=task_definition_arn
)
for container in task_definition['taskDefinition']['containerDefinitions']:
full_name = container['image'].split(':')
# only add to response if image contains tag
if len(full_name) > 1:
image_versions[container['name']] = full_name[1]
return image_versions
if __name__ == "__main__":
args = json.load(sys.stdin)
cluster_name = args['cluster_name']
service_name = args['service_name']
image_versions = main(cluster_name, service_name)
print(json.dumps(image_versions))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment