AWS Lambda参数传递

时间:2016-02-19 12:12:58

标签: python amazon-web-services aws-lambda

我正在使用带有Lambda函数的AWS API Gateway创建一个api。我希望能够使用以下标准进行API调用:

  • 在API的方法请求中,我指定了查询字符串:itemid
  • 我希望能够在我的lambda函数中使用此itemid值
  • 我在Lambda中使用Python

我已尝试将以下内容放在方法执行下的Mapping模板中,但是会收到错误:

- {“itemid”:“$ input.params('itemid')”}

2 个答案:

答案 0 :(得分:0)

您还必须在资源/方法请求部分中包含查询字符串参数。

答案 1 :(得分:0)

我会在API网关中使用代理集成,这样您的Lambda函数就会收到整个请求。请查看the docs进行设置。配置为代理时,Lambda函数的输入将如下所示:

{  
   "path":"/test/hello",
   "headers":{  
      "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
      "Accept-Encoding":"gzip, deflate, lzma, sdch, br",
      "Accept-Language":"en-US,en;q=0.8",
      "CloudFront-Forwarded-Proto":"https",
      "CloudFront-Is-Desktop-Viewer":"true",
      "CloudFront-Is-Mobile-Viewer":"false",
      "CloudFront-Is-SmartTV-Viewer":"false",
      "CloudFront-Is-Tablet-Viewer":"false",
      "CloudFront-Viewer-Country":"US",
      "Host":"wt6mne2s9k.execute-api.us-west-2.amazonaws.com",
      "Upgrade-Insecure-Requests":"1",
      "User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36 OPR/39.0.2256.48",
      "Via":"1.1 fb7cca60f0ecd82ce07790c9c5eef16c.cloudfront.net (CloudFront)",
      "X-Amz-Cf-Id":"nBsWBOrSHMgnaROZJK1wGCZ9PcRcSpq_oSXZNQwQ10OTZL4cimZo3g==",
      "X-Forwarded-For":"192.168.100.1, 192.168.1.1",
      "X-Forwarded-Port":"443",
      "X-Forwarded-Proto":"https"
   },
   "pathParameters":{  
      "proxy":"hello"
   },
   "requestContext":{  
      "accountId":"123456789012",
      "resourceId":"us4z18",
      "stage":"test",
      "requestId":"41b45ea3-70b5-11e6-b7bd-69b5aaebc7d9",
      "identity":{  
         "cognitoIdentityPoolId":"",
         "accountId":"",
         "cognitoIdentityId":"",
         "caller":"",
         "apiKey":"",
         "sourceIp":"192.168.100.1",
         "cognitoAuthenticationType":"",
         "cognitoAuthenticationProvider":"",
         "userArn":"",
         "userAgent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36 OPR/39.0.2256.48",
         "user":""
      },
      "resourcePath":"/{proxy+}",
      "httpMethod":"GET",
      "apiId":"wt6mne2s9k"
   },
   "resource":"/{proxy+}",
   "httpMethod":"POST",
   "body": "...",
   "queryStringParameters":{  
      "name":"me"
   },
   "stageVariables":{  
      "stageVarName":"stageVarValue"
   }
}