joh*_*aws 5 geolocation amazon-web-services aws-lambda aws-api-gateway
我们正在努力做什么
我们正在尝试使用 API Gateway 和 Lambda 建立一个非常简单的地理定位服务。
与https://ipstack.com/非常相似,但我们不想使用外部服务,因为我们认为在某些司法管辖区将非匿名 IP 地址发送到我们无法控制的服务可能会出现问题(在征得用户同意之前)。
想要一个简单的 api https://location.my-site.com返回国家/地区(用于 GDPR、cookie 等目的)。
现在看来,API Gateway 后面有一个轻量级的 Cloudfront,它会生成标头“Cloudfront-Viewer-Country”,这将非常简单并实现我们所需要的。即 lambda 接收 Cloudfront-Viewer-Country 并将其发回。
我们尝试过什么
我见过这样的解决方案:使用 AWS Lambda 和 MaxMind 构建地理定位 API,但我很难理解为什么部署 RDS 和维护 MaxMind 数据库对我们来说有意义,如果它已经可以从 Cloudfront-Viewer-Country 获得的话。
我看到过这个问题:Accessing cloudfront-viewer-country header in AWS API Gateway using HTTP Proxy? ,并尝试实现Michael-sqlbot的答案。但我似乎无法访问标题。
我也尝试了这篇文章中的建议,但我似乎也无法访问 Cloudfront-Viewer-Country 的价值。
我们正在做什么(与“我们已经尝试过的事情”结合起来)
要访问并检查标头是否可用,我使用以下 python lambda 函数
import json
def lambda_handler(event, context):
response = {
'status': '200',
'statusDescription': 'Found',
'headers': {
'location' : [ {
'event': json.dumps(event)
} ]
}
}
return response
Run Code Online (Sandbox Code Playgroud)
问题是什么
但事件 json 转储不包含 Cloudfront-Viewer-Country。
我怀疑我做错了什么,但我真的无法弄清楚。任何指针将非常感激。
谢谢
我能够通过设置端点类型=边缘优化来访问Cloudfront-Viewer-Country。
我无法让它与 Endpoint Type = Regional 或 http api gateway 一起使用。
| 归档时间: |
|
| 查看次数: |
3768 次 |
| 最近记录: |