aws / aws/aws-sam-cli

Lambda response must be valid json

Open
#7,224 3 comments 0 reactions 0 assignees View on GitHub
blocked/more-info-needed
Dominant language
Python
Stars
6.7k
Forks
1.2k
Avg merge
1d 10h
Merged PRs (30d)
52

Description

hey new to sam
i just used sam init to get strated with the default python template

**SYSTEM AND SAM INFO**
sam --info
/usr/lib/python3/dist-packages/paramiko/transport.py:237: CryptographyDeprecationWarning: Blowfish has been deprecated and will be removed in a future release
"class": algorithms.Blowfish,
{
"version": "1.119.0",
"system": {
"python": "3.10.12",
"os": "Linux-6.5.0-41-generic-x86_64-with-glibc2.35"
},
"additional_dependencies": {
"docker_engine": "24.0.7",
"aws_cdk": "Not available",
"terraform": "1.8.2"
},
"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"
]
}

**DOCKER INFO**
docker --version
Docker version 24.0.7, build 24.0.7-0ubuntu2~22.04.1

**HERE ARE THE CODE** (I have not changed anything default template)

import json

# import requests

import json

# import requests

def lambda_handler(event, context):
"""Sample pure Lambda function

Parameters
----------
event: dict, required
API Gateway Lambda Proxy Input Format

Event doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format

context: object, required
Lambda Context runtime methods and attributes

Context doc: https://docs.aws.amazon.com/lambda/latest/dg/python-context-object.html

Returns
------
API Gateway Lambda Proxy Output Format: dict

Return doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html
"""

# try:
# ip = requests.get("http://checkip.amazonaws.com/")
# except requests.RequestException as e:
# # Send some context about this error to Lambda Logs
# print(e)

# raise e

return {
"statusCode": 200,
"body": json.dumps({
"message": "hello world",
# "location": ip.text.replace("\n", "")
}),
}

