2017-09-07 102 views
4

如何連接到ASW Lambda函數上的RDS SQL服務器數據庫?AWS LAMBDA使用SqlConnection連接到RDS SQL數據庫

這下面的代碼在常規的C#控制檯應用程序項目工作正常,但給我的錯誤在AWS上LAMBDA項目一旦我部署它AWS並試圖調用它:

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; 
     } 
    } 

錯誤:

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 
+0

嗨,你有這個任何解決方案。我也面臨同樣的問題。 –

回答

0

你必須做到以下幾點:

  1. 將拉姆達功能在同一VPC的RDS實例。
  2. 打開分配給RDS實例的安全組以允許來自Lambda函數的連接。最簡單的方法是在RDS安全組中創建一個允許來自Lambda安全組的入站連接的規則。
+0

如何知道我的Lambda函數和RDS intance是否在同一個VPC中?當我看到我的RDS實例時,我看到VPC名稱,但我的Lamdba函數沒有顯示它所在的VPC? –

+0

Nmv我在配置選項卡下看到了Lambda的VPC選項,它們在相同的VPC –

+0

OK,LAMA的IAM角色具有完全訪問幾乎所有的功能(Lambda,S3,Clouwatch,RDS,EC2等),安全組因爲RDS可以接受來自世界各地的所有TRAFFICS,仍然存在相同的問題。我不知道.Net核心和LAmbda不支持ADO.NET(sqlconnection等) –