def _update_routes()

in blogs/ecs-canary-deployments-pipeline/shared_stack/lambda_functions/rollbackto_previous_canary/main.py [0:0]


def _update_routes(event, healthy_deployment):
    """ Update routes in AppMesh VirtualRouter """

    try:
        entries = APPMESH_CLIENT.describe_route(
            meshName=event['EnvironmentName'],
            routeName=event['MicroserviceName']+'-'+'route',
            virtualRouterName=event['MicroserviceName']+'-'+'vr'
        )['route']['spec']['httpRoute']['action']['weightedTargets']

        if len(entries) > 1:
            if event['Protocol'].lower() == 'http':
                spec={
                    'httpRoute': {
                        'action': {
                            'weightedTargets': [
                                {
                                    'virtualNode':event['MicroserviceName']+'-'+healthy_deployment,
                                    'weight':100
                                }
                            ]
                        },
                        "match": {
                                    "prefix": "/"
                                },
                        'retryPolicy': {
                            'httpRetryEvents': [
                                'server-error',
                                'client-error',
                                'gateway-error'
                            ],
                            'maxRetries': 2,
                            'perRetryTimeout': {
                                'unit': 'ms',
                                'value': 2000
                            }
                        }
                    }
                }
            else:
                spec={
                    'tcpRoute': {
                        'action': {
                            'weightedTargets': [
                                {
                                    'virtualNode':event['MicroserviceName']+'-'+event['Sha'],
                                    'weight':100
                                }
                            ]
                        },
                        'timeout': {
                            'idle': {
                                'unit': 'ms',
                                'value': 2000
                            }
                        }
                    }
                }
            APPMESH_CLIENT.update_route(
                meshName=event['EnvironmentName'],
                routeName=event['MicroserviceName']+'-'+'route',
                spec=spec,
                virtualRouterName=event['MicroserviceName']+'-'+'vr'
            )
            return True
    except ClientError as ex:
        LOGGER.error("Update route failed with error: %s", ex)
        return False