【问题标题】:Why am I getting "this.combinedGrant is not a function" when synthesizing lambda functions with AWS CDK?使用 AWS CDK 合成 lambda 函数时,为什么会出现“this.combinedGrant 不是函数”?
【发布时间】:2020-10-26 01:44:08
【问题描述】:

我正在使用 AWS CDK 创建我的云堆栈,我发现以下代码正在生成错误“this.combinedGrant is not a function”,但我不能找到对它的任何引用:

    let authFunctions = [
      {
        name: 'user-registration',
        handler: 'index.register',
        code: './handlers/user-registration',
        loginAccess: loginTable.grantFullAccess,
        otpAccess: otpTable.grantWriteData
      },

...

    for (let i in authFunctions) {
      let definition = authFunctions[i];
      let func = new Function(this, `${definition.name}-function`, {
        runtime: Runtime.NODEJS_12_X,
        handler: definition.handler,
        code: Code.asset(definition.code),
        environment: {
          LOGIN_TABLE_NAME: loginTable.tableName,
          OTP_TABLE_NAME: otpTable.tableName,
          MAILGUN_DOMAIN: credentials.mailgun.domain,
          MAILGUN_FROM: credentials.mailgun.from,
          MAILGUN_API_KEY: credentials.mailgun.api_key,
          JWT_SECRET: definition.hasJWTSecret ? "secret-placeholder" : ""
        },
        layers: [authLayer, utilityLayer],
        timeout: Duration.seconds(5)
      });

      definition.loginAccess(func);
      definition.otpAccess(func);

      let api = new RestApi(this, `${definition.name}-api`);
      api.root.addMethod('POST', new LambdaIntegration(func));
    }

【问题讨论】:

    标签: javascript typescript amazon-web-services aws-cdk synth


    【解决方案1】:

    问题在于尝试直接分配loginTable.grantFullAccess 函数。解决方案是用字符串替换,然后使用 switch case 来应用它们:

        let authFunctions = [
          {
            name: 'user-registration',
            handler: 'index.register',
            code: './handlers/user-registration',
            loginAccess: 'full',
            otpAccess: 'write'
          },
    

    ...

          switch (definition.loginAccess) {
            case 'full':
              loginTable.grantFullAccess(func);
              break;
            case 'read':
              loginTable.grantReadData(func);
              break;
          }
    

    【讨论】:

      猜你喜欢
      • 2017-05-28
      • 1970-01-01
      • 2019-09-17
      • 1970-01-01
      • 2016-10-08
      • 2021-04-08
      • 1970-01-01
      • 1970-01-01
      • 2018-08-12
      相关资源
      最近更新 更多