AWS offers various services to help create a serverless architecture, which allows developers to build and run applications and services without managing infrastructure. In this blog, we’ll explore how to create a serverless architecture using AWS services, such as AWS Lambda and Amazon API Gateway. This setup, aligned with the AWS Certified Developer Associate Certification, simplifies serverless application development. The certification covers essential AWS services and features, offering hands-on labs and practical examples to boost your skills. Let’s dive into the deployment steps with a simple scenario.
Scenario – Serverless architecture using AWS Lambda and Amazon API Gateway
As a developer, you will create a serverless application to check if people’s names exist in a voter names database. The database resides in DynamoDB, which is an AWS-managed NoSQL database service. The application will handle API requests through the API Gateway, the API will invoke the Lambda function, query the DynamoDB for the presence of a name, and return a response.
Amazon API Gateway is an AWS service for creating, publishing, maintaining, monitoring, and securing REST, HTTP, and WebSocket APIs. AWS Lambda is a serverless computing service used for running event-driven code in the cloud.
Here’s the list of objects you will create during the deployment.
Table. Objects and their names
The deployment process can be divided into four steps:
Step 1. Create a database in DynamoDB
Step 2. Create and configure Lambda function
Step 2. Create an API in API Gateway
Step 3. Integrate API Gateway with Lambda and deploy the API
Create a DynamoDB table
Follow these steps to create a table with information on voter names and their voter ID.
- Sign in to the AWS Management Console and open the DynamoDB console.
- In the left navigation pane, choose Tables.
- Choose Create table.
- Enter the Table details as follows:
- For Table name, enter Voter_DB.
- For Partition key, enter VoterID.
- Click Create Table. The table is created.
Next, add attributes such as voter names and voter IDs to the table.
- In the DynamoDB console, select the table you’ve created and click Items.
- Click Create item>Edit, add the attributes, and save them. The tables with attributes are created.
Create a Lambda Function
Follow these steps to create a Lambda Function in the Lambda console.
- In the AWS Management Console, navigate to the Lambda service and click Create function.
- Select Author from scratch and enter AddVoterLambda for the function name.
- Select the latest Python run time as the runtime. In this scenario, we’ve used Python 3.12.
- Expand Change default execution role and select Create a new role from AWS policy template and add Role name as LambdaRole.
- For Policy templates, select Simple microservice permissions DynamoD and click Create function.
Next, add code to the Lambda function.
- In the Lambda function code editor, add the following code and click Deploy to create the Lambda function.
import json
import boto3
def lambda_handler(event, context):
# Extract query string parameters
query_params = event.get(‘queryStringParameters’)
# Check if the required parameters are present
if not query_params or ‘voter_id’ not in query_params:
return {
‘statusCode’: 400,
‘body’: json.dumps({‘message’: ‘voter_id query parameter is required’})
}
voter_id = query_params[‘voter_id’]
# Initialize a DynamoDB resource
dynamodb = boto3.resource(‘dynamodb’)
# Reference the specific DynamoDB table
table = dynamodb.Table(‘Voter_DB’)
# Query the table for the specified voter ID
response = table.get_item(
Key={‘VoterID’: voter_id}
)
# Check if the item exists in the response
if ‘Item’ in response:
voter_name = response[‘Item’].get(‘name’) # Retrieve VoterName
if voter_name: # Check if VoterName exists
return {
‘statusCode’: 200,
‘body’: json.dumps({‘message’: f’Voter {voter_name} with ID {voter_id} is a registered voter.’})
}
else:
return {
‘statusCode’: 200,
‘body’: json.dumps({‘message’: f’Voter ID {voter_id} found, but no name is registered.’})
}
else:
return {
‘statusCode’: 404,
‘body’: json.dumps({‘message’: f’No voter found with ID {voter_id}.’})
}
Create REST API in API Gateway
Follow these steps to complete the API-side requirements.
- In the AWS Management Console, navigate to API Gateway service and click Create API.
- Select REST API and then Build.
Next, create the API resource path
- In the API dashboard, click Create Resource.
- Enter /voter as the Resource Path and click Create Resource.
Next, create a GET Method.
- Select the /voter resource and click Create Method.
- Click GET from the dropdown and select integration type as Lambda Function.
- Select the AddVoterLambda under the lambda function drop-down menu and click Save and then OK.
Note: You can integrate API Gateway with Lambda by selecting Lambda Proxy Integration or leaving it unchecked. In a proxy integration, the entire client HTTPS request is sent as-is to the Lambda function. In a non-proxy integration, you configure how the parameters, headers, and body of the client request are passed to the event parameter of the Lambda handler function. In this scenario, we’ve used the non-proxy option.
Next, add URL Query String Parameters and mapping templates.
- From the API console go the GET method>Edit Method Request and add the name of your query string and save the changes.
Next, set up a Mapping Template for Path and Query Parameter Handling.
2 . From the Method Execution pane, click Integration Request>Edit>Mapping templates>Add mapping template and add the following template.
Deploy the API
You’ve reached the final stage of deploying and integrating Lambda with API Gateway. Follow these steps to complete the deployment.
- From the API Gateway console, select the API you’ve created.
- Click Action>Deploy API create a new stage and deploy it.
If the deployment is successful, you will see the Invoke URL for the deployed stage. You can use this URL to test the application a web browser or a tool like Postman.
Now you’ve successfully integrated Lambda with API Gateway for creating a serverless architecture. Congratulations!
Conclusion:
Eliminating the need for traditional server management, AWS can be your first choice for deploying a serverless architecture because of the wide range of services it offers. I hope the deployment process explained in this blog using three AWS services—DynamoDB, AWS Lambda, and API Gateway—has given you a basic understanding of building and managing serverless applications. For a thorough exploration of serverless architecture and other AWS services, sign up for the AWS Certified Developer – Associate course. For additional hands-on experience, you can experiment and implement your serverless scenarios using our AWS hands-on labs and AWS sandboxes.
- How to Create Secure User Authentication with AWS Cognito for Cloud Applications - September 30, 2024
- 2024 Roadmap to AWS Security Specialty Certification Success - August 16, 2024
- Top 25 AWS Full Stack Developer Interview Questions & Answers - August 14, 2024
- AWS Machine Learning Specialty vs Google ML Engineer – Difference - August 9, 2024
- Deploy a serverless architecture using AWS Lambda and Amazon API Gateway - August 9, 2024
- Mastering AWS SDK Integration in Node.js: A Step-by-Step Guide - August 8, 2024
- Is the AWS Certified Security Specialty Certification Right for You? - August 7, 2024
- How Google Cloud Architects Contribute To Business Transformation? - August 6, 2024