'Conditionally create a task definition in ECS

I have a boto3 script, which updates(create new revision) of ECS task definition and update the ECS service with the new task definition.

import boto3
import json

client = boto3.client("ecs", region_name="us-east-1")

task_def_resp = client.register_task_definition(
    containerDefinitions=[<some_container_definitions>],
    executionRoleArn="arn:aws:iam::xxxxxxxxxxx:role/ecsTaskExecutionRole",
    family= "AWSSampleApp2",
    networkMode="awsvpc",
    requiresCompatibilities= ["FARGATE"],
    cpu= "256",
    memory= "512")
...
...
update_svc_resp = client.create_service(
    cluster="xxxxxxxx", 
    serviceName="xxxxxxxxxxxxxx",
    taskDefinition=task_def_resp.get('taskDefinition').get('taskDefinitionArn'),
    desiredCount=1,
    networkConfiguration={
        'awsvpcConfiguration': {
            'subnets': ['xxxxxxxxxxx'],
            'securityGroups': ["xxxxxxxxxxxxx"]
        }
    },
    launchType='FARGATE',
)
...
...

The code works perfectly fine. But, even if there is no change in task definition, A new revision(version) is always created whenever I call this script.

My thought here is, Is it possible to compare the task definition(find if the task defintion contains any changes) and create the new revision only if it contains some changes?

Any suggestions are appreciated.

Thanks.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source