8/25/24, 10:24 AM StackEdit
Lab Session: Create a Simple Serverless API with AWS Lambda and API
Gateway
Objective:
Build and deploy a simple serverless API that returns a “Hello, World!” message using
AWS Lambda and API Gateway.
Steps:
1. Set Up the Lambda Function:
Step 1: Log in to the AWS Management Console and navigate to the
Lambda service.
Step 2: Click on Create Function.
Step 3: Choose Author from scratch.
Function name: HelloWorldFunction
Runtime: Python 3.9 (or your preferred language)
Role: Select “Create a new role with basic Lambda permissions”
Step 4: Click Create Function.
2. Write the Lambda Function Code:
Step 1: Scroll down to the Code source section.
Step 2: Replace the default code with the following:
def lambda_handler(event, context):
return {
'statusCode': 200,
'body': 'Hello, World!'
}
Step 3: Click Deploy to save and deploy the function.
3. Set Up API Gateway:
Step 1: In the AWS Management Console, navigate to the API Gateway
service.
Step 2: Click on Create API and choose REST API.
https://stackedit.io/app# 1/2
8/25/24, 10:24 AM StackEdit
Step 3: Choose New API and set:
API Name: HelloWorldAPI
Endpoint Type: Regional
Step 4: Click Create API.
4. Create a Resource and Method:
Step 1: In the left-hand pane, click on Actions and select Create Resource.
Step 2: Name the resource hello and click Create Resource.
Step 3: With the new resource selected, click Actions and choose Create
Method.
Step 4: Select GET and click the checkmark.
Step 5: In the Integration type dropdown, select Lambda Function.
Step 6: In the Lambda Region dropdown, choose your region.
Step 7: Enter HelloWorldFunction as the Lambda function and click Save.
Step 8: Click OK to grant API Gateway permission to invoke your Lambda
function.
5. Deploy the API:
Step 1: In the left-hand pane, click on Actions and select Deploy API.
Step 2: Create a new stage named prod and click Deploy.
Step 3: After deployment, you’ll receive an Invoke URL. Copy this URL.
6. Test the API:
Step 1: Open a web browser or a tool like Postman.
Step 2: Paste the Invoke URL followed by /hello (e.g., https://{your-
api-id}.execute-api.{region}.amazonaws.com/prod/hello ).
Step 3: Press Enter or Send to trigger the API. You should see a response
that says "Hello, World!" .
Outcome:
You have successfully created and deployed a serverless API that returns a “Hello,
World!” message using AWS Lambda and API Gateway. This lab introduces the basic
flow of setting up a Lambda function, connecting it to an API, and testing it.
https://stackedit.io/app# 2/2