**CONSOLE ERROR**
--------------------------
----
COMMAND I RAN
SAM_CLI_CONTAINER_CONNECTION_TIMEOUT=30 sam local start-api --debug --profile xxx
-----
/usr/lib/python3/dist-packages/paramiko/transport.py:237: CryptographyDeprecationWarning: Blowfish has been deprecated and will be removed in a future release
"class": algorithms.Blowfish,
2024-07-05 21:32:26,064 | Config file location: /home/nik/codes/BitSweepProject/paymentMockServer/sam_py/samconfig.toml
2024-07-05 21:32:26,068 | Loading configuration values from [default.['local', 'start-api'].parameters]
(env.command_name.section) in config file at '/home/nik/codes/BitSweepProject/paymentMockServer/sam_py/samconfig.toml'...
2024-07-05 21:32:26,069 | Configuration values successfully loaded.
2024-07-05 21:32:26,070 | Configuration values are: {'stack_name': 'sam_py', 'warm_containers': 'EAGER'}
2024-07-05 21:32:26,075 | Using SAM Template at
/home/nik/codes/BitSweepProject/paymentMockServer/sam_py/.aws-sam/build/template.yaml
2024-07-05 21:32:26,102 | Using config file: samconfig.toml, config environment: default
2024-07-05 21:32:26,103 | Expand command line arguments to:
2024-07-05 21:32:26,104 |
--template_file=/home/nik/codes/BitSweepProject/paymentMockServer/sam_py/.aws-sam/build/template.yaml --host=127.0.0.1
--port=3000 --static_dir=public --layer_cache_basedir=/home/nik/.aws-sam/layers-pkg --warm_containers=EAGER
--container_host=localhost --container_host_interface=127.0.0.1
2024-07-05 21:32:26,175 | local start-api command is called
2024-07-05 21:32:27,427 | No Parameters detected in the template
2024-07-05 21:32:27,444 | Sam customer defined id is more priority than other IDs. Customer defined id for resource
HelloWorldFunction is HelloWorldFunction
2024-07-05 21:32:27,445 | There is no customer defined id or cdk path defined for resource ServerlessRestApi, so we will
use the resource logical id as the resource id
2024-07-05 21:32:27,447 | 0 stacks found in the template
2024-07-05 21:32:27,448 | No Parameters detected in the template
2024-07-05 21:32:27,461 | Sam customer defined id is more priority than other IDs. Customer defined id for resource
HelloWorldFunction is HelloWorldFunction
2024-07-05 21:32:27,462 | There is no customer defined id or cdk path defined for resource ServerlessRestApi, so we will
use the resource logical id as the resource id
2024-07-05 21:32:27,464 | 2 resources found in the stack
2024-07-05 21:32:27,464 | Found Serverless function with name='HelloWorldFunction' and CodeUri='HelloWorldFunction'
2024-07-05 21:32:27,467 | --base-dir is not presented, adjusting uri HelloWorldFunction relative to
/home/nik/codes/BitSweepProject/paymentMockServer/sam_py/.aws-sam/build/template.yaml
2024-07-05 21:32:27,469 | watch resource
/home/nik/codes/BitSweepProject/paymentMockServer/sam_py/.aws-sam/build/template.yaml
2024-07-05 21:32:27,470 | Create Observer for resource
/home/nik/codes/BitSweepProject/paymentMockServer/sam_py/.aws-sam/build/template.yaml with recursive True
2024-07-05 21:32:27,473 | watch resource
/home/nik/codes/BitSweepProject/paymentMockServer/sam_py/.aws-sam/build/template.yaml's parent
/home/nik/codes/BitSweepProject/paymentMockServer/sam_py/.aws-sam/build
2024-07-05 21:32:27,475 | Create Observer for resource
/home/nik/codes/BitSweepProject/paymentMockServer/sam_py/.aws-sam/build with recursive False
2024-07-05 21:32:27,479 | Initializing the lambda functions containers.
2024-07-05 21:32:27,480 | Async execution started
2024-07-05 21:32:27,482 | Invoking function functools.partial(.initialize_function_container at 0x73fd66fbeb90>,
Function(function_id='HelloWorldFunction', name='HelloWorldFunction', functionname='HelloWorldFunction',
runtime='python3.9', memory=128, timeout=30, handler='app.lambda_handler', imageuri=None, packagetype='Zip',
imageconfig=None, codeuri='/home/nik/codes/BitSweepProject/paymentMockServer/sam_py/.aws-sam/build/HelloWorldFunction',
environment=None, rolearn=None, layers=[], events={'HelloWorld': {'Type': 'Api', 'Properties': {'Path': '/hello',
'Method': 'get', 'RestApiId': 'ServerlessRestApi'}}}, metadata={'SamResourceId': 'HelloWorldFunction'}, inlinecode=None,
codesign_config_arn=None, architectures=['x86_64'], function_url_config=None,
function_build_info=, stack_path='', runtime_management_config=None, logging_config=None))
2024-07-05 21:32:27,487 | Waiting for async results
2024-07-05 21:32:27,491 | No environment variables found for function 'HelloWorldFunction'
2024-07-05 21:32:27,492 | Loading AWS credentials from session with profile 'worldleaves'
2024-07-05 21:32:27,504 | Resolving code path.
Cwd=/home/nik/codes/BitSweepProject/paymentMockServer/sam_py/.aws-sam/build,
CodeUri=/home/nik/codes/BitSweepProject/paymentMockServer/sam_py/.aws-sam/build/HelloWorldFunction
2024-07-05 21:32:27,505 | Resolved absolute path to code is
/home/nik/codes/BitSweepProject/paymentMockServer/sam_py/.aws-sam/build/HelloWorldFunction
2024-07-05 21:32:27,507 | Resolving code path.
Cwd=/home/nik/codes/BitSweepProject/paymentMockServer/sam_py/.aws-sam/build,
CodeUri=/home/nik/codes/BitSweepProject/paymentMockServer/sam_py/.aws-sam/build/HelloWorldFunction
2024-07-05 21:32:27,508 | Resolved real code path to
/home/nik/codes/BitSweepProject/paymentMockServer/sam_py/.aws-sam/build/HelloWorldFunction
2024-07-05 21:32:27,519 | watch resource
/home/nik/codes/BitSweepProject/paymentMockServer/sam_py/.aws-sam/build/HelloWorldFunction
2024-07-05 21:32:27,520 | Create Observer for resource
/home/nik/codes/BitSweepProject/paymentMockServer/sam_py/.aws-sam/build/HelloWorldFunction with recursive True
2024-07-05 21:32:27,524 | watch resource
/home/nik/codes/BitSweepProject/paymentMockServer/sam_py/.aws-sam/build/HelloWorldFunction's parent
/home/nik/codes/BitSweepProject/paymentMockServer/sam_py/.aws-sam/build
2024-07-05 21:32:27,526 | Code /home/nik/codes/BitSweepProject/paymentMockServer/sam_py/.aws-sam/build/HelloWorldFunction
is not a zip/jar file
2024-07-05 21:32:33,067 | Local image is up-to-date
2024-07-05 21:32:33,076 | Checking free port on 127.0.0.1:5743
2024-07-05 21:32:33,079 | Using local image: public.ecr.aws/lambda/python:3.9-rapid-x86_64.

