我在下面的场景中尝试使用 terraform api_gateway 进行 POC。
path= /demo/user(GET) -> 调用 lamda 函数 (hello)。
path= /demo/user/{id)(put) -> 调用 lamda 函数(测试)。
所以我在这里创建了以下资源
resource "aws_api_gateway_rest_api" "MyDemoAPI" {
name = "MyDemoAPI"
description = "This is my API for demonstration purposes"
}
resource "aws_api_gateway_resource" "MyDemoResource" {
rest_api_id = aws_api_gateway_rest_api.MyDemoAPI.id
parent_id = aws_api_gateway_rest_api.MyDemoAPI.root_resource_id
path_part = "demo"
}
resource "aws_api_gateway_integration" "MyDemoIntegration" {
rest_api_id = aws_api_gateway_rest_api.MyDemoAPI.id
resource_id = aws_api_gateway_resource.MyDemoResource.id
http_method = aws_api_gateway_method.MyDemoMethod.http_method
type = "AWS_PROXY"
uri = "<lambda function arn>/invocation"
}
Run Code Online (Sandbox Code Playgroud)
在 terraform apply 中,它正在 /demo 下创建资源
,但这里如何实现路径?
path= …
使用 REST API(AWS API Gateway v1),我们可以X-API-Key在请求中使用标头并维护 API 密钥来控制对给定端点的访问。
在 AWS 中,如果我转到 API Gateway 并选择 REST API (v1),则在资源下我会看到:API Key Required在 API 密钥下我可以找到我的密钥。
使用serverless它可以像这样配置:
provider:
apiKeys:
- my-api-key
functions:
hello:
handler: handler.hello
events:
- http:
path: /hello
method: get
private: true
Run Code Online (Sandbox Code Playgroud)
但如果我切换到httpApi然后我得到:
Configuration warning at 'functions.hello.events[0].httpApi': unrecognized property 'private'
Run Code Online (Sandbox Code Playgroud)
此外,在 API Gateway 中,在 HTTP API (v2) 下,任何地方都没有 API 密钥。
当我检查文档时,REST API 下有很大一部分关于 API 密钥:https: //www.serverless.com/framework/docs/providers/aws/events/apigateway 但 API Gateway HTTP API 没有任何关于 API 密钥的内容: https: …
rest amazon-web-services aws-api-gateway serverless aws-http-api
我收到了 SwaggerHub 上托管的 OpenAPI 3.0.1 定义的链接,并被告知要部署它。在 Terraform 方面,我看到太多让我困惑的资源,我不确定该使用哪一个。通过 Terraform 部署已在 OpenAPI 定义中配置的 API 网关的最直接方法是什么?是否有资源可以让我向 API 网关提供 OpenAPI 定义 URL,或者我必须将实际的 JSON 复制粘贴到某处?
在 Lambda 函数前面部署 API 网关函数并使用 Cloudflare 设置它后,我不断收到来自 Cloudflare 的 521 错误。
我已经完成了所有必要的步骤,例如生成证书、在 API Gateway 中创建自定义域等。
为什么在将 AWS API Gateway 与 Cloudflare 结合使用时会收到 521 错误?
我正在使用 AWS CDK 构建 Web 服务。我希望所有端点都需要 api 密钥才能允许连接。
目前我像这样设置我的网络服务:
// Define the API Gateway
const api = new apigw.RestApi(scope, 'MyApiGateway', {
defaultCorsPreflightOptions: {
allowOrigins: apigw.Cors.ALL_ORIGINS,
allowMethods: apigw.Cors.ALL_METHODS, // this is also the default
}
});
const apiKeyName = 'myKeyName';
const apiKey = new apigw.ApiKey(scope, 'api-key', {
apiKeyName,
enabled: true
});
// define the usage plan
const usagePlan = api.addUsagePlan('UsagePlan', {
name: 'UsagePlan',
throttle: {
rateLimit: 100,
burstLimit: 200,
},
});
// add the API key to the usage plan
usagePlan.addApiKey(apiKey); …Run Code Online (Sandbox Code Playgroud) 我为我的API 生成API Gateway SDK了ios.在SDK我有两个类型的文件之一,这使得请求,另一个是处理响应-
1)TESTGetAllDataClient
2)TESTEmpty
let serviceClient = TESTGetAllDataClient.defaultClient()
serviceClient.rootPost("2015").continueWithBlock{ (task:AWSTask!) -> (AnyObject!) in
if task.error != nil {
print(task.error)
} else {
let resultDict :TESTEmpty = task.result as! TESTEmpty
}
return nil
}
Run Code Online (Sandbox Code Playgroud)
这TestEmpty是类型response.但是在我的TestEmpty课程中,我得到了API Gateway SDK,不包含任何提取字典表单对象的方法.TestEmpty implementation class在下面:
@implementation TESTEmpty
+ (NSDictionary *)JSONKeyPathsByPropertyKey {
return @{
};
}
@end
Run Code Online (Sandbox Code Playgroud)
我是否需要编写自己的方法来处理响应,否则它将由SDK?提供?如果您对此有任何了解,请回复.我是新来的AWS API Gateway.
谢谢
当使用ReactJS中的Fetch api对AWS apigateway进行PUT请求时,我一直收到错误消息。几乎可以肯定,我已经在aws中启用了CORS并将其部署。
提取代码如下所示:
fetch(urlUpdate, {
method: "PUT",
headers: {
"Content-Type": "application/json"
},
mode: 'cors',
body: JSON.stringify({
"Item": value;
})
});
Run Code Online (Sandbox Code Playgroud)
GET和POST请求均正常运行,尚未选中DELETE。
我使用无服务器来实现Lambda和Api网关。当我实现错误处理时,下面的代码总是会得到502错误的网关。
handler.js
module.exports.hello = (event, context, callback) => {
const response = {
statusCode: 400,
headers: {
"Content-Type" : "application/json"
},
body: JSON.stringify({
"status": "error",
"message": "Missing Params"
})
};
callback(response);
};
Run Code Online (Sandbox Code Playgroud)
CloudWatch会记录错误。
{
"errorMessage": "[object Object]"
}
Run Code Online (Sandbox Code Playgroud)
我通过遵循AWS博客下面的“自定义错误对象序列化”方法来进行这种编码。 参考
lambda amazon-web-services aws-api-gateway serverless-framework
我正在尝试将Google日历api部署到AWS Lambda.由于我在从事件字典中提取值时遇到问题(由POST请求的JSON有效负载中的lambda创建),我创建了一个玩具函数来测试
def handler(event,context):
a=event.get("type")
if a=='create':
return {
"statusCode": 200,
"headers": { "Content-Type": "text/plain"},
#"body": "Event_id"+ str(event_identifier) + " Event Link: " +str(links)
"body" : str(a)
}
else:
return {
"statusCode": 200,
"headers": { "Content-Type": "text/plain"},
#"body": "Event_id"+ str(event_identifier) + " Event Link: " +str(links)
"body" : "nope"
}
Run Code Online (Sandbox Code Playgroud)
在使用以下JSON在Lambda控制台上进行测试时,我得到了正确的响应.
测试有效负载:{"start_time":"2018-01-24T09:00:00","end_time":"2018-01-24T13:00:00","类型":"创建","event_identifier":"pvno ","摘要":"公司","预订电子邮件":"abc@example.com"}
响应:
{
"body": "create",
"headers": {
"Content-Type": "text/plain"
},
"statusCode": 200
}
Run Code Online (Sandbox Code Playgroud)
当我从postman(二进制或主体POST)发送相同的有效负载(或在API网关控制台上测试)时,当我从event.get("type")返回值时,我得到"None".
为了进一步解释,如果我尝试获取event.get('body')并将其全部作为字符串返回,我得到以下内容,根据lambda事件应该如何工作,这是不正确的:
{
"start_time" : "2018-01-24T09:00:00",
"end_time" : "2018-01-24T13:00:00",
"type": "create",
"event_identifier": "pvnoc",
"summary": "Company", …Run Code Online (Sandbox Code Playgroud) 我是AWS新手,{"message": "Internal server error"}在Postman中使用API Gateway运行Lambda函数时遇到了麻烦。
我已经检查了CloudWatchLogs,日志中没有显示错误。但是邮递员返回了{"message": "Internal server error"}这个错误。
amazon-web-services amazon-cloudwatch aws-lambda aws-api-gateway amazon-cloudwatchlogs
aws-api-gateway ×10
aws-lambda ×2
terraform ×2
api ×1
aws-cdk ×1
aws-http-api ×1
cloudflare ×1
ios ×1
javascript ×1
json ×1
lambda ×1
openapi ×1
python ×1
rest ×1
serverless ×1
swift ×1