lift icon indicating copy to clipboard operation
lift copied to clipboard

Warning: "unsupported string format" when using lift variables in IAM Policies

Open bboure opened this issue 3 years ago • 3 comments

Description

When using lift variables as an IAM policy Resource, ServerlessFramework shows a warning.

unsupported string format

How to Reproduce

myFunction:
  handler: src/myFunction.handler
  iamRoleStatements:
    - Effect: 'Allow'
      Action:
        - sqs:SendMessage
        - sqs:ListQueues
      Resource:
        - ${construct:myQueue.queueArn}

Warning:

Serverless:   at 'functions.myFunction.iamRoleStatements[0].Resource[0]': unsupported string format

Additional Information

This is because when the config goes into the validation, variables have not fully been resolved and still contain the CDK tokens. Tokens don't match the ajv rules

eg:

[
  {
    Effect: 'Allow',
    Action: [ 'sqs:SendMessage', 'sqs:ListQueues' ],
    Resource: [ '${Token[TOKEN.64]}' ]
  }
]

Related/similar issue: https://github.com/serverless/serverless/issues/8488

bboure avatar Jan 06 '22 13:01 bboure

Hi @bboure !

As far as i know, iamRoleStatements at the function level is not supported out of the box by Serverless and is provided by a third party plugin.

It seems this plugin is conflicting with Lift probably because it resolves the variable (which gives a CDK token) but Lift never has a chance to transform the CDK token to a proper Cloudformation reference before the schema validation occurs.

Not sure how to fix this but I guess if you bypass schema validation, then it would output a valid Cloudformation template.

Reference links:

  • plugin hook: https://github.com/functionalone/serverless-iam-roles-per-function/blob/1bb7ca7da33385ac9b38f70aee9dd9c0da052382/src/lib/index.ts#L66
  • lift hooks: https://github.com/getlift/lift/blob/2ec84f8823514d8d7e0adcd8c3168b9529425495/src/plugin.ts#L103-L114

t-richard avatar Jan 06 '22 15:01 t-richard

That's right, it's supported by a third party. I forgot to mention that.

However, the plugin uses "native" validation and type

So, this problem also happens here in the framework provider.iam.role.statements.

I just reproduced it:

provider:
  iam:
    role:
      statements:
        - Effect: 'Allow'
          Action:
            - sqs:deleteMessageBatch
          Resource:
            - ${construct:winningsProcess.queueArn}
Serverless:   at 'provider.iam.role.statements[0].Resource[0]': unsupported string format

I am not sure how to fix that. Right now it's just a warning and cfn compiles just fine when all hooks run.

Maybe a solution (or hack) would be to use custom tokens and token resolvers (not sure if this is possible)

if tokens could be generated to something that match any of the possible values. eg:

  • arn:lift:Token.63:

- or -

Overwrite #/definitions/awsIamPolicyStatements definition, but that seems a bit dangerous.

bboure avatar Jan 06 '22 16:01 bboure

Another use case I just stumbled upon is if we want to setup a Lambda Trigger for the dead letter queue. I was trying something along the following lines on serverless.yml:

functions:
 on-dlq-message-received:
   handler: src/events/on-dlq-message-received.handler
   events:
     - sqs: ${construct:bulk-queue.queueArn}-dlq

I did saw a PR regarding attaching a lambda to DLQ, but it seems it is blocked by other reasons. Appending '-dlq' does not feel right though.

frcouceiro avatar Nov 08 '22 20:11 frcouceiro