2024-07-05 21:32:33,080 | Mounting
/home/nik/codes/BitSweepProject/paymentMockServer/sam_py/.aws-sam/build/HelloWorldFunction as /var/task:ro,delegated,
inside runtime container
2024-07-05 21:32:37,462 | Async execution completed
2024-07-05 21:32:37,463 | Containers Initialization is done.
2024-07-05 21:32:37,464 | Found '1' API Events in Serverless function with name 'HelloWorldFunction'
2024-07-05 21:32:37,465 | Detected Inline Swagger definition
2024-07-05 21:32:37,466 | Parsing Swagger document using 2.0 specification
2024-07-05 21:32:37,467 | Found '0' authorizers in resource 'ServerlessRestApi'
2024-07-05 21:32:37,467 | Lambda function integration not found in Swagger document at path='/hello' method='get'
2024-07-05 21:32:37,468 | Found '0' APIs in resource 'ServerlessRestApi'
2024-07-05 21:32:37,469 | Authorizer not found or disabled, returning early
2024-07-05 21:32:37,470 | Removed duplicates from '0' Explicit APIs and '1' Implicit APIs to produce '1' APIs
2024-07-05 21:32:37,471 | 1 APIs found in the template
2024-07-05 21:32:37,473 | Mounting HelloWorldFunction at http://127.0.0.1:3000/hello [GET]
2024-07-05 21:32:37,476 | You can now browse to the above endpoints to invoke your functions. You do not need to
restart/reload SAM CLI while working on your functions, changes will be reflected instantly/automatically. If you used
sam build before running local commands, you will need to re-run sam build for the changes to be picked up. You only need
to restart SAM CLI if you update your AWS SAM template
2024-07-05 21:32:37,479 | Localhost server is starting up. Multi-threading = True
2024-07-05 21:32:37,480 | Setting SIGTERM interrupt handler
2024-07-05 21:32:37 WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:3000
2024-07-05 21:32:37 Press CTRL+C to quit
2024-07-05 21:32:40,609 | Constructed Event 1.0 to invoke Lambda. Event: {'httpMethod': 'GET', 'body': None, 'resource':
'/hello', 'requestContext': {'resourceId': '123456', 'apiId': '1234567890', 'resourcePath': '/hello', 'httpMethod':
'GET', 'requestId': 'f88f62d1-44e3-45a1-bae2-96cef9914a7f', 'accountId': '123456789012', 'stage': 'Prod', 'identity':
{'apiKey': None, 'userArn': None, 'cognitoAuthenticationType': None, 'caller': None, 'userAgent': 'Custom User Agent
String', 'user': None, 'cognitoIdentityPoolId': None, 'cognitoAuthenticationProvider': None, 'sourceIp': '127.0.0.1',
'accountId': None}, 'extendedRequestId': None, 'path': '/hello', 'protocol': 'HTTP/1.1', 'domainName': '127.0.0.1:3000',
'requestTimeEpoch': 1720195346, 'requestTime': '05/Jul/2024:16:02:26 +0000'}, 'queryStringParameters': None,
'multiValueQueryStringParameters': None, 'headers': {'Host': '127.0.0.1:3000', 'Connection': 'keep-alive',
'Cache-Control': 'max-age=0', 'Sec-Ch-Ua': '"Google Chrome";v="125", "Chromium";v="125", "Not.A/Brand";v="24"',
'Sec-Ch-Ua-Mobile': '?0', 'Sec-Ch-Ua-Platform': '"Linux"', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0
(X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36', 'Accept':
'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exch
ange;v=b3;q=0.7', 'Sec-Fetch-Site': 'none', 'Sec-Fetch-Mode': 'navigate', 'Sec-Fetch-User': '?1', 'Sec-Fetch-Dest':
'document', 'Accept-Encoding': 'gzip, deflate, br, zstd', 'Accept-Language': 'en-US,en;q=0.9,es;q=0.8', 'Cookie':
'csrftoken=O1PbHtujXIXVx6WIeWhS4JjIqHNzfwFQ', 'X-Forwarded-Proto': 'http', 'X-Forwarded-Port': '3000'},
'multiValueHeaders': {'Host': ['127.0.0.1:3000'], 'Connection': ['keep-alive'], 'Cache-Control': ['max-age=0'],
'Sec-Ch-Ua': ['"Google Chrome";v="125", "Chromium";v="125", "Not.A/Brand";v="24"'], 'Sec-Ch-Ua-Mobile': ['?0'],
'Sec-Ch-Ua-Platform': ['"Linux"'], 'Upgrade-Insecure-Requests': ['1'], 'User-Agent': ['Mozilla/5.0 (X11; Linux x86_64)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36'], 'Accept':
['text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exc
hange;v=b3;q=0.7'], 'Sec-Fetch-Site': ['none'], 'Sec-Fetch-Mode': ['navigate'], 'Sec-Fetch-User': ['?1'],
'Sec-Fetch-Dest': ['document'], 'Accept-Encoding': ['gzip, deflate, br, zstd'], 'Accept-Language':
['en-US,en;q=0.9,es;q=0.8'], 'Cookie': ['csrftoken=O1PbHtujXIXVx6WIeWhS4JjIqHNzfwFQ'], 'X-Forwarded-Proto': ['http'],
'X-Forwarded-Port': ['3000']}, 'pathParameters': None, 'stageVariables': None, 'path': '/hello/', 'isBase64Encoded':
False}
2024-07-05 21:32:40,618 | Found one Lambda function with name 'HelloWorldFunction'
2024-07-05 21:32:40,619 | Invoking app.lambda_handler (python3.9)
2024-07-05 21:32:40,620 | No environment variables found for function 'HelloWorldFunction'
2024-07-05 21:32:40,621 | Resolving code path.
Cwd=/home/nik/codes/BitSweepProject/paymentMockServer/sam_py/.aws-sam/build,
CodeUri=/home/nik/codes/BitSweepProject/paymentMockServer/sam_py/.aws-sam/build/HelloWorldFunction
2024-07-05 21:32:40,621 | Resolved absolute path to code is
/home/nik/codes/BitSweepProject/paymentMockServer/sam_py/.aws-sam/build/HelloWorldFunction
2024-07-05 21:32:40,622 | Resolving code path.
Cwd=/home/nik/codes/BitSweepProject/paymentMockServer/sam_py/.aws-sam/build,
CodeUri=/home/nik/codes/BitSweepProject/paymentMockServer/sam_py/.aws-sam/build/HelloWorldFunction
2024-07-05 21:32:40,623 | Resolved real code path to
/home/nik/codes/BitSweepProject/paymentMockServer/sam_py/.aws-sam/build/HelloWorldFunction
2024-07-05 21:32:40,626 | Reuse the created warm container for Lambda function 'HelloWorldFunction'
2024-07-05 21:32:40,629 | Lambda function 'HelloWorldFunction' is already running
2024-07-05 21:33:10,650 | Timed out while attempting to establish a connection to the container. You can increase this
timeout by setting the SAM_CLI_CONTAINER_CONNECTION_TIMEOUT environment variable. The current timeout is 30.0 (seconds).
2024-07-05 21:33:10,652 | Invalid lambda response received: Lambda response must be valid json
2024-07-05 21:33:10,654 | Lambda execution failed ()
2024-07-05 21:33:10 127.0.0.1 - - [05/Jul/2024 21:33:10] "GET /hello/ HTTP/1.1" 502 -
2024-07-05 21:33:10 127.0.0.1 - - [05/Jul/2024 21:33:10] "GET /favicon.ico HTTP/1.1" 403 -

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.