Usando psycopg2 con Lambda para actualizar Redshift (Python)

Estoy intentando actualizar Redshift desde una función Lambda usando python. Para hacer esto, estoy tratando de combinar 2 fragmentos de código. Ambos fragmentos son funcionales cuando los ejecuto por separado.

Actualización de Redshift desde PyDev para Eclipse

import psycopg2

conn_string = "dbname='name' port='0000' user='name' password='pwd' host='url'"
conn = psycopg2.connect(conn_string)

cursor = conn.cursor()

cursor.execute("UPDATE table SET attribute='new'")
conn.commit()
cursor.close()

Recepción de contenido cargado en S3 Bucket (plantilla preconstruida disponible en Lambda)

from __future__ import print_function

import json
import urllib
import boto3

print('Loading function')

s3 = boto3.client('s3')


def lambda_handler(event, context):
    #print("Received event: " + json.dumps(event, indent=2))

    # Get the object from the event and show its content type
    bucket = event['Records'][0]['s3']['bucket']['name']
    key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key']).decode('utf8')

    try:
        response = s3.get_object(Bucket=bucket, Key=key)
        print("CONTENT TYPE: " + response['ContentType'])
        return response['ContentType']

    except Exception as e:
        print(e)
        print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket))
        raise e

Como ambos segmentos funcionaron, intenté combinarlos para poder actualizar Redshift al cargar un archivo a s3:

from __future__ import print_function

import json
import urllib
import boto3
import psycopg2

print('Loading function')

s3 = boto3.client('s3')


def lambda_handler(event, context):
    #print("Received event: " + json.dumps(event, indent=2))

    # Get the object from the event and show its content type
    bucket = event['Records'][0]['s3']['bucket']['name']
    key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key']).decode('utf8')

    conn_string = "dbname='name' port='0000' user='name' password='pwd' host='url'"

    conn = psycopg2.connect(conn_string)

    cursor = conn.cursor()

    cursor.execute("UPDATE table SET attribute='new'")
    conn.commit()
    cursor.close()

    try:
        response = s3.get_object(Bucket=bucket, Key=key)
        print("CONTENT TYPE: " + response['Body'].read())
        return response['Body'].read()
    except Exception as e:
        print(e)
        print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket))
        raise e

Como estoy usando una biblioteca externa, necesito crear un paquete de implementación. Creé una nueva carpeta (lambda_function1) y moví mi archivo .py (lambda_function1.py) a esa carpeta. Ejecuté el siguiente comando para instalar psycopg2 en esa carpeta:

pip install psycopg2 -t \lambda_function1

Recibo los siguientes comentarios:

Collecting psycopg2
  Using cached psycopg2-2.6.1-cp34-none-win_amd64.whl
Installing collected packages: psycopg2
Successfully installed psycopg2-2.6.1 

Luego comprimí el contenido del directorio. Y subí ese zip a mi función lambda. Cuando subo un documento al bucket que la función monitorea, recibo el siguiente error en mi registro de cloudwatch:

Unable to import module 'lambda_function1': No module named _psycopg 

Cuando miro en la biblioteca, lo único que se llama "_psycopg" es "_psycopg.pyd".

que esta causando este problema? ¿Importa que Lambda use Python 2.7 cuando yo uso 3.4? ¿Importa que haya comprimido el contenido de mi archivo en una máquina con Windows? ¿Alguien ha podido conectarse con éxito a Redshift desde lambda?

Respuestas a la pregunta(1)

Su respuesta a la pregunta