Skip to content

bug: eventbridge input transformer does not work with json object as a result #10287

@vladshoob

Description

@vladshoob

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

Recently added localstack feature to enable input transformation works fine to get a string, but fails on trying to create a JSON input. Localstack logs contain this error message - exception during call chain: replace() argument 2 must be str, not dict.

I believe the issue is with this line - link.

Expected Behavior

JSON as input for target is created

How are you starting LocalStack?

With a docker-compose file

Steps To Reproduce

Given localstack is running from docker compose.yml

version: "3.8"

services:
  localstack:
    image: localstack/localstack:latest
    ports:
      - "127.0.0.1:4566:4566"            # LocalStack Gateway
      - "127.0.0.1:4510-4559:4510-4559"  # external services port range
    volumes:
      - "${LOCALSTACK_VOLUME_DIR:-./temp/localstack}:/var/lib/localstack"
      - "/var/run/docker.sock:/var/run/docker.sock"

And given resources are created from Cloudformation template

Resources:
  LogGroup:
    Type: AWS::Logs::LogGroup
    Properties:
      LogGroupName: /aws/lambda/AwsLambdaMinimalExample

  LambdaFunction:
    Type: AWS::Lambda::Function
    Properties:
      FunctionName: AwsLambdaMinimalExample
      Handler: index.handler
      Runtime: nodejs20.x
      Role: !GetAtt LambdaFunctionRole.Arn
      MemorySize: 256
      Code:
        ZipFile: |
          exports.handler = async (event) => {
            console.log(event);
            return {
              statusCode: 200,
              body: JSON.stringify(event),
            }
          }

  LambdaFunctionRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - lambda.amazonaws.com
            Action:
              - sts:AssumeRole
      Path: "/"
      Policies:
        - PolicyName: AppendToLogsPolicy
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
              - Effect: Allow
                Action:
                  - logs:CreateLogGroup
                  - logs:CreateLogStream
                  - logs:PutLogEvents
                Resource: "*"

  EventRule:
    Type: AWS::Events::Rule
    Properties:
      Name: input-transformer-rule
      EventPattern:
        detail-type:
          - "ExampleEvent"
      Targets:
        - Id: transform-and-invoke-lambda
          Arn: !GetAtt LambdaFunction.Arn
          InputTransformer:
            InputPathsMap:
              "detail" : "$.detail"
            InputTemplate: |
              {
                "method" : "POST",
                "path" : "my/custom/path",
                "body": <detail>
              }


  PermissionForEventToInvokeLambda:
    Type: AWS::Lambda::Permission
    Properties:
      FunctionName: !GetAtt LambdaFunction.Arn
      Action: "lambda:InvokeFunction"
      Principal: "events.amazonaws.com"
      SourceArn: !GetAtt EventRule.Arn

And deployed using aws cloudformation create-stack --stack-name minimal-example --template-body file://cloudformation.yaml --endpoint-url http://localhost:4566 --region us-east-1 command.

On dispatching eventbridge events - aws events put-events --endpoint-url http://localhost:4566 --region us-east-1 --entries file://event.json with event.json containing

[
  {
    "Source": "acme.com",
    "Detail": "{ \"key1\": \"value1\", \"key2\": \"value2\" }",
    "DetailType": "ExampleEvent"
  }
]

Validation is done using lambda function with simple log and monitored by watching tail of a log group via
aws logs tail /aws/lambda/AwsLambdaMinimalExample --endpoint-url http://localhost:4566 --region us-east-1 --follow command.

Expected result is JSON object presented in logs

{
    "method" : "POST",
    "path" : "my/custom/path",
    "body": {
      "key1": "value1",
      "key2": "value2"
    }
}

Actual behavior - 500 error in localstack log.

Environment

- OS: Ubuntu 20.04
- LocalStack: latest

Anything else?

Provided above is working on actual AWS platform.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions