Anshul Johri
Anshul Johri

Reputation: 102

Call a aws Lambda function from another lambda function in C#

I am new to aws lambda functions with c#. I have two lambda functions and I want to call one lambda function from second function, I am using code as below:

public string Function1(JObject input)
{
    string param = input["param"].ToString();
    string param1 = input["param1"].ToString();
    return param.ToUpper()+" "+param1.ToUpper();
}


public string Function2()
{
    try
    {
        using (AmazonLambdaClient client = new AmazonLambdaClient(some region))
        {
            JObject ob = new JObject();
            ob.Add("param", "hello");
            ob.Add("param1", "Lambda");
            var request = new InvokeRequest
            {
                FunctionName = "Function1",
                Payload = ob.ToString()
            };
            var response = client.Invoke(request);
            string result;
            using (var sr = new StreamReader(response.Payload))
            {
                return result = sr.ReadToEnd();
            }
        }
    }
    catch (Exception ex) 
    {
        return ex.Message.ToString();
    }
}

And I am getting an error as below:

{ "errorType": "TypeLoadException", "errorMessage": "Could not load type 'System.Net.HttpStatusCode' from assembly 'System, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e, Retargetable=Yes'.", "stackTrace": [ "at AWSLambdaApp.Function.Function2()", "at lambda_method(Closure , Stream , Stream , ContextInfo )" ] }

And in function 2 application I have added a reference of "AWSSDK.Core" and "AWSSDK.Lambda" dlls. Can any one tell me what I am doing wrong?

Upvotes: 8

Views: 10151

Answers (2)

RredCat
RredCat

Reputation: 5421

I don't have 'netcoreapp1.0' installed on my PC. So I have tried on 'netcoreapp2.0'.

And my code looks like:

public string FunctionHandler(JObject input, ILambdaContext context)
{
    param = input["param"].Value<string>();
    param1 = input["param1"].Value<string>();
    param.ToUpper() + " " + param1.ToUpper();
}

and for Function2:

public async Task<string> FunctionHandler(string input, ILambdaContext context)
{
    try
    {
        using (AmazonLambdaClient client = new AmazonLambdaClient(RegionEndpoint.USEast1))
        {
            JObject ob = new JObject { { "param", "hello" }, { "param1", "Lambda" } };

            var request = new InvokeRequest
            {
                FunctionName = "Function1",//Function1:v1 if you use alias
                Payload = ob.ToString()
            };

            var response = await client.InvokeAsync(request);

            using (var sr = new StreamReader(response.Payload))
            {
                return await sr.ReadToEndAsync();
            }
        }
    }
    catch (Exception ex)
    {
        return ex.Message;
    }
}

And the result of execution is "\"HELLO LAMBDA\"".

The reason for work of my code could be:

  • How I get parameter from input in Function1. It is most probably.
  • My async code. I can't use the synchronous code for call lambda with the latest SDK.
  • The version of .net core.

Also, I need send a simple string as the parameter of Function2.

Upvotes: 8

RredCat
RredCat

Reputation: 5421

I think you have to rethink your architecture. Keep in mind that the lambda function has limitations (time of execution & CPU usage).

It is better to use aws step function for such cases. In this case, you could use the Chain of Responsibility pattern. Lambda functions will be decoupled, and it is easier to support them. You will have something like as a result:

AWS Step Function Example

Moreover, aws step-functions has a lot of features like condition, parallel execution, waiters so on. It gives you powerful tools for improving your application.

Upvotes: 0

Related Questions