Generate communication between lambdas - AWS SAM - Step Functions

Viewed 14

I am learning to use AWS SAM and it occurred to me that when receiving a payload a lambda has the responsibility of verifying that what is expected is correct. If it is, another lambda will be called to tokenize its data and once that process is finished, call two more lambdas to save the results obtained.

enter image description here

The structure of this project that I am trying to do is as follows:

enter image description here

and my template.yaml file is organized as follows:

AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: >
  lambda-data-dictionary-register

  Sample SAM Template for step-function

Resources:
  StockTradingStateMachine:
    Type: AWS::Serverless::StateMachine # More info about State Machine Resource: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-statemachine.html
    Properties:
      DefinitionUri: statemachine/data-dictionary.asl.json
      DefinitionSubstitutions:
        DataDictionaryFunctionArn: !GetAtt DataDictionaryFunction.Arn
        TokenizeFunctionArn: !GetAtt TokenizeFunction.Arn
        MongoDBFunctionArn: !GetAtt MongoDBFunction.Arn
        RedisFunctionArn: !GetAtt RedisFunction.Arn
      Events:
        HourlyTradingSchedule:
          Type: Schedule # More info about Schedule Event Source: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-property-statemachine-schedule.html
          Properties:
            Description: Schedule to run the stock trading state machine every hour
            Enabled: False # This schedule is disabled by default to avoid incurring charges.
            Schedule: "rate(1 hour)"
      Policies: # Find out more about SAM policy templates: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-policy-templates.html
        - LambdaInvokePolicy:
            FunctionName: !Ref DataDictionaryFunction
        - LambdaInvokePolicy:
            FunctionName: !Ref TokenizeFunction
        - LambdaInvokePolicy:
            FunctionName: !Ref MongoDBFunction
        - LambdaInvokePolicy:
            FunctionName: !Ref RedisFunction

  DataDictionaryFunction:
    Type: AWS::Serverless::Function # More info about Function Resource: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html
    Properties:
      CodeUri: functions/data-dictionary-register/
      Handler: app.lambdaHandler
      Runtime: nodejs16.x
      Architectures:
        - x86_64
      Events:
        Api:
          Type: Api
          Properties:
            Path: /api/data-dictionary-register
            Method: GET
    Metadata:
      BuildMethod: esbuild
      BuildProperties:
        Minify: false
        Target: 'es2020'
        Sourcemap: true
        UseNpmCi: true

  TokenizeFunction:
    Type: AWS::Serverless::Function # More info about Function Resource: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html
    Properties:
      CodeUri: functions/tokenize-data/
      Handler: app.lambdaHandler
      Runtime: nodejs16.x
      Architectures:
        - x86_64
      Events:
        Api:
          Type: Api
          Properties:
            Path: /api/tokenize-data
            Method: GET
    Metadata:
      BuildMethod: esbuild
      BuildProperties:
        Minify: false
        Target: 'es2020'
        Sourcemap: true
        UseNpmCi: true

  MongoDBFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: functions/lambda-mongo-db/
      Handler: app.lambdaHandler
      Runtime: nodejs16.x
      Architectures:
        - x86_64
      Events:
        Api:
          Type: Api
          Properties:
            Path: /api/lambda-mongo-db
            Method: GET
    Metadata:
      BuildMethod: esbuild
      BuildProperties:
        Minify: false
        Target: 'es2020'
        Sourcemap: true
        UseNpmCi: true

  RedisFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: functions/lambda-redis/
      Handler: app.lambdaHandler
      Runtime: nodejs16.x
      Architectures:
        - x86_64
      Events:
        Api:
          Type: Api
          Properties:
            Path: /api/lambda-redis
            Method: GET
    Metadata:
      BuildMethod: esbuild
      BuildProperties:
        Minify: false
        Target: 'es2020'
        Sourcemap: true
        UseNpmCi: true

With all of the above, I want to know how I can make the data-dictionary-register lambda, when processing an incoming data and the result is successful, pass a JSON to the tokenize-data lambda, and this in turn sends it to the other two (lambda-mongo-db and lambda-redis). I want to emphasize that I am working in my local environment and the ideal now is to do everything there. Ultimately, my question is: how do I make the end of one successful process the start of another?

Additionally, I indicate that my test files at the moment are found with this structure in their corresponding app.ts:

enter image description here

At the time I tried with Axios pasting for example to http://127.0.0.1:8080/api/tokenize-data from http://127.0.0.1:8080/api/data-dictionary-register but it always gives an error and reading me I ran into step functions...

0 Answers
Related