"""Script that starts SWF execution from AWS Lambda. Code writen in python 2.7 (AWS Lambda for now supports only that version). Functions such as urllib.unquote_plus won't work in python 3+. """ import json from os import environ import urllib import uuid import boto3 SWF_DOMAIN = environ.get('SWF_DOMAIN') or 'dev' SWF_REGION_NAME = 'us-east-1' SWF_WORKFLOW_ID_PATTERN = 'ows_ft_etl_theatrical_cuts_{cid}' SWF_WORKFLOW_NAME = 'ows_ft_etl_theatrical_cuts' SWF_WORKFLOW_TIMEOUT = '3600' SWF_WORKFLOW_VERSION = '1.0' def lambda_handler(event, context): """Event handler. Process event triggered by Object creation on S3. Args: event (dict): AWS Lambda event data. context (LambdaContext): AWS Lambda context. Returns: dict: dict containing the runId of the execution. """ bucket = event['Records'][0]['s3']['bucket']['name'] key = urllib.unquote_plus( event['Records'][0]['s3']['object']['key'].encode('utf8')) correlation_id = str(uuid.uuid1()) swf_context = { 'correlation_id': correlation_id, 's3_key': key, 's3_bucket': bucket } swf = boto3.client('swf', region_name=SWF_REGION_NAME) response = swf.start_workflow_execution( input=json.dumps(swf_context), domain=SWF_DOMAIN, taskList={'name': SWF_WORKFLOW_NAME}, executionStartToCloseTimeout=SWF_WORKFLOW_TIMEOUT, workflowId=SWF_WORKFLOW_ID_PATTERN.format(cid=correlation_id), workflowType={ 'name': SWF_WORKFLOW_NAME, 'version': SWF_WORKFLOW_VERSION}) return response