Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 903
Anshul Johri Avatar asked Dec 05 '17 12:12

Anshul Johri


People also ask

Can you call a lambda function from another lambda function?

In order to allow the ParentFunction to call the ChildFunction, we need to provide the ParentFunction with specific rights to call another lambda function. This can be done by adding specific policies to a role and then assign that role to the lambda function.

How do you call a lambda function from another account?

To have your Lambda function assume an IAM role in another AWS account, do the following: Configure your Lambda function's execution role to allow the function to assume an IAM role in another AWS account. Modify your cross-account IAM role's trust policy to allow your Lambda function to assume the role.

How do you call a function in AWS Lambda?

You can invoke Lambda functions directly using the Lambda console, a function URL HTTP(S) endpoint, the Lambda API, an AWS SDK, the AWS Command Line Interface (AWS CLI), and AWS toolkits.


1 Answers

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.

like image 98
RredCat Avatar answered Nov 01 '22 18:11

RredCat