This SAM application consists of a simple ticket booking system, "Crisis at the Concert". The application is designed to allow users to book tickets for a concert. Let's break it down:
High Level Design
SAM Template
The SAM template, defined in YAML format, describes the AWS resources that make up the application.
AWSTemplateFormatVersion:'2010-09-09'Transform:AWS::Serverless-2016-10-31Description:Crisis at the Concert Ticket Booking SystemResources:BookTicketFunction:Type:AWS::Serverless::FunctionProperties:Handler:lambdas.ticket_booking.handlerRuntime:python3.10Environment:Variables:TABLE_NAME:!RefTicketsTableEvents:BookTicketApi:Type:ApiProperties:Path:/book_ticketMethod:postSumTicketsFunction:Type:AWS::Serverless::FunctionProperties:Handler:lambdas.sum_tickets.handlerRuntime:python3.10Environment:Variables:TABLE_NAME:!RefTicketsTableEvents:SumTicketsApi:Type:ApiProperties:Path:/sum_ticketsMethod:getTicketsTable:Type:AWS::Serverless::SimpleTable
The Resources section of the template describes three resources:
BookTicketFunction: This is a Lambda function that handles the ticket booking logic. It's written in Python, and the function handler is ticket_booking.handler. The function is triggered by an API Gateway event (BookTicketApi) when a POST request is made to the /book_ticket path. The environment variable TABLE_NAME is set to the logical ID of the DynamoDB table resource (TicketsTable), which will be replaced by the actual table name at runtime.
SumTicketsFunction: This is a Lambda function that summarizes the number of booked tickets.
TicketsTable: This is a DynamoDB table where ticket booking records will be stored. The table is created with default properties defined by AWS::Serverless::SimpleTable.
Book Function Code
The Lambda function, ticket_booking.handler, is written in Python. It receives an event object from API Gateway, which contains details of the HTTP request.
The function begins by parsing the body of the HTTP request, expecting to find 'ticket_count' field. If these fields are not present, it returns a 400 status code indicating a bad request.
body=json.loads(event['body'])if'ticket_count'notinbody:return{'statusCode':400,'body':json.dumps({'message':'Invalid request, name and ticket_count are required.'})}
The function also checks that the 'ticket_count' field is a string, returning a 400 status code if not.
The function establishes a connection to DynamoDB and references the table specified by the TABLE_NAME environment variable.
If the request is valid, the function attempts to store the booking information in the DynamoDB table. If this operation fails for any reason, it returns a 500 status code indicating an internal server error.
try:table.put_item(Item={'id':_generate_random_string(5),'name':name,'ticket_count':ticket_count})exceptExceptionase:return{'statusCode':500,'body':json.dumps({'message':'Internal server error.'})}
If the operation is successful, the function returns a 200 status code and a message indicating that the booking was successful.
return{'statusCode':200,'body':json.dumps({'message':f'Successfully booked {ticket_count} tickets for {name}.'})}
In summary, this is a simple serverless ticket booking system, where a user can book tickets by making a POST request to the /book_ticket API endpoint with their name and the number of tickets they want to book. The system will validate the request, book the tickets by adding a record to a DynamoDB table, and return a success message. If anything goes wrong during this process, an appropriate HTTP status code and error message will be returned.
try:response=table.scan()exceptExceptionase:return{'statusCode':500,'body':json.dumps({'message':'Internal server error.'})}ticket_sum=sum(int(item['ticket_count'])foriteminresponse['Items'])
The try block contains a scan operation that retrieves all the items from the TicketsTable. If the scan operation fails for any reason (like the table doesn't exist or there's a problem with permissions), an exception is raised, and the function returns a 500 Internal Server Error response.