Search code examples
aws-lambdaamazon-rdssqlclient

Trouble connecting to SQL Server Express with a C# AWS Lambda function


I get the following error when trying to connect to my SQL Server Express RDS instance over AWS Lambda using System.Data.SqlClient:

"errorType": "PlatformNotSupportedException",
"errorMessage": "Operation is not supported on this platform.",
"stackTrace": [
"at System.Runtime.InteropServices.OSPlatform.get_Windows()",
"at System.Data.SqlClient.SNI.SNITCPHandle.d__22.MoveNext()"
]

I build the deploy package using Amazon.Lambda.Tools. Running the function and connecting to the database from local works fine, but fails when trying to run it on AWS Lambda.

Here is my project.json:

{
"version": "1.0.0-*",
"description": "AWS lambda Console Application",
"authors": [ "" ],
"packOptions": {
    "tags": [],
    "projectUrl": "",
    "licenseUrl": ""
},
"buildOptions": {
    "emitEntryPoint": true
},
"dependencies": {
    "Microsoft.NETCore.App": {
    "type": "platform",
    "version": "1.0.0"
    },
    "Newtonsoft.Json": "9.0.1",
    "Amazon.Lambda.Core": "1.0.0*",
    "Amazon.Lambda.Serialization.Json": "1.0.1",
    "Amazon.Lambda.Tools": {
    "type": "build",
    "version": "1.0.0-preview1"
    },
    "Dapper": "1.50.2",
    "System.Data.SqlClient": "4.3.0"
},
"tools": {
    "Amazon.Lambda.Tools": "1.0.0-preview1"
}, 
"commands": {
    "AwsLambda": "run"
},
"frameworks": {
    "netcoreapp1.0": {
        "imports": "dnxcore50"
    }
}
}

Solution

  • Had the same problem and for me just changing

    "System.Data.SqlClient": "4.3.0"
    

    to

    "System.Data.SqlClient": "4.1.0"
    

    worked for me. I did not have to change the NetCore.App dependencies.