Sqlclient: A network-related or instance-specific error occurred while establishing a connection to SQL Server. On AWS Lambda

Created on 4 Dec 2020  路  5Comments  路  Source: dotnet/SqlClient

Describe the bug

With version 2.1.0 of Microsoft.Data.SqlClient attempts to connect to a SQL server database that is not hosted in AWS are met with the below exception.

After failing to find any other cause for this error I rolled back to version 2.0.1 and everything worked fine. I did not see this issue on my local (windows) machine.

Exception message: 

Microsoft.Data.SqlClient.SqlException (0x80131904): 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: 40 - Could not open a connection to SQL Server)

Stack trace:

 at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at Microsoft.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover, SqlAuthenticationMethod authType)
   at Microsoft.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover)
   at Microsoft.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout)
   at Microsoft.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance)
   at Microsoft.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling, String accessToken, DbConnectionPool pool)
   at Microsoft.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
   at Microsoft.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
   at Microsoft.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   at Microsoft.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at Microsoft.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
   at Microsoft.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at Microsoft.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at Microsoft.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry, SqlConnectionOverrides overrides)
   at Microsoft.Data.SqlClient.SqlConnection.Open(SqlConnectionOverrides overrides)
   at Microsoft.Data.SqlClient.SqlConnection.Open()

To reproduce

Attempt to open a connection to a sql server database hosted outside aws from a lambda function.

Expected behavior

The connection should work fine, just like it does in package versions previous to this.

Further technical details

Microsoft.Data.SqlClient version: 2.1.0
.NET target: core 3.1
SQL Server version: SQL Server 2017
Operating system: AWS Lambda Runtime

Most helpful comment

Psychic debugging attempt. Look at the tls versions that the linux machine you're using has setup, recent docker containers on things like alpine have had known problems with not enabling the required version.

All 5 comments

After a bit more investigation I think this is a Linux vs Windows thing, and doesn't have anything to do with AWS, Lambda, or the fact that the database is hosted outside AWS.

@pnquest

After a bit more investigation I think this is a Linux vs Windows thing, and doesn't have anything to do with AWS, Lambda, or the fact that the database is hosted outside AWS.

Is the issue still Microsoft.Data.SqlClient related? If yes, can you provide us a sample repro? besides that can you share your connection string properties?

@JRahnama

Is the issue still Microsoft.Data.SqlClient related? If yes, can you provide us a sample repro? besides that can you share your connection string properties?

Yes, It is definitely the library. There doesn't seem to be much to the repro it fails the instant you call Open:

C# var con = new SqlConnection(conString); con.Open();

The trick seems to be that it only occurs on certain platforms, and not others (Win 10 is fine, but whatever underpins the lambda runtime, Amazon Linux 2 I assume, is not).

Whatever caused this was introduced in the latest release, every prior version this same code works just fine.

The connection string looks like this:

Server=SERVERNAME.DOMAINNAME\\INSTANCENAME; Database=DB_NAME; Encrypt=yes; TrustServerCertificate=true; User Id={userId}; Password={password}

Psychic debugging attempt. Look at the tls versions that the linux machine you're using has setup, recent docker containers on things like alpine have had known problems with not enabling the required version.

@Wraith2 any idea how I would go about checking those from inside a c# program?

@JRahnama Did this latest release drop support for a TLS version? That seems like it would have belonged in a 3.0.0 release rather than 2.1.0.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ph1294 picture ph1294  路  8Comments

MartinHBA picture MartinHBA  路  9Comments

jeroen-mostert picture jeroen-mostert  路  7Comments

amitagrawalhyd picture amitagrawalhyd  路  4Comments

stylesm picture stylesm  路  6Comments