首页 > 解决方案 > AWS CDK:在本地测试 api 网关时忽略 Api Key?

问题描述

我目前正在使用aws-cdk开发 API,并且正在使用 aws-sam-cli 和 docker 在本地对其进行测试。我想添加 API Key 的要求来调用 API。

这是我的堆栈中的代码:

const api = new apigw.RestApi(this, "MyAPI", {
  restApiName: "My API",
  description: "BLABLABLA API",
});

const myLambdaIntegration = new apigw.LambdaIntegration(myLambda, {
  proxy: false,
});

// Endpoints of the API
api.root.addResource("test").addMethod("GET", myLambdaIntegration, {
  apiKeyRequired: true,
});

然后我构建这个堆栈并合成它(npm run build ; cdk synth --no-staging myStack > template.yaml 并尝试在本地测试它 sam local start-api

当我在没有任何 API KEY 的情况下请求我的 api 时,API 会返回我的 lambda 的结果。

我希望它会给我一个错误,比如{"message":"Missing Authentication Token"}

有谁知道发生了什么?我怀疑这是因为授权在本地被忽略但没有找到任何相关信息......

提前致谢!

编辑:部署此堆栈后,API 正确地要求我提供令牌。

标签: aws-api-gatewayaws-cdkaws-sam-cli

解决方案


推荐阅读