Skip to content

Instantly share code, notes, and snippets.

@endofcake
Created May 22, 2018 21:22
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save endofcake/4ea2ac5c030a37965b65c7591c83a047 to your computer and use it in GitHub Desktop.
Save endofcake/4ea2ac5c030a37965b65c7591c83a047 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))
@rilutham
Copy link

rilutham commented Sep 2, 2020

I am using terraform cloud to execute terraform apply, how to install boto3 in terraform cloud @endofcake ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment