aws_helpers.py
def create_ecs_service(cluster,
                       service,
                       task_definition_family,
                       target_group_arn,
                       service_count=1,
                       launch_type='EC2'):
    """Create ecs service for serving whatever you need."""
    ecs_client = boto3.client('ecs')

    if service in ecs_client.list_task_definition_families()["families"]:
        task_def = ecs_client.list_task_definitions(
            familyPrefix=task_definition_family,
            sort='DESC')["taskDefinitionArns"][0]

    response = ecs_client.create_service(
        cluster=cluster,
        serviceName=service,
        taskDefinition=task_def,
        loadBalancers=[
            {
                'targetGroupArn': target_group_arn,
                'containerName': service,
                'containerPort': 8080
            },
        ],
        desiredCount=1,
        launchType=launch_type,
        deploymentConfiguration={
            'maximumPercent': 200,
            'minimumHealthyPercent': 50
        },
        placementStrategy=[
            {
                'type': 'spread',
                'field': 'attribute:ecs.availability-zone'
            },
        ],
        healthCheckGracePeriodSeconds=15
    )
    return response
Create ecs service for serving whatever you need.
Create ecs service for serving whatever you need.
Create ecs service for serving whatever you need .
def create_ecs_service(cluster,
                       service,
                       task_definition_family,
                       target_group_arn,
                       service_count=1,
                       launch_type='EC2'):
    ecs_client = boto3.client('ecs')
    if service in ecs_client.list_task_definition_families()["families"]:
        task_def = ecs_client.list_task_definitions(
            familyPrefix=task_definition_family,
            sort='DESC')["taskDefinitionArns"][0]
    response = ecs_client.create_service(
        cluster=cluster,
        serviceName=service,
        taskDefinition=task_def,
        loadBalancers=[
            {
                'targetGroupArn': target_group_arn,
                'containerName': service,
                'containerPort': 8080
            },
        ],
        desiredCount=1,
        launchType=launch_type,
        deploymentConfiguration={
            'maximumPercent': 200,
            'minimumHealthyPercent': 50
        },
        placementStrategy=[
            {
                'type': 'spread',
                'field': 'attribute:ecs.availability-zone'
            },
        ],
        healthCheckGracePeriodSeconds=15
    )
    return response
def create_ecs_service ( cluster , service , task_definition_family , target_group_arn , service_count = 1 , launch_type = 'EC2' ) : ecs_client = boto3 . client ( 'ecs' ) if service in ecs_client . list_task_definition_families ( ) [ "families" ] : task_def = ecs_client . list_task_definitions ( familyPrefix = task_definition_family , sort = 'DESC' ) [ "taskDefinitionArns" ] [ 0 ] response = ecs_client . create_service ( cluster = cluster , serviceName = service , taskDefinition = task_def , loadBalancers = [ { 'targetGroupArn' : target_group_arn , 'containerName' : service , 'containerPort' : 8080 } , ] , desiredCount = 1 , launchType = launch_type , deploymentConfiguration = { 'maximumPercent' : 200 , 'minimumHealthyPercent' : 50 } , placementStrategy = [ { 'type' : 'spread' , 'field' : 'attribute:ecs.availability-zone' } , ] , healthCheckGracePeriodSeconds = 15 ) return response
Create ecs service for serving whatever you need.
Create ecs service for serving whatever you need .
"""Create ecs service for serving whatever you need."""
cluster
service
task_definition_family
target_group_arn
service_count
launch_type