Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda access local resource or storage C#

How can I store and access a file in Lambda using C# I used the tmp folder that is available for the lambda but I get an error of could not load file or assembly. How can I solve the error? I used the ADP nuget.

        using (WebClient webClient = new WebClient())
        {
            webClient.DownloadFile(reportLine, Path.GetTempPath() + 
            "sample_auth.key");
        }

I used this to download the file into the tmp folder of the lambda. I did not include the other config in the string confidential but you can check the github below for the exact same sample.

    string config = @"{
           ""sslCertPath"": ""/tmp/sample.pfx"",
           ""sslKeyPath"": ""/tmp/sample_auth.key"",
           }";

        ADPAccessToken token = null;

        if (String.IsNullOrEmpty(clientconfig))
        {
            Console.WriteLine("Settings file or default options not available.");
        }
        else
        {
              ClientCredentialConfiguration connectionCfg = JSONUtil.Deserialize<ClientCredentialConfiguration>(clientconfig);
              ClientCredentialConnection connection = (ClientCredentialConnection)ADPApiConnectionFactory.createConnection(connectionCfg);

            //context.Logger.Log(ADPApiConnection.certificatepath);
            //context.Logger.Log(clientconfig);
            try
            {
                connection.connect();
                if (connection.isConnectedIndicator())
                {
                token = connection.accessToken;

                    //    context.Logger.Log("Connected to API end point");
                    //    //Console.WriteLine("Token:  ");
                    //    //Console.WriteLine("         AccessToken: {0} ", token.AccessToken);
                    //    //Console.WriteLine("         TokenType: {0} ", token.TokenType);
                    //    //Console.WriteLine("         ExpiresIn: {0} ", token.ExpiresIn);
                    //    //Console.WriteLine("         Scope: {0} ", token.Scope);
                    //    //Console.WriteLine("         ExpiresOn: {0} ", token.ExpiresOn);
                    //    //Console.ReadLine();
                }
            }
            catch (ADPConnectionException e)
            {
                context.Logger.Log(e.Message);
            }
            //catch (Exception e)
            //{
            //    context.Logger.Log(e.Message);
            //}
            //Console.Read();
        }

        return "Ok";
    }

I get an error of I think lambda check the /var/task folder

errorMessage": "One or more errors occurred. (Could not load file or 
assembly 'System.Net.Http.WebRequest, Version=4.0.0.0, Culture=neutral, 
PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file 
specified.\n)",
 "cause":   {
"errorType": "FileNotFoundException",
"errorMessage": "Could not load file or assembly 
'System.Net.Http.WebRequest, Version=4.0.0.0, Culture=neutral, 
  PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file 
 specified.\n",

Here is the sample program: https://github.com/adplabs/adp-connection-NET/blob/master/ADPClientDemo/Program.cs

I can run the program on console but when I try to do it in lambda I get an error. Is it because of the NuGet from AWS?

I have the following NuGet

Amazon Lambda Core
Amazon Lambda S3 Events 
Amazon lambda Serialization json
AWS SDK Core 
Microsoft Asp Net Web Api Client 
ADP library connection NET
like image 669
John Avatar asked Sep 28 '18 01:09

John


People also ask

Can Lambda access local files?

You can configure a function to mount an Amazon Elastic File System (Amazon EFS) file system to a local directory. With Amazon EFS, your function code can access and modify shared resources safely and at high concurrency.

Does AWS Lambda have local storage?

Since customers could not cache larger data locally in the Lambda execution environment, every function invoke had to read data in parallel, which made scaling out harder for customers. Today, we are announcing that AWS Lambda now allows you to configure ephemeral storage ( /tmp ) between 512 MB and 10,240 MB.

What AWS resources can Lambda access?

You can now enable AWS Lambda to access resources in a Virtual Private Cloud (VPC). Your Lambda functions can now access Amazon RDS databases, Amazon Redshift data warehouses, Amazon ElasticCache nodes, and other endpoints that are accessible only from within a particular VPC (e.g., web service running on EC2).


1 Answers

Could not load file or assembly: System.Net.Http.WebRequest

The error seems to be caused by a versioning issue, I think you need to use a .Net core version of System.Net.Http.WebRequest dll or a later version than .Net 4.0 to work with .NET Core 2.0.

Actually please see this answer you might be out of luck: The libraries you use need to ship targeting .NET Core: https://github.com/dotnet/corefx/issues/28267#issuecomment-396349873

Also see https://stackoverflow.com/a/41683787/495455 and Could not load file or assembly "System.Net.Http.Webrequest" in .NET AWSSDK for mono for similar versioning issues and fixes.


If that doesn't fix it, please consider using the AWS API. You could put your sample_auth.key file on an S3 bucket and read it, eg https://docs.aws.amazon.com/lambda/latest/dg/with-s3.html


Or as per the example you linked to they package the json file with the Lambda: https://github.com/adplabs/adp-connection-NET/tree/master/ADPClientDemo/Content/config

And they read it using StreamReader, perhaps this will work using the System.IO dll instead of trying to find the System.Net.Http.WebRequest dll:

string configFileName = "default.json";
StreamReader sr = new StreamReader("..\\..\\Content\\config\\" + configFileName);
string clientconfig = sr.ReadToEnd();
like image 190
Jeremy Thompson Avatar answered Oct 07 '22 23:10

Jeremy Thompson