Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS LAMBDA Connect to RDS SQL database using SqlConnection

How can I connect to a RDS SQL server database on a ASW Lambda function?

This code below work fine on regular C# console app project but giving me error on AWS Lambda project once I deployed it to AWS and tried to invoke it:

public static string CS = "Data Source=host-name, 1433; Initial Catalog=database-name; User ID=username; Password='password';";

    public static List<string> Get_Rule_Group()
    {
        using (SqlConnection con = new SqlConnection(CS))
        {
            string query = "SELECT abc from table where abc = xyz";
            SqlCommand cmd = new SqlCommand(query, con);
            con.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            List<string> Rule_Group = new List<string>();
            while (reader.Read())
            {
                Rule_Group.Add(reader["abc"].ToString());
            }
            return Rule_Group;
        }
    }

Error:

2017-10-06 19:10:27: START RequestId: f33cf098-9360-11e7-96fb-7594ff8505a7 Version: $LATEST
2017-10-06 19:10:43: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 35 - An internal exception was caught)
2017-10-06 19:10:44:    at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling)
2017-10-06 19:10:44:    at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
2017-10-06 19:10:44:    at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
2017-10-06 19:10:44:    at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
2017-10-06 19:10:44:    at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
2017-10-06 19:10:44:    at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
2017-10-06 19:10:44:    at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
2017-10-06 19:10:44:    at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
2017-10-06 19:10:44:    at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
2017-10-06 19:10:44:    at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
2017-10-06 19:10:44:    at System.Data.SqlClient.SqlConnection.Open()
2017-10-06 19:10:44:    at AWSLambdaErrorReport.DAL.Get_Rule_Group()
2017-10-06 19:10:44:    at AWSLambdaErrorReport.Function.<FunctionHandler>d__6.MoveNext()
2017-10-06 19:10:44: FAILED to proceed
2017-10-06 19:10:44: END RequestId: f33cf098-9360-11e7-96fb-7594ff8505a7
2017-10-06 19:10:44: REPORT RequestId: f33cf098-9360-11e7-96fb-7594ff8505a7 Duration: 17209.25 ms   Billed Duration: 17300 ms   Memory Size: 256 MB Max Memory Used: 40 MB
like image 858
Ronaldinho Learn Coding Avatar asked Sep 07 '17 00:09

Ronaldinho Learn Coding


People also ask

How does RDS connect to Lambda database?

To connect a Lambda function to an RDS instance, the networking configurations on each must be set to allow the connection. There are different configuration settings for each of the following connection types: A Lambda function and RDS instance in the same VPC.

How do I connect to AWS RDS database?

Sign in to the AWS Management Console and open the Amazon RDS console at https://console.aws.amazon.com/rds/ . In the navigation pane, choose Databases to display a list of your DB instances. Choose the name of the DB instance to display its details. On the Connectivity & security tab, copy the endpoint.

Can Lambda write to RDS?

Lambda can work seamlessly with RDS instances, as long as you remember the specific requirements for this particular setup. Since RDS instances are running in your VPC and Lambda by default does not have access to those resources, you'll need to configure the VPC connection when creating a Lambda function.


2 Answers

You have to do the following:

  1. Place the Lambda function in the same VPC as the RDS instance.
  2. Open the Security Group assigned to the RDS instance to allow connections from the Lambda function. The easiest way to do this is to create a rule in the RDS security group allowing inbound connections from the Lambda security group.
like image 189
Mark B Avatar answered Oct 19 '22 19:10

Mark B


Did you configure the VPC in your Lambda Function?

Have a look at: http://docs.aws.amazon.com/lambda/latest/dg/vpc-rds.html

like image 29
Tom Melo Avatar answered Oct 19 '22 20:10

Tom Melo