简体   繁体   中英

Using python for put_item in dynamodb table with Lambda function

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 !

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM