DEV Community

Michael Wahl for AWS Community Builders

Posted on

Using an AWS API Gateway with AWS Lambda, and AWS DynamoDB

Image description

Basically I was looking for a way to host a simple demo app using a Server-less Architecture. I wanted a simple to update/maintain front and back end, using AWS cloud services.

Basically I wanted a web app/form, where a user could enter a value (price), then have that call an API, which in turn executes a python function that writes two sets of data to a DynamoDB table. The first set of data is value or price the user entered into the form, the second is the same value, but with the last couple digits masked. Clients >> S3 bucket >> AWS API Gateway >> AWS Lambda >> AWS DynamoDB.

As someone who doesn't write code or create software solutions daily, I also leveraged ChatGPT to assist me in terms of my code debugging and with to generate a list of alternatives in terms of my overall design and approach. Originally the Lambda function was developed using javascript, but I ran into many different issues, after some time, I moved on and decided to use python instead.

The code is available in Github below, but you can see how I started using javascript and python:

const AWS = require('aws-sdk');
const dynamoDB = new AWS.DynamoDB.DocumentClient({ region: 'YOUR_AWS_REGION' });

exports.handler = async (event) => {
  const { price } = JSON.parse(event.body);

  // Anonymization logic - Replace this with your own implementation
  const anonymizedPrice = '**********';

  // Save anonymized data to DynamoDB
  const params = {
    TableName: 'YOUR_DYNAMODB_TABLE_NAME',
    Item: {
      originalPrice: price,
      anonymizedPrice: anonymizedPrice,
    },
  };

  try {
    await dynamoDB.put(params).promise();
    return {
      statusCode: 200,
      body: JSON.stringify({ message: 'Price anonymized and stored successfully' }),
    };
  } catch (error) {
    console.error('Error saving anonymized price to DynamoDB:', error);
    return {
      statusCode: 500,
      body: JSON.stringify({ message: 'An error occurred while saving the price' }),
    };
  }
};


Enter fullscreen mode Exit fullscreen mode
import json
import boto3

dynamodb = boto3.resource('dynamodb')
table_name = 'YOUR_DYNAMODB_TABLE_NAME'
table = dynamodb.Table(table_name)

def lambda_handler(event, context):
    body = json.loads(event['body'])
    price = body['price']

    # Anonymization logic - Replace this with your own implementation
    anonymized_price = '**********'

    # Save anonymized data to DynamoDB
    item = {
        'originalPrice': price,
        'anonymizedPrice': anonymized_price
    }

    try:
        table.put_item(Item=item)
        return {
            'statusCode': 200,
            'body': json.dumps({'message': 'Price anonymized and stored successfully'})
        }
    except Exception as e:
        print('Error saving anonymized price to DynamoDB:', str(e))
        return {
            'statusCode': 500,
            'body': json.dumps({'message': 'An error occurred while saving the price'})
        }

Enter fullscreen mode Exit fullscreen mode

Image description

Code sample is available on Github, which can be found here:
https://github.com/mwahl217/anonpricedemo

Top comments (0)