franchyze923
franchyze923

Reputation: 1200

Not all Python code executing in AWS Lambda function

I have a simple lambda function which prints an event and then attempts to insert a row into a database. It runs with no error, but does not execute all of the code.

event gets printed, but the row never gets inserted into the table. Anything, even a print statement I put after connection doesn't get executed. I'm guessing something is wrong with the connection, but as far as I know I have no way of telling what is wrong. Are there more logs somewhere? In CloudWatch I see at the end it says Task timed out after 3.00 seconds

import boto3
import psycopg2

s3 = boto3.client('s3')


def insert_data(event=None, context=None):
    print(event)

    connection = psycopg2.connect(user="xxxx", password="xxxx",
                                  host="xxxx", port="xx",
                                  database="xxxx")

    cursor = connection.cursor()
    postgres_insert_query = "INSERT INTO dronedata (name,lat,long,other) VALUES ('img2','54','43','from lambda')"
    cursor.execute(postgres_insert_query)
    connection.commit()
    count = cursor.rowcount
    print(count, "Record inserted successfully into mobile table")

Upvotes: 2

Views: 853

Answers (3)

Mrinal
Mrinal

Reputation: 125

hence psycopg2 is an external lib, please upload that lib along with your code into your Lambda Function. So Issue is, it is not able to connect, that's why you are facing a timeout issue.

Upvotes: 0

John Rotenstein
John Rotenstein

Reputation: 269826

The typical security setup is:

  • A security group on the AWS Lambda function (Lambda-SG) that permits all outbound access (no need for inbound rules)
  • A security group on the database (either an EC2 instance or Amazon RDS) (DB-SG) that permits inbound access on the appropriate port from Lambda-SG

That is, DB-SG should specifically reference Lambda-SG in its inbound rules.

Upvotes: 3

Marcin
Marcin

Reputation: 238497

Yes, you have to increase default Timeout from 3 seconds to more:

Timeout – The amount of time that Lambda allows a function to run before stopping it. The default is 3 seconds. The maximum allowed value is 900 seconds.

enter image description here

Upvotes: 1

Related Questions