【问题标题】:Pass parameters to AWS Lambda function将参数传递给 AWS Lambda 函数
【发布时间】:2017-12-30 22:08:28
【问题描述】:

我有一个 Lambda 函数,它需要 3 个参数

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

我使用的是 Visual Studio 2015,我将它发布到 AWS 环境,我在示例输入框中输入什么来调用这个函数?

【问题讨论】:

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


    【解决方案1】:

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

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

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

    然后像这样将两个变量拉出来:

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

    然后在 AWS Web 控制台中输入:

    {
      "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 字符串或对象来传递一个需要的多个变量,那可能是最好的。

    【讨论】:

      猜你喜欢
      • 2016-04-10
      • 1970-01-01
      • 2017-12-25
      • 1970-01-01
      • 2019-07-05
      • 2016-03-27
      • 2021-12-13
      • 2019-04-03
      • 1970-01-01
      相关资源
      最近更新 更多