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.
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:
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
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:
hotdog-userpool
hotdog-photos
hotdog-photos-bucket
pink-banter
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 promptssls 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
processMyCron: handler: src/handlers/processMyCron.handler events: - schedule: rate(1 minute)
sls invoke -f processMyCron -l
-l gives us it's logs as well
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
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();