AWS 노트

AWS CDK가 Argument of type 'Function' is not assignable to parameter of type 'IFunction'. 에러를 뿜을 때

Jonchann 2020. 4. 17. 13:38

문제발생

AWS CDK stack안에 적었던 Lambda함수와 이를 반복 실행시키는 규칙 부분에서 Argument of type 'Function' is not assignable to parameter of type 'IFunction'.에러가 나기 시작했다.

에러 메세지는 아래와 같으며 구글에 찾아봐도 '아무 문제 없어야 하는 코드인데 왜 그러지??'라는 대답으로 끝나는 것이 대부분이었다.

Argument of type 'Function' is not assignable to parameter of type 'IFunction'.
  Types of property 'role' are incompatible.
    Type 'import("/YOUR_PATH/node_modules/@aws-cdk/aws-iam/lib/role").IRole | undefined' is not assignable to type 'import("/YOUR_PATH/node_modules/@aws-cdk/aws-iam/lib/role").IRole | undefined'.
      Type 'import("/YOUR_PATH/node_modules/@aws-cdk/aws-iam/lib/role").IRole' is not assignable to type 'import("/YOUR_PATH/node_modules/@aws-cdk/aws-iam/lib/role").IRole'.
        Types of property 'grant' are incompatible.
          Type '(grantee: import("/YOUR_PATH/node_modules/@aws-cdk/aws-iam/lib/principals").IPrincipal, ...actions: string[]) => import/YOUR_PATH/node_modules/@aws-cdk/aws-iam/lib/grant").Grant' is not assignable to type '(grantee: import("/YOUR_PATHnode_modules/@aws-cdk/aws-iam/lib/principals").IPrincipal, ...actions: string[]) => import("/YOUR_PATH/node_modules/@aws-cdk/aws-iam/lib/grant").Grant'.
            Types of parameters 'grantee' and 'grantee' are incompatible.
              Type 'import("/YOUR_PATH/node_modules/@aws-cdk/aws-iam/lib/principals").IPrincipal' is not assignable to type 'import/YOUR_PATH/node_modules/@aws-cdk/aws-iam/lib/principals").IPrincipal'.
                Types of property 'addToPolicy' are incompatible.
                  Type '(statement: import("/YOUR_PATH/node_modules/@aws-cdk/aws-iam/lib/policy-statement").PolicyStatement) => boolean' is not assignable to type '(statement: import("/YOUR_PATH/node_modules/@aws-cdk/aws-iam/lib/policy-statement").PolicyStatement) => boolean'.
                    Types of parameters 'statement' and 'statement' are incompatible.
                      Type 'import("/YOUR_PATH/node_modules/@aws-cdk/aws-iam/lib/policy-statement").PolicyStatement' is not assignable to type 'import("/YOUR_PATH/node_modules/@aws-cdk/aws-iam/lib/policy-statement").PolicyStatement'.
                        Types have separate declarations of a private property 'action'.

57       targets: [new targets.LambdaFunction(func)]

혹시 또 aws-cdk라이브러리 쪽에서 버전이 안 맞아 그럴까 하여 package.json을 봤으나 전혀 이상 없이 최신 버전으로 통일되어있었다.

해결방법: 에러가 나기 직전 상태로 되돌리기

이 에러가 나기 전까지 CDK stack내의 아래 코드는 문제 없었다.

const func = new lambda.Function(this, 'Example-lambda', {
        code: lambda.Code.fromAsset('src'),
        handler: 'lambda_handler.lambda_handler',
        runtime: lambda.Runtime.PYTHON_3_7,
        timeout: Duration.seconds(300),
        layers: [psycopg2Layer, slackerLayer],
        vpc: redshiftVpc,
        securityGroup: ec2.SecurityGroup.fromSecurityGroupId(this, 
          'lambdaSecurityGroup', 'SECURITY_GROUP_ID')
    });

    new event.Rule(this, "Example-rule", {
      schedule: event.Schedule.rate(Duration.minutes(5)),
      targets: [new targets.LambdaFunction(func)]
    });

전체적인 코드 변경도 없었으니 추측해볼 만한 것은 @aws-cdk/aws-ssmaws-sdk를 새로 설치했다는 것이다.

$ npm uninstall @aws-cdk/aws-ssm
$ aws-sdk

파라미터 스토어에서 SecureString타입 값을 가져오려고 설치했던 것인데 아직 원하는대로 실행되지 않아서 새로운 코드를 지우고 원래 상태로 돌려놓았으니 가벼운 마음으로 uninstall했다.
그랬더니 에러가 사라졌다.

둘 중 어느 것이 문제였을까 궁금해서 하나씩 다시 설치해봤는데 두개 다 재설치 된 상태에서 에러는 나지 않았다.. 미스테리구만.

다른 원인의 부상

무사히 컴파일하고 cdk deploy할 때 아래 메세지가 나왔다.

IAM Statement Changes
┌───┬────────────────────┬────────┬────────────────────┬────────────────────┬──────────────────────┐
│   │ Resource           │ Effect │ Action             │ Principal          │ Condition            │
├───┼────────────────────┼────────┼────────────────────┼────────────────────┼──────────────────────┤
│ - │ ${Example │ Allow  │ lambda:InvokeFunct │ Service:events.ama │ "ArnLike": {         │
│   │ -lambda.Arn}       │        │ ion                │ zonaws.com         │   "AWS:SourceArn": " │
│   │                    │        │                    │                    │ ${ReExample │
│   │                    │        │                    │                    │ rule73C1CB5C.Arn}"   │
│   │                    │        │                    │                    │ }                    │
├───┼────────────────────┼────────┼────────────────────┼────────────────────┼──────────────────────┤
│ + │ ${Example │ Allow  │ lambda:InvokeFunct │ Service:events.ama │ "ArnLike": {         │
│   │ -lambda.Arn}       │        │ ion                │ zonaws.com         │   "AWS:SourceArn": " │
│   │                    │        │                    │                    │ ${Example-r │
│   │                    │        │                    │                    │ ule.Arn}"            │
│   │                    │        │                    │                    │ }                    │
└───┴────────────────────┴────────┴────────────────────┴────────────────────┴──────────────────────┘
(NOTE: There may be security-related changes not in this list. See https://github.com/aws/aws-cdk/issues/1299)

두개 라이브러리를 삭제하고 재설치하고 나서 완전히 에러는 사라졌지만 분명 'Example-rule'이라고 이벤트 규칙의 식별자를 적어놓았을텐데 'ReExample-rule'이 되어있길래 뭐지? 하고 고쳤다. (아마 잘못 눌렸거나 새로운 라이브러리를 설치하고 여러가시 실험해본다고 ssm을 사용한 채로 컴파일을 해서 생긴 것일수도 있겠다)
이번 에러가 사실 lambda.FunctionIFunction에 할당할 수 없다면서 나왔던 거였는데 설마 이름이 바뀌어있어서 그랬었나..?