Home > Enterprise >  Testing a HTTP API is working with a boto3 Lambda function
Testing a HTTP API is working with a boto3 Lambda function

Time:07-10

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!")
    }

CodePudding user response:

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

CodePudding user response:

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!')
    }
  • Related