๐Ÿš€ Going Serverless: Deploying a Restaurant Booking API with Lambda and API Gateway

After building scalable infrastructure using EC2, Terraform, and Secrets Manager, it’s time to go fully serverless. In this post, weโ€™ll migrate your app to use:

  • ๐Ÿง  AWS Lambda โ€” run code without managing servers
  • ๐ŸŒ API Gateway โ€” expose HTTPS endpoints
  • ๐Ÿ” Secrets Manager โ€” keep secrets out of your code
  • ๐Ÿ—ƒ๏ธ DynamoDB or RDS Proxy โ€” handle your database layer

๐Ÿ“ฆ Architecture Overview

Client โ†’ API Gateway โ†’ Lambda Functions โ†’ DB (DynamoDB or RDS via Proxy)
                                   โ†“
                             Secrets Manager

โœ… Benefits of This Serverless Stack

FeatureBenefit
No server to managePay-per-use compute
AutoscalingLambda scales on demand
Secure secretsAWS Secrets Manager or Parameter Store
Low ops overheadNo patching, no EC2, no autoscaling setup
Fast deploymentSingle command using CDK or SAM

๐Ÿ”ง Step-by-Step: Deploy with AWS SAM (Serverless Application Model)

1. ๐Ÿ›  Install SAM CLI

brew install aws/tap/aws-sam-cli
sam --version

2. ๐Ÿ“ Project Structure

restaurant-booking-api/
โ”œโ”€โ”€ template.yaml        # SAM template (Infra as code)
โ”œโ”€โ”€ app/
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ””โ”€โ”€ handler.py       # Lambda function code
โ”œโ”€โ”€ events/              # Example test events
โ””โ”€โ”€ requirements.txt

3. โœ๏ธ Sample SAM Template (template.yaml)

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
  BookingAPI:
    Type: AWS::Serverless::Function
    Properties:
      Handler: app.handler.lambda_handler
      Runtime: python3.11
      MemorySize: 512
      Timeout: 10
      Environment:
        Variables:
          DB_SECRET: /myapp/db-creds
      Events:
        ApiEvent:
          Type: Api
          Properties:
            Path: /bookings
            Method: post

4. ๐Ÿง  handler.py (Lambda Code)

import json, boto3, os

def lambda_handler(event, context):
    secrets = boto3.client('secretsmanager')
    response = secrets.get_secret_value(SecretId=os.environ['DB_SECRET'])
    creds = json.loads(response['SecretString'])

    booking = json.loads(event['body'])
    # Connect to DB, store booking using creds
    return {
        "statusCode": 200,
        "body": json.dumps({"message": "Booking successful"})
    }

5. ๐Ÿš€ Deploy to AWS

sam build
sam deploy --guided

๐Ÿ” Storing DB Credentials in Secrets Manager

aws secretsmanager create-secret \
  --name /myapp/db-creds \
  --secret-string '{"username":"admin","password":"mypassword"}'

๐Ÿ“Š Monitoring with CloudWatch

Each Lambda execution gets its own logs under /aws/lambda/. You can:

  • Set up CloudWatch alarms for failure rates or latency
  • Enable X-Ray tracing for distributed tracing

๐ŸŒ Bonus: Custom Domain with API Gateway + ACM

aws acm request-certificate \
  --domain-name api.mkcloudai.com \
  --validation-method DNS

# Then link ACM to API Gateway via custom domain mappings.

Scroll to Top