typescriptaws-cdkaws-devops

TypeScript - Indirect self-referencing


Im building my first CDK code for AWS and it uses Typescript.

I get the following error:

"IAMManagedPolicy4' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer."

The code looks like this:

const IAMManagedPolicy4 = new iam.CfnManagedPolicy(this, 'IAMManagedPolicy4', {
        managedPolicyName: IAMRole28.ref,
        path: "/" });


const IAMRole28 = new iam.CfnRole(this, 'IAMRole28', {
        managedPolicyArns: [
            "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole",
            IAMManagedPolicy4.ref
        ]});

This is generated by Former2 that exports CDK code formatted in TypeScript. But i guess the error exists because the two elements refer to one another in a loop. How do I avoid the error?


Solution

  • Yes you have a circular dependency. former2 is great but sometimes might have issues.

    I'd suggest to combine the two into a single instantiation. Something like this should work (I assume this is a role for lambda):

    const cfnRole = new iam.CfnRole(this, 'cfnRole', {
        assumeRolePolicyDocument: {
            Version: '2012-10-17',
            Statement: [
              {
                Sid: '',
                Effect: 'Allow',
                Principal: {
                  Federated: 'lambda.amazonaws.com',
                },
                Action: 'sts:AssumeRole',
              },
            ],
          },
        // the properties below are optional
        description: 'description',
        managedPolicyArns: ['arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole'],
        path: '/',
      });