Python aws-lambda return xml file to aws-api-gateway
<p>I'm trying to build an RESTful service in Amazon Web Service, using API Gateway and Lambdas. One of the API Gateway method is intended to return a single record from a DynamoDB table of a corresponding resource from S3. This resource is an XML file, but I don't know how can I return this content from the Lambda function in a way that its served as a downloadable file.
I'm using Python to code the lambdas, and so far it looks like this:</p>

<pre><code>import json
from lxml import etree

def get_item_handler(event, context):
    # Validate request
    # ...
    # End validation

    logger.info('Querying by id:{0}'.format(event["id"]))
    query_kwargs = {
        'Select': "ALL_ATTRIBUTES",
        'Limit': event["PageSize"] if "PageSize" in event else settings.DEFAULT_PAGE_SIZE,
        'KeyConditionExpression': Key('id').eq(event["id"])
    }

    dynamodb = boto3.resource('dynamodb')
    table = dynamodb.Table(settings.TABLE_NAME)
    response = table.query(**query_kwargs)

    if "format" in event and event["format"] and response["Items"]:
        response_format = event["format"].lower()
        item = json.loads(json.dumps(response['Items'], cls=DecimalEncoder))[0]
        if response_format == "xml":    
            s3_path = get_item_path(item) # Form path to the item in S3
            resource = os.path.join(s3_path , item["resource"])
            local_file = '/tmp/{0}'.format(item["resource"])
            s3_client = boto3.client('s3')
            transfer = S3Transfer(s3_client)
            transfer.download_file(settings.BUCKET_NAME, resource, local_file)
            xml = etree.parse(local_file)
            return etree.tostring(xml)

    return json.dumps(response['Items'], cls=DecimalEncoder)
</code></pre>

<p>The API Gateway is set to application/xml, and it returns an string with the xml content, but this is not what I want, I need to return the XML as a file.</p>