Auction Service Processing Auctions
Last updated
Last updated
We want to process auctions that have hit their deadline (like an eBay auction).
You can do this by creating a lambda function that triggers on a scheduled event. In your serverless.yml
, you just add this:
Notes:
We're using the rate
expression to set up the scheduling rules. You can also use the cron
syntax. Amazon explains both .
schedule
uses AWS EventBridge behind the scenes.
Serverless gives you the sls log
command to be able to view CloudWatch logs from your terminal.
To view a trailing number of logs for your lambda function:
To view logs from a chunk of the past:
Scheduled events are not always great for development. It's therefore better to comment out the events
part of your lambda function and then trigger the function manually instead.
To trigger a function manually, just type this in the terminal:
Note: -l
just returns the logs for the function.
As of right now, our application can only identify auctions in DynamoDB using the id
primary partition key.
For processAuctions
, we want to close auctions that have a status: 'OPEN'
and an endingAt
that is in the past.
Therefore, to query these auctions in a performant way, we want to make status
into a partition key and endingAt
into a sort key.
serverless.yml
To add a global secondary index, we add this under Properties
in the DynamoDB setup:
The main thing to note in this config is that we set the global secondary index in GlobalSecondaryIndexes
and create a custom IndexName
.
Things to note:
AttributeDefinitions
declares the attributes that will be used as keys
KeySchema
is where we define the primary key
HASH
sets the attribute to a partition key (or hash attribute)
RANGE
sets the attribute to a sort key (where the database stores items in sorted order)
ProjectionType: ALL
projects all attributes into the global secondary index.
Pro tip: Behind the scenes, when you create a global secondary index, DynamoDB creates a virtual copy of your table, making it possible to query efficiently based on the global secondary index.
We now can begin using Query
to efficiently get auctions that we want to close.
To begin, we need to (1) give query permissions in IAM and (2) add the new table used by the global secondary index statusAndEndDate
.
The actual query has a few parts:
Things to note:
KeyConditionExpression
uses DynamoDB's query syntax
The variables :status
and :now
are set inside ExpressionAttributeValues
#status
begins with a #
because status
is a reserved keyword; we resolve #status
to mean status
in ExpressionAttributeNames
Now that we have a list of auctions that we want to close, we can flesh out our processAuctions
handler/lambda.
High-level, here's what processAuctions
is doing:
Gets all auctions to close
Asynchronously closes each auction.
Waits for all closures to resolve.
Returns number of closed auctions.
Notice that the return object isn't an HTTP response. That's because this lambda isn't triggered by API Gateway.
To flesh out closeAuction
some more, it just contains an Update
operation to the DynamoDB table. Specifically, it sets status
to 'CLOSED'
.
The course spends some time explaining how to use @middy/validator
to create schemas that help you error handle missing user inputs for API endpoints.
The documentation explains it well enough.