1

这是我的cdk脚本,

import * as cdk from '@aws-cdk/core';
import * as codecommit from "@aws-cdk/aws-codecommit";
import * as amplify from "@aws-cdk/aws-amplify";
import * as cognito from "@aws-cdk/aws-cognito";
import * as iam from "@aws-cdk/aws-iam";

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

    // Part 1  - Connect to Code Commit Repository
    const codecommitRepo = codecommit.Repository.fromRepositoryName(
      this,
      "reactamplifyfullstackcdk",
      "react-amplify-fullstack"
    );

    // Part 2 - Creation of the Amplify Application
    const amplifyApp = new amplify.App(this, "reactamplifyfullstack", {
      sourceCodeProvider: new amplify.CodeCommitSourceCodeProvider({
        repository: codecommitRepo,
      }),
    });
    const devBranch = amplifyApp.addBranch("dev");
    //const qaBranch = amplifyApp.addBranch("qa");
    //const stageBranch = amplifyApp.addBranch("stage");

    // Creation of new Cognito UserPool
    const userPool = new cognito.UserPool(
      this,
      "raf-userpool", 
      {
        userPoolName: "reactamplifyfullstack_userpool",
        selfSignUpEnabled: true,
        autoVerify: {
          email: true
        },
        signInAliases: {
          email: true
        }
      }
      );

      const cfnUserPool = userPool.node.defaultChild as cognito.CfnUserPool;
      cfnUserPool.policies = {
        passwordPolicy: {
          minimumLength: 8,
          requireUppercase: true
        }
      };

    //Creation of new Userpool client
    const userPoolClient = new cognito.UserPoolClient(
      this,
      "reactamplifyfullstack_userpoolClient",
      {
        generateSecret: false,
        userPool: userPool,
        userPoolClientName: "reactamplifyfullstack_userpool_client_web"
      }
    );

    //Creation of new Identity Pool
    const identityPool = new cognito.CfnIdentityPool(
      this,
      "reactamplifyfullstack_identitypool",
      {
        allowUnauthenticatedIdentities: false,
        cognitoIdentityProviders: [{
          clientId: userPoolClient.userPoolClientId,
          providerName: userPool.userPoolProviderName
        }]
      }
    );

    //Creation of new Authenticated Role for Identity Pool
    const authenticatedRole = new iam.Role(
      this,
      "reactamplifyfullstack_auth_role",
      {
        assumedBy: new iam.FederatedPrincipal('cognit-identity.anazonaws.com', {
          "StringEquals": {'cognit-identity.anazonaws.com:aud': identityPool.ref },
          "ForAnyValue:StringLike": { 'cognit-identity.anazonaws.com:amr': "authenticated"},
        }, "sts:AssumeRoleWithWebIdentity"),
      }
    );
    //Add Policy to the IAM role
    authenticatedRole.addToPolicy(new iam.PolicyStatement({
      effect: iam.Effect.ALLOW,
      actions: [
        "mobileanalytics:*",
        "cognito-sync:*",
        "cognito-identity:*"
      ],
      resources: ['*']
    }));

    //Set Default policy
    const defaultPolicy = new cognito.CfnIdentityPoolRoleAttachment(this, "DefaultValid", {
      identityPoolId: identityPool.ref,
      roles: {
        "authenticated": authenticatedRole.roleArn
     }
    });

    //CDK output
    new cdk.CfnOutput(this, 'aws_project_region', { value: 'ap-south-1'});
    new cdk.CfnOutput(this, 'aws_cognito_identity_pool_id', { value: identityPool.ref });
    new cdk.CfnOutput(this, 'aws_cognito_region', { value: 'ap-south-1' });
    new cdk.CfnOutput(this, 'aws_user_pools_id', { value: userPool.userPoolId });
    new cdk.CfnOutput(this, 'aws_user_pools_web_client_id', { value: 
userPoolClient.userPoolClientId });
    new cdk.CfnOutput(this, 'oauth', { value: '{}' });
  }
}

当我尝试构建时,

npm run build

显示以下错误,

***> cdkdeploy@0.1.0 构建

tsc lib/cdkdeploy-stack.ts:57:7 - 错误 TS2345:“this”类型的参数不可分配给“Construct”类型的参数。类型“CdkdeployStack”不可分配给类型“Construct”。属性“节点”的类型不兼容。'import("/home/crypto/react/react-amplify-fullstack/cdkdeploy/node_modules/@aws-cdk/core/lib/construct-compat").ConstructNode' 类型中缺少属性 'addValidation' 但类型中需要'import("/home/crypto/react/react-amplify-fullstack/cdkdeploy/node_modules/@aws-cdk/aws-cognito/node_modules/@aws-cdk/core/lib/construct-compat").ConstructNode'。57 这个,~~~~ node_modules/@aws-cdk/aws-cognito/node_modules/@aws-cdk/core/lib/construct-compat.d.ts:439:5 439 addValidation(validation:constructs.IValidation):空白; ~~~~~~~~~~~~~ 这里声明了'addValidation'。lib/cdkdeploy-stack.ts:91:69 - 错误 TS2345:“this”类型的参数不可分配给“Construct”类型的参数。91 const defaultPolicy = new cognito.CfnIdentityPoolRoleAttachment(this, "DefaultValid", { ~~~~ 发现 2 个错误。npm ERR!code 2 npm ERR!path /home/crypto/react/react-amplify-fullstack/cdkdeploy npm ERR! command failed npm ERR! command sh -c tsc npm ERR! 可以在以下位置找到此运行的完整日志:npm ERR!/home/crypto/.npm/_logs/2021-02-24T08_34_57_019Z-debug.log*** { ~~~~ 发现2个错误。npm 错误!代码 2 npm 错误!路径 /home/crypto/react/react-amplify-fullstack/cdkdeploy npm ERR!命令失败 npm ERR!命令 sh -c tsc npm 错误!可以在以下位置找到此运行的完整日志:npm ERR!/home/crypto/.npm/_logs/2021-02-24T08_34_57_019Z-debug.log*** { ~~~~ 发现2个错误。npm 错误!代码 2 npm 错误!路径 /home/crypto/react/react-amplify-fullstack/cdkdeploy npm ERR!命令失败 npm ERR!命令 sh -c tsc npm 错误!可以在以下位置找到此运行的完整日志:npm ERR!/home/crypto/.npm/_logs/2021-02-24T08_34_57_019Z-debug.log***

请帮我解决这个问题。提前致谢。

4

1 回答 1

0

您的 authrole 字符串有一些拼写错误

const authenticatedRole = new iam.Role(this, `hd-management-auth-role-${customProps.environment}`, {
        assumedBy: new iam.FederatedPrincipal(
            'cognit-identity.amazonaws.com',
            {
                'StringEquals': { 'cognito-identity.amazonaws.com:aud': identityPool.ref },
                'ForAnyValue:StringLike': { 'cognito-identity.amazonaws.com:amr': 'authenticated' },
            },
            'sts:AssumeRoleWithWebIdentity'
        ),
    });
于 2021-06-25T00:41:39.153 回答