Building Your First AWS Serverless Application: A Beginner-Friendly Project with Lambda, DynamoDB and API Gateway

Let's build a beginner friendly serverless application using AWS. In this blog, we'll see how we can do it step by step. First we will create the lambda function using python, you can choose any python version as per your choice, preferably python 3.9+.

import json
import boto3

# Create a boto3 resource for the dynamodb which is created manually specifying the region_name
dynamodb = boto3.resource("dynamodb", region_name='eu-central-1')
# get the table details using the table name, in this case it's Resumes
table=dynamodb.Table('Resumes')

def lambda_handler(event, context):

    # fetch the resume json data using the id of the resume from dynamodb
    response = table.get_item(
        Key={
            'resume_id': '1',
        }
    )
    print(response)

    # check if 'Item' is not present in response
    if 'Item' not in response:
        return {
            'statusCode': 404,
            'body': json.dumps({'message': 'Resume content not present in Database', 'resume_content': ""})
        }

    # if 'Item' is present in response
    return {
        'statusCode': 200,
        'body': json.dumps({'message': 'Resume content fetched succesfully', 'resume_content': response['Item']})
    }

Refer the comments for better understanding of the above python code, it's a pretty simple code.

Now we have to package all the dependencies in a zip and add a layer to the lambda function, In this case there is only one which is boto3. To make a zip of the boto3 library execute the below command.

mkdir pypackages
cd pypackages
pip install boto3 -t .

# Make a zip of the pypackages folder 
# In windows
Compress-Archive -Path * -DestinationPath pypackages.zip

# In Linux
zip -r pypackages.zip .

Add the zip created from the above commands in the lambda function layer.

Now to execute the code we've to insert the json data in dynamodb in AWS. To start with create a table named Resumes, make the primary key as resume_id using which we are fetching the data in the lambda function also*.* Insert the mock json data from this website schema or you can add your own JSON resume.

We are very close to test our lambda function...

The last step is we have to give permission to lambda function to access the dynamodb. For that go to lambda function -> configuration -> Role name.

Click on the link below Role name, you'll be redirected to IAM role where you can attach an policy of AmazonDynamoDBReadOnlyAccess.

Now we're good to test our lambda function, when you test it you should see a proper resume response with statusCode 200.

Now we can attach a API gateway to access it over the internet. For that create a API Gateway with REST API.

After the API Gateway is ready, click on create method, refer the configuration from below image

After the method is created, we've to create a resource

Last step is to deploy the API and now you'll be able to see the deployed API URL. Go ahead and test it. :-)

Hope you enjoyed making this project. Special thanks to Rishab Kumar for giving this problem statement. As next step we can implement a CI/CD pipeline to automate the deployment of the Lambda function whenever code changes are pushed. This topic will be covered in a separate blog post to ensure a more focused and manageable discussion.

Github-Repo: https://github.com/ayushmehta651/resume-api