Aws-cdk: (AppSync): The "Cannot read property 'length' of undefined" error is displayed when the defaultAuthorization setting.

Created on 20 Jun 2020  路  1Comment  路  Source: aws/aws-cdk

:question: General Issue

I'm trying the AppSync authentication settings.
I want to use Cognito authentication. No additional authentication settings are required.
A similar setup worked in version 1.45.0, but did not work in 1.46.0 .

The Question

Sample code 1 outputs error 1.
The sample code has been modified for the breaking changes in version 1.46.0 It is.
feat(appsync): enhances and completes auth config #7878

  • Sample code 1
import * as cdk from '@aws-cdk/core';
import * as appsync from '@aws-cdk/aws-appsync';
import * as cognito from '@aws-cdk/aws-cognito';

export class WorkStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const userPool = new cognito.UserPool(this, 'userPool', {
      userPoolName: 'userPool',
    });

    const api = new appsync.GraphQLApi(this, 'api', {
      name: 'graphQlApi',
      authorizationConfig: {
        defaultAuthorization: {
          authorizationType: appsync.AuthorizationType.USER_POOL,
          userPoolConfig: {
            userPool,
            defaultAction: appsync.UserPoolDefaultAction.ALLOW
          }
        }
      },
      schemaDefinitionFile: './graphql/schema.graphql'
    });
  }
}
  • Error 1
$ cdk deploy
Cannot read property 'length' of undefined
Subprocess exited with error 1
$

Sample code 2 works fine.

  • Sample code 2
import * as cdk from '@aws-cdk/core';
import * as appsync from '@aws-cdk/aws-appsync';
import * as cognito from '@aws-cdk/aws-cognito';

export class WorkStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const userPool = new cognito.UserPool(this, 'userPool', {
      userPoolName: 'userPool',
    });

    const api = new appsync.GraphQLApi(this, 'api', {
      name: 'graphQlApi',
      authorizationConfig: {
        defaultAuthorization: {
          authorizationType: appsync.AuthorizationType.USER_POOL,
          userPoolConfig: {
            userPool,
            defaultAction: appsync.UserPoolDefaultAction.ALLOW
          }
        },
        additionalAuthorizationModes: []   // Add this configuration
      },
      schemaDefinitionFile: './graphql/schema.graphql'
    });
  }
}

When setting defaultAuthorization, it seems that you need to specify additionalAuthorizationModes, even if you don't need the additional authentication features.
(1.45.0 is additionalAuthorizationModes to There was no need to configure it.

Is this behavior expected?
Sorry if I missed the documentation.
I was confused by the configuration, so I created an Issue.

Environment

  • CDK CLI Version: 1.46.0
  • Module Version: 1.46.0
  • Node.js Version: 12.18.0 (npm 6.14.5)
  • OS: Ubuntu 20.04
  • Language (Version): TypeScript
@aws-cdaws-appsync bug in-progress needs-triage

Most helpful comment

NOTE: In the new version I also had to switch off a couple of eslint warnings on the authorizationType as follows:

    const graphQlApi = new cdkAppsync.GraphQLApi(this, 'GraphQL', {
      ...,
      authorizationConfig: {
        defaultAuthorization: {
          // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
          authorizationType: cdkAppsync.AuthorizationType.USER_POOL,
          userPoolConfig: {
            userPool,
            defaultAction: cdkAppsync.UserPoolDefaultAction.ALLOW
          }
        },
        additionalAuthorizationModes: []     // <--- workaround here
      }
    });

This occurs with TypeScript 3.9.5 and eslint 7.2.0

>All comments

NOTE: In the new version I also had to switch off a couple of eslint warnings on the authorizationType as follows:

    const graphQlApi = new cdkAppsync.GraphQLApi(this, 'GraphQL', {
      ...,
      authorizationConfig: {
        defaultAuthorization: {
          // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
          authorizationType: cdkAppsync.AuthorizationType.USER_POOL,
          userPoolConfig: {
            userPool,
            defaultAction: cdkAppsync.UserPoolDefaultAction.ALLOW
          }
        },
        additionalAuthorizationModes: []     // <--- workaround here
      }
    });

This occurs with TypeScript 3.9.5 and eslint 7.2.0

Was this page helpful?
0 / 5 - 0 ratings

Related issues

artyom-melnikov picture artyom-melnikov  路  3Comments

eladb picture eladb  路  3Comments

schof picture schof  路  3Comments

ababra picture ababra  路  3Comments

pepastach picture pepastach  路  3Comments