Joe Gilmore

50 mins read

ServerLess Framework - Creating an AWS Backend Service

Using the ServerLess framework we are going to use AWS Cognito, DynamoDB, API Gateway, Lambda and S3 to create ourselves a backend service that allows photo uploads.

ServerLess Framework - Creating an AWS Backend Service

Introduction

In this blog post we are going to use the ServerLess framework to create a backend service. . Before we start I'm going to assume that you already have knowledge about the ServerLess framework and AWS. You will need to have the sls command installed, and you will also need to have an IAM user with the correct permissions to create the resources.

There are 2 repos for us to use:

  • The backend repo is here, this is a ServerLess framework app - you will need to add your own .env file
  • The front end repo is here, this is a NextJS/React app - you will need to add your own .env.local file

Naming prefixes

A quick note on my naming prefixes convention, whenever I'm deploying services to AWS, or starting a new project I like to think of a naming prefix that I prepend to all my resources. There is nothing that grind smy gears more than seeing a table called "users" or a set of functions called "processors" or "handlers" etc. I can almost guarantee that you'll kick yourself later down the line of you haven't thought of a good naming convention for your services. It applies for both production and development, and helps with cleaning up resources if you're unsure if they are in use or not.

It literally doesn't matter what your prefix naming convention is, as long as it's unique, so in this case I used https://randomwordgenerator.com/fake-word.php to create the prefix of:

hotdog

Creating the backend.

Once you have downloaded the backend Repo, create yourself a .env file and add the following:

AWS_ACCESS_KEY_ID=YOUR_AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY=YOUR_AWS_SECRET_ACCESS_KEY
AWS_REGION=YOUR_AWS_REGION

Now run sls deploy --verbose and we will then have the following resources created:

  • Cognito User Pool = hotdog-userpool
  • DynamoDB table = hotdog-photos
  • S3 bucket = hotdog-photos-bucket
  • Create a new folder on your computer e.g. pink-banter
  • Open a terminal or VS Code and navigate to the folder
  • Run sls create --name pink-banter --template-url https://github.com/codingly-io/sls-base (Joe to add his own Template)
  • npm install and follow the prompts

Joes notes:

sls deploy --verbose - deploys with verbose mode

sls deploy -f MyFunction --verbose - deploys a specific function with verbose mode

https://github.com/middyjs/middy - Middleware engine

npm i @middy/core @middy/http-event-normalizer @middy/http-error-handler @middy/http-json-body-parser @middy/validator
npm i http-errors

Running a cron job (Appears in Event Bridge)

  processMyCron: 
    handler: src/handlers/processMyCron.handler
    events:
      - schedule: rate(1 minute)

Manually triggering a function (bypassing API Gateway)

sls invoke -f processMyCron -l -l gives us it's logs as well

View logs for a given function:

sls logs -f processMyCron - gets all logs sls logs -f processMyCron -t - gets all logs and tails them as they come in sls logs -f processMyCron --startTime 1m - gets all logs from the last minute

Reserved words in your Dynamo DB expressions:

notice the #status:

  const params = {
    TableName: process.env.AUCTIONS_TABLE_NAME,
    IndexName: 'statusAndEndDate',
    KeyConditionExpression: '#status = :status AND endingAt <= :now',
    ExpressionAttributeValues: {
      ':status': 'OPEN',
      ':now': now.toISOString(),
    },
    ExpressionAttributeNames: {
      '#status': 'status',
    },
  };

  const result = await dynamodb.query(params).promise();