Home > Blockchain >  AWS SAM template with multiple MSK topics
AWS SAM template with multiple MSK topics

Time:08-19

I have a Lambda function which tiggers off 3 MSK topics.

I have found I can easily define this dependency in my template.yaml, but only for 1 topic:

Resources:
  MyFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: src/
         ...
         MSKEvent:
          Type: MSK
          Properties:
            MaximumBatchingWindowInSeconds: 1
            StartingPosition: LATEST
            Stream: arn:aws:kafka:.....
            Topics:
              - myTopicName

If I try to add more topics sam build will fail saying my template is invalid. I have tried, and failed, providing my topics in these 2 ways:

Topics:
  - myTopicName
  - myTopicName2
  - myTopicName3
Topics:
  - myTopicName, myTopicName2, myTopicName3

How do I add multiple MSK topics to my SAM template?

CodePudding user response:

Seems like a bug in the sam cli or issue in documentation. But you can just repeat the MSKEvent construct to add the trigger for other topics.

Something like the below code.

  TopicConsumerFunction:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      CodeUri: src/
      Handler: app.lambdaHandler
      Runtime: nodejs12.x
      Events:
        MSKEvent:
          Type: MSK
          Properties:
            StartingPosition: LATEST
            Stream: arn:aws:kafka:ap-south-1:123456789012:cluster/my-cluster/12abcd12-1234-1234-1234-1234abcd1234-1
            Topics:
              - myTopicName              
        MSKEvent2:
          Type: MSK
          Properties:
            StartingPosition: LATEST
            Stream: arn:aws:kafka:ap-south-1:123456789012:cluster/my-cluster/12abcd12-1234-1234-1234-1234abcd1234-1
            Topics:              
              - myTopicName2
      Policies:
      - AWSLambdaMSKExecutionRole
  • Related