aws-cdk-具有Lambda代理端点的Cognito授权者的ApiGateway

时间:2019-06-17 14:11:13

标签: amazon-web-services aws-api-gateway aws-cdk

我在设置带有由Cognito用户池保护的lambda代理的API网关时遇到问题。 我在打字稿中使用了aws-cdk 0.34.0。

我要执行的设置是具有单个资源的API网关,该资源是lambda函数的代理。此单个端点需要由Cognito用户池保护。

我正在尝试的代码如下。

但是,当我运行它时,出现以下错误...

apiDeployment (Deployment2E842E9E) No integration defined for method (Service: AmazonApiGateway; Status Code: 400; Error Code: BadRequestException; Request ID: 64f4e3bb-9107-11e9-84f1-999724493159)
    new Deployment (/home/circleci/aws/node_modules/@aws-cdk/aws-apigateway/lib/deployment.ts:69:21)

我的打字稿代码在下面

    const backendLambda = new lambda.Function(
      this,
      "CampManager-backendLambda",
      {
        code: new lambda.AssetCode("../server/", AssetPackaging.ZipDirectory),
        handler: "src/index.js",
        timeout: 1000,
        runtime: lambda.Runtime.NodeJS10x
      }
    );

    const backendApi = new api.RestApi(this, "CampManager-backendApi");

    const userPool = new cognito.UserPool(this, "CampManager-userPool", {
      autoVerifiedAttributes: [],
      poolName: "CampManager-userPool",
      signInType: cognito.SignInType.Username,
      usernameAliasAttributes: [
        cognito.UserPoolAttribute.Email,
        cognito.UserPoolAttribute.PhoneNumber
      ]
    });

    const cognitoAuthorizer = new api.CfnAuthorizerV2(
      this,
      "CampManager-authorizer",
      {
        apiId: backendApi.restApiId,
        authorizerType: api.AuthorizationType.Cognito,
        identitySource: ["Authorisation"],
        authorizerUri: userPool.userPoolProviderUrl,
        name: "Cognito"
      }
    );

    backendApi.root.addProxy({
      defaultIntegration: new api.LambdaIntegration(backendLambda),
      defaultMethodOptions: {
        authorizationType: api.AuthorizationType.Cognito,
        authorizerId: cognitoAuthorizer.authorizerId,
        operationName: "Proxy"
      }
    });

    const apiDeployment = new api.Deployment(
      this,
      "CampManager-apiDeployment",
      {
        api: backendApi,
        description: "Camp Manager Deployment"
      }
    );

    const apiStage = new api.Stage(this, "CampManager-apiStage", {
      deployment: apiDeployment
    });

0 个答案:

没有答案