Skip to content

Bug: sam local invoke still print the response in latin-1 #5229

@guanlinz

Description

@guanlinz

Description:

Passing the event in Chineses to sam local invoke produces results like unsupported character.

This issue only hapeens in STDOUT with sam local invoke, I can get correct and human readable Chineses character via API Gateway

I go through the closed issue and got this: #2494, seems like the issue is fixed in 2021, but in my testing result, it didn't.

Steps to reproduce:

SAM Template:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  helloword-app

  Sample SAM Template for helloword-app

# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
  Function:
    Timeout: 3
    MemorySize: 128

Resources:
  HelloWorldFunction:
    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: hello_world/
      Handler: app.lambda_handler
      Runtime: python3.9
      Architectures:
        - x86_64
      Events:
        HelloWorld:
          Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
          Properties:
            Path: /hello
            Method: get

Outputs:
  # ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
  # Find out more about other implicit resources you can reference within SAM
  # https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
  HelloWorldApi:
    Description: "API Gateway endpoint URL for Prod stage for Hello World function"
    Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
  HelloWorldFunction:
    Description: "Hello World Lambda Function ARN"
    Value: !GetAtt HelloWorldFunction.Arn
  HelloWorldFunctionIamRole:
    Description: "Implicit IAM Role created for Hello World function"
    Value: !GetAtt HelloWorldFunctionRole.Arn

Event.json:

{
  "Records": [
    {
      "body": "你好北京"
    }
  ]
}

Lambda code:

import json

def lambda_handler(event, context):
    response = {  
        "statusCode": 200,  
        "body": json.dumps(event, ensure_ascii=False)
    }  

    return response

Invoking method:
sam local invoke -e events/event.json

Observed result:

STDOUT with sam local invoke -e events/event.json :

[ec2-user@ip-172-31-25-214 helloword-app]$  sam local invoke -e events/event.json 
Invoking app.lambda_handler (python3.9)                                                                                                                                                                                 
Local image is up-to-date                                                                                                                                                                                               
Using local image: public.ecr.aws/lambda/python:3.9-rapid-x86_64.                                                                                                                                                       
                                                                                                                                                                                                                        
Mounting /home/ec2-user/sam-workshop/helloword-app/.aws-sam/build/HelloWorldFunction as /var/task:ro,delegated, inside runtime container                                                                                
START RequestId: 4fc26e4f-175b-4818-9279-d09ba190a184 Version: $LATEST
{"statusCode": 200, "body": "{\"Records\": [{\"body\": \"\u4f60\u597d\u5317\u4eac\"}]}"}END RequestId: 4fc26e4f-175b-4818-9279-d09ba190a184

Expected result:

It should print the meanful Chineses character in STDOUT, like in below

REPORT RequestId: 048587d1-8017-44a9-8887-64b6bdd0c84f  Init Duration: 0.12 ms  Duration: 60.01 ms      Billed Duration: 61 ms  Memory Size: 128 MB     Max Memory Used: 128 MB
[ec2-user@ip-172-31-25-214 helloword-app]$  sam local invoke -e events/event.json 
Invoking app.lambda_handler (python3.9)                                                                                                                                                                                 
Local image is up-to-date                                                                                                                                                                                               
Using local image: public.ecr.aws/lambda/python:3.9-rapid-x86_64.                                                                                                                                                       
                                                                                                                                                                                                                        
Mounting /home/ec2-user/sam-workshop/helloword-app/.aws-sam/build/HelloWorldFunction as /var/task:ro,delegated, inside runtime container                                                                                
START RequestId: 4fc26e4f-175b-4818-9279-d09ba190a184 Version: $LATEST
{"statusCode": 200, "body": "{\"Records\": [{\"body\": \"你好北京\"}"}END RequestId: 4fc26e4f-175b-4818-9279-d09ba190a184
REPORT RequestId: 4fc26e4f-175b-4818-9279-d09ba190a184  Init Duration: 0.04 ms  Duration: 60.12 ms      Billed Duration: 61 ms  Memory Size: 128 MB     Max Memory Used: 128 MB

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

  1. OS: Amazon Linux 2
  2. sam --version: SAM CLI, version 1.84.0
  3. AWS region: cn-north-1
# Paste the output of `sam --info` here

[ec2-user@ip-172-31-25-214 helloword-app]$ sam --info
{
  "version": "1.84.0",
  "system": {
    "python": "3.11.3",
    "os": "Linux-5.10.130-118.517.amzn2.x86_64-x86_64-with-glibc2.26"
  },
  "additional_dependencies": {
    "docker_engine": "20.10.17",
    "aws_cdk": "2.58.1 (build 3d8df57)",
    "terraform": "Not available"
  },
  "available_beta_feature_env_vars": [
    "SAM_CLI_BETA_FEATURES",
    "SAM_CLI_BETA_BUILD_PERFORMANCE",
    "SAM_CLI_BETA_TERRAFORM_SUPPORT",
    "SAM_CLI_BETA_RUST_CARGO_LAMBDA"
  ]
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions