title: name 'conn' is not defined: NameError
body: <p>I want to store my aws iot mqtt messages into my postgresql. To do so, I have already connected my local posrtgresql to the amazon RDS instance. Now, I need to create a connection between amazon lambda calculus and then send the data to the postgresql database. But whenever, I am testing my lambda calculus, it was giving me "name 'conn' is not defined: NameError" error. Here, is my python code in aws lambda. I have also included the psycopg2 library to my project.</p>
  
  <pre><code>import sys
  import logging
  import rds_config
  import psycopg2
  #rds settings
  rds_host  = "myhost"
  name = "username"
  password = "username_password"
  db_name = "dbname"
  
  logger = logging.getLogger()
  logger.setLevel(logging.INFO)
  
  try:
      conn = psycopg2.connect(host=rds_host, user=name, password=password, 
             dbname=db_name, connect_timeout=5)
  except:
       logger.error("ERROR: Unexpected error: Could not connect to postgreSQL 
            instance.")
  
  logger.info("SUCCESS: Connection to RDS postgreSQL instance succeeded")
  def handler(event, context):
  """
  This function fetches content from postgreSQL RDS instance
  """
  item_count = 0
  with conn.cursor() as cur: 
      cur.execute('insert into awsiotdata (serialnumber, dateandtime, clicktype, batteryvoltage) values(serialNumber, datetime.datetime.utcnow(), clickType, batteryVoltage)')
      conn.commit()
      cur.execute("select * from awsiotdata")
      for row in cur:
          item_count += 1
          logger.info(row)
          #print(row)
  
  
  return "Added %d items from RDS PostgreSQL table" %(item_count)
  </code></pre>