3

Riddle me this!

Why lambda?

curl -X POST -H "Content-Type: application/json" -d '{"first_name":"ABCDE", "last_name": "john"}' https://8.execute-api.us-east-1.amazonaws.com/prod/HelloPython

{"message": "Internal server error"}

It works in the lambda test! Granted I am new but still How do I find out why? All I need in cloud metrics is a count of 500 errors. But I want to know why. I am doing a simple hello world.

import json
def respond(err, res=None):
    return {
        'statusCode': '400' if err else '200',
        'body': err.message if err else json.dumps(res),
        'headers': {
            'Content-Type': 'application/json',
        },
    }

def my_handler(event, context):


    message = 'Hello {} {}!'.format(event['first_name'], 
                                    event['last_name'])  
    res = {'message' : message }
    return respond(None, res)

update - Below is the logs:

'first_name': KeyError
Traceback (most recent call last):
File "/var/task/hello_python.py", line 68, in my_handler
message = 'Hello
{}
{}
!'.format(event['first_name'],
KeyError: 'first_name'

I am confused. Is the events a what type of object? If a string do I need do use json.loads(event) to get a dict object?

4
  • 1
    can you please share the cloudwatch logs as well? Commented Oct 28, 2017 at 7:23
  • In the monitoring tab, click "View in CloudWatch Logs" this will give you the Logs for each run Commented Oct 28, 2017 at 13:22
  • i added the logs Commented Oct 28, 2017 at 19:32
  • So first_name is not being passed into the Lambda function. You should try logging the entire event object so you can see what is going on. Perhaps there is an issue with your API Gateway mapping template. Commented Oct 28, 2017 at 19:37

1 Answer 1

2

I typically dont like answering my own question but as a newbie to lambda I hope this helps someone.

1) the event data looks like this:

{
    u 'body': u '{"first_name":"ABCDE", "last_name": "john"}', u 'resource': u '/HelloPython', u 'requestContext': {
        u 'resourceId': u 'kl9',
        u 'apiId': u '81s0q',
        u 'resourcePath': u '/HelloPython',
        u 'httpMethod': u 'POST',
        u 'requestId': u 'e1c72ee6-7-9059-1bd746727831',
        u 'path': u '/prod/HelloPython',
        u 'accountId': u 'cccc',
        u 'identity': {
            u 'apiKey': u '',
            u 'userArn': None,
            u 'cognitoAuthenticationType': None,
            u 'accessKey': None,
            u 'caller': None,
            u 'userAgent': u 'curl/7.54.0',
            u 'user': None,
            u 'cognitoIdentityPoolId': None,
            u 'cognitoIdentityId': None,
            u 'cognitoAuthenticationProvider': None,
            u 'sourceIp': u 'ccccc',
            u 'accountId': None
        },
        u 'stage': u 'prod'
    }, u 'queryStringParameters': None, u 'httpMethod': u 'POST', u 'pathParameters': None, u 'headers': {
        u 'Content-Type': u 'application/json',
        u 'Via': u '1.1 07c960c5d.cloudfront.net (CloudFront)',
        u 'CloudFront-Is-Desktop-Viewer': u 'true',
        u 'CloudFront-Is-SmartTV-Viewer': u 'false',
        u 'CloudFront-Forwarded-Proto': u 'https',
        u 'X-Forwarded-For': u 'xxx, cccc',
        u 'CloudFront-Viewer-Country': u 'US',
        u 'Accept': u '*/*',
        u 'User-Agent': u 'curl/7.54.0',
        u 'X-Amzn-Trace-Id': u 'Root=1-59f4e74a9fecde',
        u 'Host': u '81s0q.execute-api.us-east-1.amazonaws.com',
        u 'X-Forwarded-Proto': u 'https',
        u 'X-Amz-Cf-Id': u 'GXnihIB370EYguOCiGYEA==',
        u 'CloudFront-Is-Tablet-Viewer': u 'false',
        u 'X-Forwarded-Port': u '443',
        u 'CloudFront-Is-Mobile-Viewer': u 'false'
    }, u 'stageVariables': None, u 'path': u '/HelloPython', u 'isBase64Encoded': False
} 

2) data is in body that then has to be loaded from json:

test = json.loads(event['body'])  
message = "Dude %s %s" % (test['first_name'],  test['last_name'])

Curl now works

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.