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.
Related
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.
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
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!!')
}
I have aws recognition code written in Python, and it run's by Node API, which works fine on Windows system but when I'm deploying it on Linux I'm facing this issue:- botocore.errorfactory.InvalidS3ObjectException: An error occurred (InvalidS3ObjectException) when calling the DetectText operation: Unable to get object metadata from S3. Check object key, region and/or access permissions.
I have given both AmazonRekognitionFullAccess and AmazonS3ReadOnlyAccess access role to I'm user. Still I don't know how to get things going.
Python code:-
bucket = 'image-test'
def image_to_dict(fileName, bucket):
client = boto3.client('rekognition', 'us-east-2')
response = client.detect_text(Image = { 'S3Object': { 'Bucket': bucket,
'Name': fileName } })
return response
Node Code used to run Python script:-
var options = {
mode: 'text',
pythonPath:"/usr/bin/python2.7"
pythonOptions: ['-u'],
scriptPath: "/home/ubuntu/test",
args: [imageURl]
};
PythonShell.run('script.py', options, function (err, results) {
if (err)
throw err;
console.log("Data is: "+results)
I have Python version 2.7 installed on my Ubuntu, pip version 10.0.1.
Thanks for the help.
The reason behind the issue was, when i was passing image name as argument from Node API, the name was getting manipulated due to some Substring logic.So when python script goes with that manipulated name to search in S3 bucket, it used to through above error as that name was not existing in the S3 bucket.