Brightspace / serverless-plugin-for-each

Serverless plugin that adds $forEach syntax to reduce code duplication and allow creating dynamic templates
Apache License 2.0
1 stars 5 forks source link

Events forEach problem #32

Closed calebebrim closed 1 year ago

calebebrim commented 1 year ago

image

image

I've tryed also with the commented line and it does not work.

frameworkVersion: '3'

AntonBazhal commented 1 year ago

~What is - msk? Shouldn't it be - kafka?~

NVM, I see now. I think the problem is with the iterator and template indentation. It should be

events:
  - $forEach:
      iterator: ...
      template: ...
calebebrim commented 1 year ago

Just for reference: https://www.serverless.com/framework/docs/providers/aws/guide/serverless.yml/#msk

calebebrim commented 1 year ago

I'm trying this:

msk_event:
  handler: src/handler.msk_event_handler
  environment: ${file(envs/env.${sls:stage}.yml)}
  timeout: 29
  layers:
    - { Ref: PythonRequirementsLambdaLayer }
  package:
    patterns:
      - "!./**"
      - "src/**/*.py"     
  events:
    - $forEach: 
        iterator: $split(${file(./envs/env.${sls:stage, 'dev'}.yml):topicsNames}, ',')}
        template:
          - msk:
            arn: "${file(envs/env.${opt:stage, 'dev'}.yml):KAFKA_CLUSTER_ARN}"
            topic: $forEach.value
            batchSize: 10
            maximumBatchingWindow: 10
            startingPosition: LATEST

Still failing:

image

AntonBazhal commented 1 year ago

Now template indentation seems not correct

msk_event:
  handler: src/handler.msk_event_handler
  environment: ${file(envs/env.${sls:stage}.yml)}
  timeout: 29
  layers:
    - { Ref: PythonRequirementsLambdaLayer }
  package:
    patterns:
      - "!./**"
      - "src/**/*.py"     
  events:
    - $forEach: 
        iterator: $split(${file(./envs/env.${sls:stage, 'dev'}.yml):topicsNames}, ',')}
        template:
          - msk:
              arn: "${file(envs/env.${opt:stage, 'dev'}.yml):KAFKA_CLUSTER_ARN}"
              topic: $forEach.value
              batchSize: 10
              maximumBatchingWindow: 10
              startingPosition: LATEST
calebebrim commented 1 year ago
msk_event:
  handler: src/handler.msk_event_handler
  environment: ${file(envs/env.${sls:stage}.yml)}
  timeout: 29
  layers:
    - { Ref: PythonRequirementsLambdaLayer }
  package:
    patterns:
      - "!./**"
      - "src/**/*.py"     
  events:
    - $forEach: 
        iterator: $split(${file(./envs/env.${sls:stage, 'dev'}.yml):topicNames}, ',')}
        template:
          - msk:
              arn: "${file(envs/env.${opt:stage, 'dev'}.yml):KAFKA_CLUSTER_ARN}"
              topic: $forEach.value
              batchSize: 10
              maximumBatchingWindow: 10
              startingPosition: LATEST

Still:

image

AntonBazhal commented 1 year ago

Make sure iterator is an array. I do not know what the $split is but it's likely that it's doing its thing after serverless-plugin-for-each, so it's not an array from the plugin perspective.

calebebrim commented 1 year ago

image Same thing mate!

calebebrim commented 1 year ago

The $split comes from: https://www.npmjs.com/package/serverless-plugin-utils

AntonBazhal commented 1 year ago

Did you add the plugin to the list in serverless.yml? Does your console output has Found and replaced XXX $forEach matches?

calebebrim commented 1 year ago

Hum... That is it... I'm new to serverless so I thought it isn't necessary.

- serverless-plugin-for-each

I shoud put like this right?

AntonBazhal commented 1 year ago

Yes, just add it to the list of plugins