Anas El Mhamdi

How to Build a Website from Scratch Under the AWS Free Tier (Part II: Back)

by Anas El Mhamdi

This article continues a series on budget-friendly website deployment using AWS services. It demonstrates how to construct a serverless backend using AWS Lambda and the Serverless framework.

Introduction

The first part covered hosting static websites via S3 and distributing them securely through CloudFront. This installment addresses adding backend functionality without the typical deployment headaches.

Modern AWS services allow developers to run a fully lightweight and functional backend with a few functions in no time using Lambda functions and the Serverless framework.

Prerequisites

  • Active AWS account
  • Existing live website
  • Node.js installation
  • Serverless framework: npm install -g serverless

Initial AWS Setup

  1. Access AWS Console’s IAM (Identity Access Manager)
  2. Create a new user named “serverless-admin” with Programmatic Access enabled
  3. Attach the AdministratorAccess policy
  4. Store the generated Access Key ID and Secret Key securely
  5. Configure credentials: serverless config credentials --provider aws --key YOUR_KEY --secret YOUR_SECRET

Service Architecture

The Lambda-based API performs three functions:

  • Receives a signature from website visitors
  • Validates signature authenticity
  • Returns verification results to the client

Lambda Function Design

Lambda functions receive event and context parameters. For HTTP requests, the relevant event structure includes:

  • body: Request payload
  • httpMethod: HTTP verb (POST, GET, etc.)
  • queryStringParameters: URL parameters
  • isBase64Encoded: Encoding flag

API Gateway Lambda responses must contain statusCode and body JSON keys; omitting these triggers 500 errors.

Serverless Configuration

Deployment relies on a serverless.yml configuration file at the project root, containing all necessary Lambda deployment specifications.

File Structure

lambda_demo
├── serverless.yml
├── decode_api.py
└── encode_decode.py

Deployment Process

Execute serverless deploy to trigger automated Lambda creation and API Gateway endpoint generation.

Upon completion, the terminal displays the new endpoint URL:

https://s8zszn4fu2.execute-api.eu-west-3.amazonaws.com/dev/check

Testing with Postman

  1. Copy the generated endpoint URL
  2. Open Postman and paste the endpoint
  3. Select POST method
  4. Set Body tab to Raw format
  5. Send request

Lambda Logging

Access logs through the Lambda dashboard’s Monitoring tab via CloudWatch Logs. Every Lambda invocation appears logged, facilitating rapid debugging.

Cost Advantages

AWS provides 1 million free Lambda requests monthly, making this approach probably super cheaper than what you considered. This pricing structure suits development and testing without significant expenses.

Achievements

Across both articles, you’ve learned:

  • S3-based website hosting
  • CloudFront-based secure distribution
  • Serverless Lambda backend creation

This foundation enables testing stuff out with minimal financial investment.