1

Writing my first Lambda funcation to put item with Python

The problem im having - not getting the input from the registration form ( front end hosted on S3 bucket with static webhost enabled ) to the DynamoDB table

the Data will be sent with this funcation to the API hosted on AWS

const BASE_URL = `API-URL`;

function handleForm() {
    const name = document.querySelector('#name').value;
    const email = document.querySelector('#email').value;
    const phone = document.querySelector('#phone').value;
    const data = {
        name,
        email,
        phone
    }
    console.log(data);
    saveDataToAWS(data);
}

async function saveDataToAWS(data) {
    const result = await axios.post(BASE_URL, data);
    return result.data;
}

Im not sure im using AXIOS the right way but lets continue

The Lambda funcation im using now is pretty much this :

import json
import boto3

dynamodb=boto3.resource('dynamodb')
table=dynamodb.Table('register')

def lambda_handler(event, context):
    table.put_item(
        Item={
            'name'=event['name'],
            'email'=event['email'],
            'phone'=event['phone']
        }
    )
    respone={
        'mes':'Good input !'
    }
    return {
        'statusCode': 200,
        'body': respone
    }

Im pretty much 99% new to writting code in AWS so im sure im doing most of it wrong Really looking for you help !

1
  • 2
    start with printing the event to see its structure -- you'll need to probably do some debugging to solve this problem Commented Apr 24, 2021 at 18:27

1 Answer 1

4

The 'event' attribute has a 'body' parameter that will contain the data in your example:

data = json.loads(event["body"])

table.put_item(
        Item={
            'name':data['name'],
            'email':data['email'],
            'phone':data['phone']
        }
    )

Remember to check CloudWatch Logs as well, as it will tell you whether the Lambda was invoked in the first place, and if it failed.

More information on the structure of the event-attribute can be found here: https://aws-lambda-for-python-developers.readthedocs.io/en/latest/02_event_and_context/

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

2 Comments

hey bret,i changed the code but im getting syntax error in line 11 : Line 11\n 'name'=data['name'],\n"
My bad @EyalSolomon, I copy/pasted without checking. I've edited my answer - there should be a : to separate the key/values in the Item-dictionary

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.