rest - 调用 AWS Lambda 端点
问题描述
我实现了 AWS lambda 方法来了解我可以用它做什么。我现在拥有的:
- AWS lambda 本身
- API 网关
- 亚马逊云观察日志
在 API Gateway 配置中,我看到了下一个选项:
- 端点:https ://xyz.execute-api.us-east-2.amazonaws.com/dummy/test-store
- API密钥:QW123E45RTY6
- 授权:无
在这个 lambda 后面我有 Java 代码,实现com.amazonaws.services.lambda.runtime.RequestStreamHandler和它背后的 REST 控制器,类似这样:
@Path("/tester")
public class TestResource {
private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
private TestRepository testRepository;
public void setTestRepository(TestRepository testRepository) {
this.testRepository = testRepository;
}
@POST
@Path("/{identifier}")
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.WILDCARD)
public Response store(@PathParam("identifier") String identifier, @QueryParam("hashcode") String hashcode) {
try {
this.testRepository.store(identifier, hashcode);
} catch (RuntimeException ex) {
LOG.error("Failed to store pair {}, {}", identifier, hashcode, ex);
throw new InternalServerErrorException(ex);
}
return Response.noContent().build();
}
}
现在我尝试通过 Postman 调用此服务:
选项 1 - 调用我在 API Gateway 中看到的内容:
- 网址:https ://xyz.execute-api.us-east-2.amazonaws.com/dummy/test-store
- 方法:POST
- 标题:
- 密钥:x-api-key
- 值:QW123E45RTY6
结果:404 未找到,请参阅 AWS CloudWatch 中的相应日志。
选项 2 - 调用我的方法:
- 网址:https ://xyz.execute-api.us-east-2.amazonaws.com/dummy/test-store/tester/qwerty123
- 方法:POST
- 标题:
- 密钥:x-api-key
- 值:QW123E45RTY6
结果:403 Forbidden,AWS CloudWatch 中没有日志。
{
"message": "Missing Authentication Token"
}
我完全理解为什么选项 1不起作用,但是选项 2出了什么问题?你能帮我理解我错过了什么吗?
解决方案
当您尝试调用不存在的 URL 时(大多数情况下)会出现此消息。
请确保您正在拨打电话http://api-gateway-name/stage-name/resource-name
此外,请确保您已部署 API。
推荐阅读
- python - python小提琴图规则轴
- xamarin - Shell BackButtonBehavior 命令绑定在 UWP 中不起作用
- reactjs - Jest with Styled Components 错误:解析预期 css 的语法错误:缺少 '}'
- python-3.x - 将日期与 Pandas 中的多列进行比较
- amazon-web-services - 如何将 AWS EC2 从 t2.2xlarge 升级到 t3.2xlarge?
- html - 如何修复在整行上工作的悬停选项
- python - 从 1 到 n 顺序编号的周末日期
- excel - 如何将超过 12 个月的文件夹移动到 VBA 中的另一个存档文件夹?
- javascript - 方法调用不向数组返回值
- google-sheets - 在 Google 表格上构建 M1 Finance 饼形 UI!有什么建议么?