Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass parameters to AWS Lambda function

I have a Lambda function and it supposes to take 3 parameters

public async Task<string> FunctionHandler(string pName, string dictName, ILambdaContext context)
{
//code...
}

I am using Visual Studio 2015, I published this to AWS environment, what do I put in the sample input box to invoke this function? enter image description here

like image 561
Ronaldinho Learn Coding Avatar asked Jan 30 '23 22:01

Ronaldinho Learn Coding


1 Answers

Personally, I've not experimented with async Task in the Lambda entry point so not able to comment on that.

However, another way to go about it is to change the Lambda function entry point to:

public async Task<string> FunctionHandler(JObject input, ILambdaContext context)

Then pull the two variables out of that like so:

string dictName = input["dictName"].ToString();
string pName = input["pName"].ToString();

Then in the AWS Web Console you enter:

{
  "dictName":"hello",
  "pName":"kitty"
}

Or instead one could take the JObject value and use it as shown in the following example code:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace SimpleJsonTest
{
    [TestClass]
    public class JsonObjectTests
    {
        [TestMethod]
        public void ForgiveThisRunOnJsonTestJustShakeYourHeadSayUgghhhAndMoveOn()
        {
            //Need better names than dictName and pName.  Kept it as it is a good approximation of software potty talk.
            string json = "{\"dictName\":\"hello\",\"pName\":\"kitty\"}";

            JObject jsonObject = JObject.Parse(json);

            //Example Zero
            string dictName = jsonObject["dictName"].ToString();
            string pName = jsonObject["pName"].ToString();

            Assert.AreEqual("hello", dictName);
            Assert.AreEqual("kitty", pName);

            //Example One
            MeaningfulName exampleOne = jsonObject.ToObject<MeaningfulName>();

            Assert.AreEqual("hello", exampleOne.DictName);
            Assert.AreEqual("kitty", exampleOne.PName);

            //Example Two (or could just pass in json from above)
            MeaningfulName exampleTwo = JsonConvert.DeserializeObject<MeaningfulName>(jsonObject.ToString());

            Assert.AreEqual("hello", exampleTwo.DictName);
            Assert.AreEqual("kitty", exampleTwo.PName);
        }
    }
    public class MeaningfulName
    {
        public string PName { get; set; }

        [JsonProperty("dictName")] //Change this to suit your needs, or leave it off
        public string DictName { get; set; }
    }

}

The point is I don't know if you can have two input variables in an AWS Lambda. Odds are you can't. Besides it's probably best if you stick with a json string or object to pass in the multiple variables one needs.

like image 128
Zaxxon Avatar answered Feb 02 '23 11:02

Zaxxon