title: Using psycopg2 with Lambda to Update Redshift (Python)
body: <p>I am attempting to update Redshift from a Lambda function using python. To do this, I am attempting to combine 2 code fragments. Both fragments are functional when I run them separately.</p>
  
  <ol>
  <li><p>Updating Redshift from PyDev for Eclipse</p>
  
  <pre><code>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()
  </code></pre></li>
  <li><p>Receiving Content Uploaded to S3 Bucket (Pre-Built Template Available on Lambda)</p>
  
  <pre><code>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
  </code></pre></li>
  </ol>
  
  <p>Since both of these segments worked, I tried to combine them so that I could update Redshift upon the upload of a file to s3:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Since I am using an outside library, I need to create a deployment package. I created a new folder (lambda_function1) and moved my .py file (lambda_function1.py) to that folder. I ran the following command to install psycopg2 in that folder:</p>
  
  <pre><code>pip install psycopg2 -t \lambda_function1
  </code></pre>
  
  <p>I receive the following feedback:</p>
  
  <pre><code>Collecting psycopg2
    Using cached psycopg2-2.6.1-cp34-none-win_amd64.whl
  Installing collected packages: psycopg2
  Successfully installed psycopg2-2.6.1 
  </code></pre>
  
  <p>I then zipped the contents of the directory. And uploaded that zip to my lambda function. When I upload a document to the bucket the function monitors, I receive the following error in my cloudwatch log:</p>
  
  <pre><code>Unable to import module 'lambda_function1': No module named _psycopg 
  </code></pre>
  
  <p>When I look in the library, the only thing named "_psycopg" is "_psycopg.pyd".</p>
  
  <p>What is causing this problem? Does it matter that Lambda uses Python 2.7 when I use 3.4? Does it matter that I zipped the contents of my file on a Windows machine? Has anyone been able to successfully connect to Redshift from lambda? </p>