【发布时间】:2019-08-27 22:21:00
【问题描述】:
TL;DR:使用 AWS Lambda 和 API-Gateway 遇到错误
背景
我编写了一个 python 脚本来帮助我跟踪我正在使用的一些指数基金。它基本上是从不同网站上抓取(使用请求和 bs4)价格。
我想把它变成一个 API,这样我就可以在任何地方使用它,所以我使用 Flask 和 Connexion 来制作它(关注这个 https://realpython.com/flask-connexion-rest-api/)。这一切在本地都运行良好,但我在 AWS 上实施时遇到了麻烦。
我已将我的代码和相应的应用程序包压缩到 AWS Lambda 并在 API Gateway 中使用。但是我收到以下错误:
Endpoint response body before transformations: {"errorMessage": "module initialization error"}
Method response body after transformations: {"errorMessage": "module initialization error"}
我已经在谷歌上搜索过,按照此处的步骤 (Amazon API Gateway : response body is not transformed when the API is called via Postman?),部署了应用程序等 - 不知道为什么会这样。
这是一个sn-p的代码:
import requests
from bs4 import BeautifulSoup
from datetime import date, timedelta
import datetime
import json
def CurrentPortfolioValue(event, context):
initial_porfolio_price = float (100)
initial_date = date(2018, 8, 19) #set the initial date to a date where the fortnightly payment has gone
num_of_shares_vanguard = float(500)
num_of_shares_AMPNZShares = float(300)
num_of_shares_AMPAustralisianProperty = float(100)
fortnightly_contrib = float(100)
current_value = initial_porfolio_price
#print(initial_date)
#sets a user-agent
example_headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0'} #can make this better by having a list of random header
#vanguard
vanguard = requests.get("https://www.bloomberg.com/quote/VAN1579:AU",headers=example_headers) #need to have headers to avoid being blocked
soup = BeautifulSoup(vanguard.content, 'html.parser')
vanguard_price = soup.find_all("span", class_ = 'priceText__1853e8a5')[0].get_text()
#print("Vanguard Price:\n" + vanguard_price+"AUD")
#AMP Capital NZ Index
ampnzshares = requests.get("https://www.ampcapital.com/nz/en/investments/funds/index-funds/nz-shares-index-fund",headers=example_headers) #need to have headers to avoid being blocked
soup1 = BeautifulSoup(ampnzshares.content, 'html.parser')
ampnzshares_price = soup1.find_all("div", class_ = 'ht-module_title ht-highlight_color')[1].get_text()
#print("AMP Capital NZ Index Price:\n" + ampnzshares_price+"NZD")
#AMP Capital Australisian Property Index Fund
ampAustralisianProperty = requests.get("https://www.ampcapital.com/nz/en/investments/funds/index-funds/australasian-property-index-fund",headers=example_headers) #need to have headers to avoid being blocked
soup2 = BeautifulSoup(ampAustralisianProperty.content, 'html.parser')
ampAustralianProperty_price = soup2.find_all("div", class_ = 'ht-module_title ht-highlight_color')[1].get_text()
#print("AMP Capital Australisian Property Index Fund:\n" + ampAustralianProperty_price+"NZD")
#get day difference between today and initial date
current_date = date.today()
days_difference = (current_date - initial_date).days #.days shows just the number of days
#print(days_difference)
count = days_difference
while count > 14: #while difference is less than 14 days i.e forntightly payment hasnt been made
current_value += initial_porfolio_price + (float(50) * float(vanguard_price)) + (float(50) * float(ampnzshares_price))
count = count - 14
print("Current Portfolio value is $" + str(current_value))
return {
"statusCode": 200,
"body": json.dumps(current_value)}
CurrentPortfolioValue() #retrieves current portfolio value
我的 yaml 文件看起来像这样:
swagger: "2.0"
info:
description: This is the swagger file that goes with our server code
version: "1.0.0"
title: Swagger REST Article
consumes:
- "application/json"
produces:
- "application/json"
basePath: "/api"
paths:
/value: #name of the endpoint
get:
operationId: "ShareScraper.CurrentPortfolioValue" #link to the module - this is called when you navigate to /value
tags:
- "Portfolio"
summary: "The portfolio data structure supported by the server application"
description: "Read the current portfolio value"
responses:
200:
description: "Successful read current portfolio value"
schema:
type: "string"
items:
properties:
portfolio_value:
type: "string"
提前致谢!
[编辑] 这是我第一次创建 api 并使用 AWS,如果我遗漏了一些相当简单/明显的东西,非常抱歉
【问题讨论】:
-
你的
current_value定义了吗?功能代码正在向其附加一个数字。 -
@MunavirChavody 是的 current_value 已定义。我没有包含那部分代码,因为它只是计算。代码完全按预期工作,只是在 aws 中不起作用
-
你可以尝试单独运行 lambda 函数,看看执行情况如何?你可能会在那里得到更详细的错误。
-
我不太确定如何只运行 lambda 函数,它是从“测试”按钮开始的吗? (我使用在线控制台)
-
是的。相同的。使用测试按钮。如果您有任何测试对象要传递给
event变量,请将其传递给弹出窗口。
标签: python amazon-web-services rest aws-lambda