1

I'm currently trying to do a http/s request under aws lambda (C#).

System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
System.Net.Http.HttpResponseMessage response = await client.GetAsync("URL");
string responseText = response.ToString();

I get the following error code. If I manually add the dll it still dont work.

Unable to load DLL 'iphlpapi.dll': The specified module could not be found.

Is it possible to do a request under aws?

My stuff:

  • Msvc 15
  • Amazon Lambda Core, Amazon Lambda Tools, Amazon Lambda Serialization Json
  • netcore 1.0
1
  • I don't know what that dll is but it has nothing to do with making an HttpClient request. The AWS service is just another http endpoint from your program's perspective. Commented Jan 11, 2017 at 11:55

2 Answers 2

5

I ran into the same issue and finally found the solution. Effectively what the error is telling you is that the iphlpapi.dll assembly does not exist in the Lambda execution environment. This is the system level assembly that gets pinvoked from HttpClient to handle the actual TCPIP requests. In researching this issue, I found that AWS Lambda's run in an AWS Linux AMI environment. Obviously, a windows system assembly will not be available.

Diving into .Net core, there is an ability to target the runtime environment and build your .Net core for the specific environments. This is only really needed for pulling in the correct system level dependencies from nuget.

The solution was to add a runtimes node to my project.json that included a linux target. .Net does not have a target specifically for AWS Linux AMI so I just used an Ubuntu.14.04-64 target and that seemed to work.

"runtimes": {
    "ubuntu.14.04-x64": {},
    "win10-x64": {}
}

The other hurdle to get around is that VS does not seem to allow me to compile for the ubuntu runtime, so I had to drop to the command line and use the DotNet CLI. Navigate to your project directory and issue the following commands:

dotnet restore dotnet publish -c release -r ubuntu.14.04-x64

This will get all of the appropriate nuget dependencies for Ubuntu and then build your project for ubuntu. All of the assemblies will be placed in the /bin/release/netcoreapp1.0/ubuntu.14.04-x64/publish directory. Just zip up these files and you can then upload this package to your Lambda function.

Note: I was not able to get the AWS toolkit function window to upload my file so I had to resort to uploading it from the AWS console website.

Once I finished uploading the new package, everything worked as expected and I no longer get the error.

Sign up to request clarification or add additional context in comments.

4 Comments

Consider using "dotnet lambda package". No need to edit your project file, and it creates the .zip file for you.
@swbradshaw That is awesome. Thanks for sharing.
@swbradshaw In trying this out, I found that it doesn't actually target Ubuntu or any specific Linux system on my machine. When I look in the Ubuntu version I created previously I see .so files which is what I expect. When I look in the zip created by dotnet lambda, those Linux assemblies are missing. I need to do more testing on this.
You'll also notice the new .zip file is much smaller. It doesn't package up assemblies that you don't need. It will work fine. I believe "dotnet lambda package" is the same thing that the Visual Studio Toolkit GUI window is using.
3

I'm curious about what your project.json file looks like? Targeting .NETCoreApp 1.0.3 (C# on Lambda supports .NETCoreApp 1.0, and 1.0.3 is the LTS version for .NETCoreApp 1.0), I don't have any issue with having to specify a runtime to publish dependencies for.

This is my project.json:

{
    "version": "1.0.0-*",
    "dependencies": {
        "Amazon.Lambda.Core": "1.0.0",
        "Amazon.Lambda.Serialization.Json": "1.0.1"
    },
    "frameworks": {
        "netcoreapp1.0": {
            "dependencies": {
                "Microsoft.NETCore.App": {
                    "type": "platform",
                    "version": "1.0.1"
                }
            },
            "imports": "dnxcore50"
        }
    }
}

And for the code:

using System.Net.Http;
using System.Threading.Tasks;

using Amazon.Lambda.Core;
using Amazon.Lambda.Serialization.Json;

namespace LambdaHttpRequest
{
    public class Handler
    {
        [LambdaSerializer(typeof(JsonSerializer))]
        public static async Task<string> MakeRequest()
        {
            HttpClient client = new HttpClient();
            HttpResponseMessage response = await client.GetAsync("https://www.amazon.com/");
            return response.ToString();
        }
    }
}

When I run a dotnet publish (or use Visual Studio), I only see these assemblies, and no subdirectories for platform-specific assemblies:

  • LambdaHttpRequest.dll
  • Newtonsoft.Json.dll
  • Amazon.Lambda.Core.dll
  • Amazon.Lambda.Serialization.Json.dll
  • System.Runtime.Serialization.Primitives.dll

I believe the issue may be you targeting the full .NET framework, or some other version besides .NETCoreApp 1.0.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.