Skip to main content

Serverless

Lambda and API Gateway Explained for Beginners

A beginner-friendly guide to AWS Lambda and Amazon API Gateway: what each service does, how they differ, how they work together, and how to use them for a contact form API.

Published:2026-06-01/Updated:2026-06-01/Author:AWS Cert Roadmap Lab
#aws#lambda#api-gateway#serverless#backend

Conclusion

Lambda is the service that runs your code.

API Gateway is the entry point that receives API requests from outside.

By combining the two, you can build APIs without keeping a server running all the time.

On this site, we use them to handle contact form submissions.

What is Lambda

AWS Lambda is a serverless service that runs your code.

Serverless does not mean there is no server.

It means AWS manages the server for you.

You do not have to manage an OS or keep a server running, as you would with EC2.

What is API Gateway

API Gateway is the entry point that receives HTTP requests.

When the frontend sends a request like POST /contact, API Gateway receives it.

API Gateway then invokes Lambda.

In other words, API Gateway is the front door, and Lambda is the worker.

Differences in Role

ServiceRole
API GatewayExposes the API URL
LambdaRuns the actual processing
DynamoDBStores results and data
CloudWatch LogsLets you check execution logs
IAM RoleGrants Lambda the permissions it needs

Once you understand this split of responsibilities, the overall picture of a serverless API becomes clear.

Using It for a Contact Form

The flow for a contact form is as follows.

StepAction
1The user fills in the form
2The frontend validates the input
3A POST request is sent to API Gateway
4API Gateway invokes Lambda
5Lambda validates the input values
6Lambda writes to DynamoDB
7Lambda returns a success response
8The screen shows a confirmation message

For something like a form submission, you do not need a large backend server.

What Lambda Does

In Lambda, you mainly handle the following.

TaskDescription
JSON parsingRead the request body
Honeypot checkFilter out bot submissions
ValidationCheck name, email, subject, and message
ID generationCreate a contactId
Timestamp generationCreate createdAt
DynamoDB writeSave the inquiry content
Response buildingReturn success or error

The frontend also validates input, but frontend checks alone are not enough.

Because an API can be called directly from outside, Lambda must validate as well.

What API Gateway Does

In API Gateway, you mainly configure the following.

SettingDescription
RouteCreate POST /contact
IntegrationConnect to Lambda
CORSRestrict the allowed frontend Origin
MethodHandle POST and OPTIONS
LogsReview logs as needed

For the MVP, no authentication is used.

If you add login functionality later, you can combine Cognito with a JWT Authorizer.

What is CORS

CORS controls requests made by the browser to a different Origin.

For example, if the frontend is at https://example.com and the API is at the API Gateway URL, the Origins differ.

For this reason, you configure the allowed Origin on the API Gateway side.

In production, do not use *. Allow only the URL of your production frontend.

Required IAM Permissions

For Lambda to write to DynamoDB, it needs permission to access DynamoDB.

However, you should not grant permissions that allow anything.

What the contact form Lambda needs is dynamodb:PutItem on ContactsTable.

If reads or deletes are not required, do not grant them.

This least-privilege mindset matters for both AWS certifications and real-world work.

What to Check in CloudWatch Logs

When Lambda runs, logs appear in CloudWatch Logs.

However, you must not log the full inquiry body or the full email address.

If you log information, keep it to things like the following.

Safe to logReason
contactIdUseful for tracking
requestIdUseful for incident investigation
Validation failure fieldsUseful for identifying causes
DynamoDB write failureUseful for incident investigation

Do not log personal information or secrets.

Points Tested on Exams

For Cloud Practitioner, understand Lambda as serverless compute and API Gateway as a service for publishing APIs.

For SAA, you are stronger if you also understand serverless APIs, authentication, CORS, IAM, logging, and DynamoDB integration.

PerspectiveKey Point
CostLambda is billed by number of executions and execution time
AvailabilityUse managed services
SecurityUse least privilege with IAM Roles
OperationsCheck CloudWatch Logs
ExtensionExtend with Cognito authentication or DynamoDB integration

Summary

With Lambda and API Gateway, you can build APIs without keeping a server running all the time.

For small dynamic processing like a contact form, a serverless setup is a good fit.

The key is to separate the roles: API Gateway as the entry point, Lambda as the worker, and DynamoDB as the storage destination.

Once you can build this setup, it becomes easier to explain your AWS certification knowledge as real implementation experience.

What to learn next

Combine blog posts with the AWS glossary, service comparisons, practice questions, and architecture gallery to connect exam knowledge with implementation.