-1

I am trying to connect with my RDS SQL Server from AWS lambda function. Below is the sample code I am using. Can anyone suggest what I am doing wrong. Below error message:

[ERROR] ConnectionClosedError: Connection was closed before we received a valid response from endpoint URL: "https://rds-data.us-east-1.amazonaws.com/BatchExecute".

def lambda_handler(event, context):
    response = rds_client.batch_execute_statement(
    database = database_name,
    resourceArn = db_cluster_arn,
    secretArn = secret_arn,
    sql = query
    )
    print(response['Records'])
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470

1 Answers1

0

The URL endpoint https://rds-data.us-east-1.amazonaws.com/ looks like an Aurora Serverless endpoint. In that case the DB is not in your VPC. To connect to it you need to deploy your Lambda into a VPC and then to make it connect to the DB you have one of 2 options.

  1. Create a VPC endpoint. See https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/vpc-interface-endpoints.html
  2. Create a NAT Gateway in the Public subnet and route your Lambda trafic via the NAT Gateway. See AWS Lambda: How to set up a NAT gateway for a lambda function with VPC access.

The VPC endpoint is a better option as it provides better security.