将参数传递给AWS Lambda函数

时间:2017-07-25 06:32:45

标签: c# amazon-web-services lambda aws-lambda

我有一个Lambda函数,它假设采用3个参数

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

我正在使用Visual Studio 2015,我将其发布到AWS环境,我在示例输入框中放置什么来调用此函数? enter image description here

1 个答案:

答案 0 :(得分:6)

就个人而言,我没有在Lambda入口点试验过异步任务,因此无法对此发表评论。

然而,另一种方法是将Lambda函数入口点更改为:

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

然后将两个变量拉出来:

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

然后在AWS Web Console中输入:

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

或者可以使用JObject值并使用它,如以下示例代码所示:

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; }
    }

}

重点是我不知道您是否可以在AWS Lambda中拥有两个输入变量。赔率是你不能。除此之外,如果你坚持使用json字符串或对象传递一个需要的多个变量,这可能是最好的。