Project 2: Serverless Website and API With S3, CloudFront, API Gateway, Lambda and DynamoDB

AWS Technical Essentials 55 min min read Updated: Nov 17, 2026 Beginner
Project 2: Serverless Website and API With S3, CloudFront, API Gateway, Lambda and DynamoDB
Beginner Topic 2 of 3

What You Will Build

text Browser ─► CloudFront ─► S3 (index.html) Browser JavaScript ─► API Gateway (HTTP API) ─► Lambda ─► DynamoDB (counter)

Step 1: DynamoDB Table

bash aws dynamodb create-table --table-name Visits --attribute-definitions AttributeName=page,AttributeType=S \ --key-schema AttributeName=page,KeyType=HASH --billing-mode PAY_PER_REQUEST

Step 2: Lambda Function

python import json, boto3 table = boto3.resource("dynamodb").Table("Visits") def handler(event, context): r = table.update_item( Key={"page": "home"}, UpdateExpression="ADD hits :one", ExpressionAttributeValues={":one": 1}, ReturnValues="UPDATED_NEW", ) return { "statusCode": 200, "headers": {"Content-Type": "application/json"}, "body": json.dumps({"visits": int(r["Attributes"]["hits"])}), }
bash zip fn.zip lambda_function.py aws iam create-role --role-name VisitsRole --assume-role-policy-document '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"Service":"lambda.amazonaws.com"},"Action":"sts:AssumeRole"}]}' aws iam attach-role-policy --role-name VisitsRole --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole aws iam put-role-policy --role-name VisitsRole --policy-name ddb --policy-document '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"dynamodb:UpdateItem","Resource":"arn:aws:dynamodb:*:*:table/Visits"}]}' sleep 10 aws lambda create-function --function-name visits --runtime python3.12 --handler lambda_function.handler \ --zip-file fileb://fn.zip --role arn:aws:iam::111111111111:role/VisitsRole

Step 3: HTTP API

bash aws apigatewayv2 create-api --name visits-api --protocol-type HTTP --target arn:aws:lambda:ap-south-1:111111111111:function:visits \ --cors-configuration AllowOrigins='*',AllowMethods='GET' aws lambda add-permission --function-name visits --statement-id apigw --action lambda:InvokeFunction --principal apigateway.amazonaws.com curl https://API-ID.execute-api.ap-south-1.amazonaws.com/ # {"visits": 1}
Tighten CORS Use your real website address instead of * for production.

Step 4: Static Page on S3 and CloudFront

html <!doctype html> <h1>My Serverless Site</h1> <p>Visitors: <span id="n">...</span></p> <script> fetch("https://API-ID.execute-api.ap-south-1.amazonaws.com/") .then(r => r.json()).then(d => document.getElementById("n").textContent = d.visits); </script>
  • Upload index.html to a private S3 bucket
  • Create a CloudFront distribution with the bucket as origin and Origin Access Control, default root object index.html
  • Update the bucket policy to allow only that CloudFront distribution
  • Open the CloudFront domain and refresh to watch the counter grow

What You Practiced

ConceptWhere
Static hosting and CDNS3 and CloudFront
Serverless computeLambda
API layerAPI Gateway HTTP API
NoSQLDynamoDB atomic counter with ADD
Least privilegeRole allows only UpdateItem on one table

Clean Up

bash aws apigatewayv2 delete-api --api-id API-ID aws lambda delete-function --function-name visits aws dynamodb delete-table --table-name Visits aws cloudfront delete-distribution --id DIST-ID --if-match ETAG # disable it first and wait aws s3 rm s3://my-site-bucket --recursive && aws s3 rb s3://my-site-bucket

Challenges

  • Add a custom domain with Route 53 and ACM
  • Add AWS WAF rate limiting
  • Add a CloudWatch alarm for Lambda errors
  • Rebuild the project with SAM or CDK

Conclusion

You built a serverless site where S3 and CloudFront serve the page and API Gateway, Lambda and DynamoDB handle the counter. No servers to patch, and cost near zero at low traffic.

Quick Summary: plan; table; Lambda; API; site; practiced; cleanup.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators