skip to Main Content

I’m not too sure how the formatting works with using json and boto3 in the same file. The function works how it should but I don’t know how to get a response from an API without an Internal server error.

I don’t know if it is permissions or the code is wrong.

import boto3
import json

def lambda_handler(event, context):
    client = boto3.resource('dynamodb')
    table = client.Table('Visit_Count')

    input = {'Visits': 1}

    table.put_item(Item=input)
    
    
    return {
        'statusCode': 200
        body: json.dumps("Hello, World!")
    }

2

Answers


  1. Instead of body it should be 'body'. Other than that you should check CloudWatch logs for any further lambda errors.

    Login or Signup to reply.
  2. Anon and Marcin were right, I just tried it and it worked
    Your Lambda role also need to have dynamodb:PutItem

    import boto3
    import json
    
    def lambda_handler(event, context):
        client = boto3.resource('dynamodb')
        table = client.Table('Visit_Count')
    
        input = {'Visits': 1}
    
        table.put_item(Item=input)
        
        return {
            'statusCode': 200,
            'body': json.dumps('Hello from Lambda!')
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search