Using an AWS API Gateway with AWS Lambda, and AWS DynamoDB
Tags: serverless, ai, python, javascript
Published: 2023-06-26 21:50:35

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/26nsv3ljrsth8jxv57a8.jpeg)

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' }),
    };
  }
};


```

```
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'})
        }

```
      
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iimpyc8ndm7vnn6arhp3.gif)


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