I'm working with a AWS API Gateway that receives a {name} as prints out "Hello {name}" but I'm unable to get this trivial task right.
Any pointers what I could be doing wrong? How the can I get the {name} from the URL?
API Gateway:
https://xxxxxx.execute-api.eu-west-1.amazonaws.com/dev/John
Python AWS Lamba code:
def lambda_handler(event, context):
name = event["requestContext"]["resourcePath"]
response = "Hello " + name
return {
'statusCode': 200,
'body': response
}
The expected output:
{
"statusCode": 200,
"body": "Hello John"
}
Use a proper mapping template on Gateway side. It cannot map whatever you want automatically, you need to configure what you expect there.
Related
Is there a way to set up a rest api using a python lambda function with a get method that uses query parameters by using the amplify CLI or editing the code?
I know this can be done through the AWS Management Console, but was hoping for a more code-oriented solution. Below is the sample lambda I'm trying to use and a simple example of how I would like to get different api responses (length of dummy text string) based on the get method called by the api using something like "curl https://......../myapi?length=4"
import json
def handler(event, context):
print('received event:')
str_len = event['queryStringParameters']['length']
body = {
"message" : "DUMMY TEST"[1:str_len]
}
response = {
"statusCode" : 200,
"body" : json.dumps(body),
"headers" : {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*"
}
}
return response
I understand that API Gateway (AG) passes params into Lambda as JSON payload.
I have setup a POST API with Lambda Proxy in AG. My lambda function takes the following param list :
{
"fullname": "Mr xxxxx",
"clientemail": "xxxxxx#xxx.com",
"clientphone": "0800 088 8888",
"locationtext": "Laxxxxxx Hotel , CA, USA",
"subject": "Gxxxxxrth",
"appointmentblock_min": "60",
"buffer": "120",
"calendar_id": "xxxxx",
"thedate": "2021-03-2xxxxxx",
"thetime": "xxxxx"
}
Testing directly within the Lambda console. Everything works fine. I map all parameters in the code using the lambda EVENT object as follows :
fullname = event['fullname']
clientemail = event['clientemail']
appointmentblock_min = int(event['appointmentblock_min'])
... and so on.
All code works fine.
Adding the API Gateway component on top ... and things don't work.
The specific problem : how can I map the lambda input parameters coming from API Gateway (AG) to lambda ?
I realise that AG is sending JSON into Lambda. How to parse this payload to extract and use the parameters that I need.
I have tried to create a dict containing all the parameters :
Event = {}
Event = json.loads(body)
Event = json.loads(event.body)
No docs explicitly explain how to grab the event object params via AG.
Any help would be appreciated.
PS I am expecting to return data back to AG as follows :
return {
'statusCode': 200,
'headers': {'Content-Type': 'application/json'},
'body': json.dumps(out_message),
'isBase64Encoded': 'false'
}
Thank you for your consideration.
According to TestEvents in Lambda, the input you get on Lambda will be
LambdaTest
So you can load your payload (or body) as
Event = json.loads(event['body'])
I'm having some issues with AWS Lambda for Python 3.8. No matter what code I try running, AWS Lambda keeps returning the same response. I am trying to retrieve a information from a DynamoDB instance with the code below:
import json
import boto3
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('planets')
def lambda_handler(event, context):
response = table.get_item(
Key = {
'id':'mercury'
}
)
print(response)
# TODO implement
return {
'statusCode': 200,
'body': response)
}
I am expecting an output like 'body':{'Item': {'id':'mercury', 'temp':'sizzling hot'}}, or an error even, but I keep getting the response below:
Response:
{
"statusCode": 200,
"body": "\"Hello from Lambda!\""
}
I even change up the code, expecting an error, but I still get the same output.
Usually this is due to one of the following reasons:
You are not deploying your code changes. In the new UI, you have to explicitly Deploy your function using Orange button.
You are invoking old lambda version, rather then your latest version, if you are versioning your functions. You must explicitly choose the correct version to invoke.
So I just started learning AWS Lambda today and tried to create a simple Hello World function in Python. The sample code which AWS generates looks like this:
import json
def lambda_handler(event, context):
# TODO implement
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
Running this code gives the following output:
Response
{
"statusCode": 200,
"body": "\"Hello from Lambda!\""
}
But now when I try to modify this code and run it I get the exact same output as the sample code. I modified it to this:
import json
def lambda_handler(event, context):
# TODO implement
return {
'statusCode': 200,
'body': json.dumps('Hello!')
}
But I still get this as output:
Response
{
"statusCode": 200,
"body": "\"Hello from Lambda!\""
}
Things I've tried:
Deleting the function and creating a new one.
Using a different test event.
Neither worked. I also watched videos on YouTube where people are doing exactly what I was and their code change seemed to work.
Can someone please help me with what I am missing?
Clicking on "Deploy" first and then "Test" worked for me.
This article describes how to create an HTTP API that calls a Lambda under a node runtime.
I just tried doing the same with a Python lambda with the following handler:
def lambda_handler(event, context):
return {
'statusCode': 200,
'body': "\"Hello from Lambda!\""
};
When tested within the Lambda editor, this Lambda returns the exact same as the node Lambda from the article, namely
{
"statusCode": 200,
"body": "\"Hello from Lambda!\""
}
However, the node function works when attached to an HTTP API and navigated to in the browser, while the Python one gives a
{"message":"Internal Server Error"}
What can I do to make it work?
I suggest checking out Cloudwatch logs for details.
It may from your return string. Let's give it a try.
import json
def lambda_handler(event, context):
return {
"statusCode": 200,
"body": json.dumps('Cheers from AWS Lambda!!')
}