{"id":97036,"date":"2024-08-09T09:42:39","date_gmt":"2024-08-09T04:12:39","guid":{"rendered":"https:\/\/www.whizlabs.com\/blog\/?p=97036"},"modified":"2024-08-09T09:42:39","modified_gmt":"2024-08-09T04:12:39","slug":"deploy-serverless-aws-lambda-api-gateway","status":"publish","type":"post","link":"https:\/\/www.whizlabs.com\/blog\/deploy-serverless-aws-lambda-api-gateway\/","title":{"rendered":"Deploy a serverless architecture using AWS Lambda and Amazon API Gateway"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">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&#8217;ll explore how to create a serverless architecture using AWS services, such as AWS Lambda and Amazon API Gateway. This setup, aligned with the <\/span><a href=\"https:\/\/www.whizlabs.com\/aws-developer-associate\/\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">AWS Certified Developer Associate Certification<\/span><\/a><span style=\"font-weight: 400;\">, simplifies serverless application development. The certification covers essential AWS services and features, offering hands-on labs and practical examples to boost your skills. Let&#8217;s dive into the deployment steps with a simple scenario.<\/span><\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_76 ez-toc-wrap-left counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #ea7e02;color:#ea7e02\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #ea7e02;color:#ea7e02\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.whizlabs.com\/blog\/deploy-serverless-aws-lambda-api-gateway\/#Scenario_%E2%80%93_Serverless_architecture_using_AWS_Lambda_and_Amazon_API_Gateway\" >Scenario &#8211; Serverless architecture using AWS Lambda and Amazon API Gateway<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.whizlabs.com\/blog\/deploy-serverless-aws-lambda-api-gateway\/#Create_a_DynamoDB_table\" >Create a DynamoDB table<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.whizlabs.com\/blog\/deploy-serverless-aws-lambda-api-gateway\/#Create_a_Lambda_Function\" >Create a Lambda Function<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.whizlabs.com\/blog\/deploy-serverless-aws-lambda-api-gateway\/#Create_REST_API_in_API_Gateway\" >Create REST API in API Gateway<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.whizlabs.com\/blog\/deploy-serverless-aws-lambda-api-gateway\/#Deploy_the_API\" >Deploy the API<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.whizlabs.com\/blog\/deploy-serverless-aws-lambda-api-gateway\/#Conclusion\" >Conclusion: \u00a0<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Scenario_%E2%80%93_Serverless_architecture_using_AWS_Lambda_and_Amazon_API_Gateway\"><\/span><span style=\"color: #000000;\"><a style=\"color: #000000;\" href=\"https:\/\/www.whizlabs.com\/blog\/aws-lambda-documentation\/\"><span style=\"font-weight: 400;\">S<\/span><span style=\"font-weight: 400;\">cenario &#8211; Serverless architecture using AWS Lambda and Amazon API Gateway<\/span><\/a><\/span><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">As a developer, you will create a serverless application to check if people\u2019s names exist in a voter names database. The database resides in <\/span><a href=\"https:\/\/docs.aws.amazon.com\/dynamodb\/\" target=\"_blank\" rel=\"nofollow noopener\"><span style=\"font-weight: 400;\">DynamoDB<\/span><\/a><span style=\"font-weight: 400;\">, 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.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Amazon API Gateway is an AWS service for creating, publishing, maintaining, monitoring, and securing REST, HTTP, and WebSocket APIs. <\/span><a href=\"https:\/\/www.whizlabs.com\/blog\/aws-lambda-documentation\/\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">AWS Lambda <\/span><\/a><span style=\"font-weight: 400;\">is a serverless computing service used for running event-driven code in the cloud.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Here\u2019s the list of objects you will create during the deployment.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Table. Objects and their names<\/span><\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-97092 size-large\" src=\"https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/Serverless-AWS-Lambda-1024x472.webp\" alt=\"serverless AWS Lambda setup object names\" width=\"1024\" height=\"472\" srcset=\"https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/Serverless-AWS-Lambda-1024x472.webp 1024w, https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/Serverless-AWS-Lambda-300x138.webp 300w, https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/Serverless-AWS-Lambda-768x354.webp 768w, https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/Serverless-AWS-Lambda-1536x708.webp 1536w, https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/Serverless-AWS-Lambda-2048x944.webp 2048w, https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/Serverless-AWS-Lambda-150x69.webp 150w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">The deployment process can be divided into four steps:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Step 1. Create a database in DynamoDB<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Step 2.\u00a0 Create and configure Lambda function\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Step 2. Create an API in API Gateway<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Step 3. Integrate API Gateway with Lambda and deploy the API<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Create_a_DynamoDB_table\"><\/span><span style=\"font-weight: 400;\">Create a DynamoDB table<\/span><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Follow these steps to create a table with information on voter names and their voter ID.\u00a0<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Sign in to the AWS Management Console and open the DynamoDB console.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">In the left navigation pane, choose\u00a0<\/span><b>Tables<\/b><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Choose\u00a0<\/span><b>Create table<\/b><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Enter the\u00a0<\/span><b>Table details\u00a0<\/b><span style=\"font-weight: 400;\">as follows:<\/span>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">For\u00a0<\/span><b>Table name<\/b><span style=\"font-weight: 400;\">, enter\u00a0<\/span><span style=\"font-weight: 400;\">Voter_DB<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">For\u00a0<\/span><b>Partition key<\/b><span style=\"font-weight: 400;\">, enter\u00a0<\/span><span style=\"font-weight: 400;\">VoterID<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ol>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Click <\/span><b>Create Table<\/b><span style=\"font-weight: 400;\">. The table is created.<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">Next, add attributes such as voter names and voter IDs to the table.<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">In the DynamoDB console, select the table you\u2019ve created and click <\/span><b>Items<\/b><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Click <\/span><b>Create item<\/b><span style=\"font-weight: 400;\">&gt;<\/span><b>Edit<\/b><span style=\"font-weight: 400;\">, add the attributes, and save them. The tables with attributes are created.<\/span><\/li>\n<\/ol>\n<p><img decoding=\"async\" class=\"alignnone wp-image-97071 size-large\" src=\"https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/1_voter_db-1024x855.webp\" alt=\" DynamoDB table \" width=\"1024\" height=\"855\" srcset=\"https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/1_voter_db-1024x855.webp 1024w, https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/1_voter_db-300x250.webp 300w, https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/1_voter_db-768x641.webp 768w, https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/1_voter_db-1536x1282.webp 1536w, https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/1_voter_db-150x125.webp 150w, https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/1_voter_db.webp 1878w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Create_a_Lambda_Function\"><\/span><span style=\"font-weight: 400;\">Create a Lambda Function<\/span><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Follow these steps to create a Lambda Function in the Lambda console.<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">In the AWS Management Console, navigate to the Lambda service and click <\/span><b>Create function<\/b><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Select <\/span><b>Author from scratch and <\/b><span style=\"font-weight: 400;\">enter <\/span><b>AddVoterLambda<\/b><span style=\"font-weight: 400;\"> for the function name.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Select the latest Python run time as the runtime. In this scenario, we\u2019ve used Python 3.12.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Expand <\/span><b>Change default execution role<\/b><span style=\"font-weight: 400;\"> and select <\/span><b>Create a new role from AWS policy template <\/b><span style=\"font-weight: 400;\">and add<\/span><b> Role name<\/b><span style=\"font-weight: 400;\"> as <\/span><b>LambdaRole<\/b><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">For Policy templates, select <\/span><b>Simple microservice permissions DynamoD <\/b><span style=\"font-weight: 400;\">and click <\/span><b>Create function<\/b><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">Next, add code to the Lambda function.\u00a0<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">In the Lambda function code editor, add the following code and click <\/span><b>Deploy<\/b><span style=\"font-weight: 400;\"> to create the Lambda function.<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">import json<\/span><\/p>\n<p><span style=\"font-weight: 400;\">import boto3<\/span><\/p>\n<p><span style=\"font-weight: 400;\">def lambda_handler(event, context):<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0# Extract query string parameters<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0query_params = event.get(&#8216;queryStringParameters&#8217;)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0# Check if the required parameters are present<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0if not query_params or &#8216;voter_id&#8217; not in query_params:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8216;statusCode&#8217;: 400,<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8216;body&#8217;: json.dumps({&#8216;message&#8217;: &#8216;voter_id query parameter is required&#8217;})<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0voter_id = query_params[&#8216;voter_id&#8217;]<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0# Initialize a DynamoDB resource<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0dynamodb = boto3.resource(&#8216;dynamodb&#8217;)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0# Reference the specific DynamoDB table<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0table = dynamodb.Table(&#8216;Voter_DB&#8217;)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0# Query the table for the specified voter ID<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0response = table.get_item(<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Key={&#8216;VoterID&#8217;: voter_id}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">\u00a0 # Check if the item exists in the response<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0if &#8216;Item&#8217; in response:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0voter_name = response[&#8216;Item&#8217;].get(&#8216;name&#8217;)\u00a0 # Retrieve VoterName<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if voter_name:\u00a0 # Check if VoterName exists<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8216;statusCode&#8217;: 200,<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8216;body&#8217;: json.dumps({&#8216;message&#8217;: f&#8217;Voter {voter_name} with ID {voter_id} is a registered voter.&#8217;})<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0else:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8216;statusCode&#8217;: 200,<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8216;body&#8217;: json.dumps({&#8216;message&#8217;: f&#8217;Voter ID {voter_id} found, but no name is registered.&#8217;})<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0else:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8216;statusCode&#8217;: 404,<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8216;body&#8217;: json.dumps({&#8216;message&#8217;: f&#8217;No voter found with ID {voter_id}.&#8217;})<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-97073 size-large\" src=\"https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/2_lambda_code-1024x590.webp\" alt=\"DynamoDB resource \" width=\"1024\" height=\"590\" srcset=\"https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/2_lambda_code-1024x590.webp 1024w, https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/2_lambda_code-300x173.webp 300w, https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/2_lambda_code-768x443.webp 768w, https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/2_lambda_code-150x86.webp 150w, https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/2_lambda_code.webp 1034w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Create_REST_API_in_API_Gateway\"><\/span><span style=\"font-weight: 400;\">Create REST API in API Gateway<\/span><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Follow these steps to complete the API-side requirements.<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">In the AWS Management Console, navigate to API Gateway service and click <\/span><b>Create API<\/b><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Select <\/span><b>REST API<\/b><span style=\"font-weight: 400;\"> and then <\/span><b>Build<\/b><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">Next, create the API resource path<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">In the API dashboard, click <\/span><b>Create Resource<\/b><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Enter \/voter as the Resource Path and click <\/span><b>Create Resource<\/b><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">Next, create a GET Method.<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Select the <\/span><b>\/voter<\/b><span style=\"font-weight: 400;\"> resource and click <\/span><b>Create Method<\/b><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Click GET from the dropdown and select integration type as <\/span><b>Lambda Function<\/b><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Select the AddVoterLambda under the lambda function drop-down menu and click <\/span><b>Save<\/b><span style=\"font-weight: 400;\"> and then <\/span><b>OK<\/b><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">Note: <\/span><span style=\"font-weight: 400;\">You can integrate API Gateway with Lambda by selecting <\/span><b>Lambda Proxy Integration<\/b><span style=\"font-weight: 400;\"> 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\u2019ve used the non-proxy option.<\/span><\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-97074 size-large\" src=\"https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/3_method_details-1024x959.webp\" alt=\"Create REST API in API Gateway \" width=\"1024\" height=\"959\" srcset=\"https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/3_method_details-1024x959.webp 1024w, https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/3_method_details-300x281.webp 300w, https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/3_method_details-768x720.webp 768w, https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/3_method_details-1536x1439.webp 1536w, https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/3_method_details-150x141.webp 150w, https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/3_method_details.webp 1586w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">Next, add URL Query String Parameters and mapping templates.\u00a0<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">From the API console go the GET method&gt;<\/span><b>Edit Method Request<\/b><span style=\"font-weight: 400;\"> and add the name of your query string and save the changes.<\/span><\/li>\n<\/ol>\n<p><img decoding=\"async\" class=\"alignnone wp-image-97075 size-large\" src=\"https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/4_string_parameters-1024x849.webp\" alt=\"API Gateway\" width=\"1024\" height=\"849\" srcset=\"https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/4_string_parameters-1024x849.webp 1024w, https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/4_string_parameters-300x249.webp 300w, https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/4_string_parameters-768x637.webp 768w, https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/4_string_parameters-1536x1274.webp 1536w, https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/4_string_parameters-150x124.webp 150w, https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/4_string_parameters.webp 1700w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">Next, set up a Mapping Template for Path and Query Parameter Handling.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0 \u00a02 . From the Method Execution pane, click <\/span><b>Integration Request<\/b><span style=\"font-weight: 400;\">&gt;<\/span><b>Edit<\/b><span style=\"font-weight: 400;\">&gt;<\/span><b>Mapping templates<\/b><span style=\"font-weight: 400;\">&gt;<\/span><b>Add mapping template<\/b><span style=\"font-weight: 400;\"> and add the following template.<\/span><\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-97076 \" src=\"https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/5_mapping_templates.webp\" alt=\"JSON mapping template in AWS API Gateway \" width=\"823\" height=\"453\" srcset=\"https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/5_mapping_templates.webp 1006w, https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/5_mapping_templates-300x165.webp 300w, https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/5_mapping_templates-768x423.webp 768w, https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/5_mapping_templates-150x83.webp 150w\" sizes=\"(max-width: 823px) 100vw, 823px\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Deploy_the_API\"><\/span><span style=\"font-weight: 400;\">Deploy the API<\/span><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">You\u2019ve reached the final stage of deploying and integrating Lambda with API Gateway. Follow these steps to complete the deployment.<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">From the API Gateway console, select the API you\u2019ve created.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Click <\/span><b>Action<\/b><span style=\"font-weight: 400;\">&gt;<\/span><b>Deploy API<\/b><span style=\"font-weight: 400;\"> create a new stage and deploy it.<\/span><\/li>\n<\/ol>\n<p><img decoding=\"async\" class=\"alignnone wp-image-97077 \" src=\"https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/6_deploy_api.webp\" alt=\"Deploy API settings in AWS API Gateway\" width=\"742\" height=\"720\" srcset=\"https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/6_deploy_api.webp 895w, https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/6_deploy_api-300x291.webp 300w, https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/6_deploy_api-768x746.webp 768w, https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/6_deploy_api-150x146.webp 150w\" sizes=\"(max-width: 742px) 100vw, 742px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">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.<\/span><\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-97078 \" src=\"https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/7_test_application-1.webp\" alt=\"Postman GET request to an AWS API Gateway \" width=\"772\" height=\"506\" srcset=\"https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/7_test_application-1.webp 829w, https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/7_test_application-1-300x197.webp 300w, https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/7_test_application-1-768x504.webp 768w, https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/7_test_application-1-150x98.webp 150w\" sizes=\"(max-width: 772px) 100vw, 772px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">Now you\u2019ve successfully integrated Lambda with API Gateway for creating a serverless architecture. Congratulations!<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span><span style=\"font-weight: 400;\">Conclusion: <\/span><span style=\"font-weight: 400;\">\u00a0<\/span><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">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\u2014DynamoDB, <\/span><a href=\"https:\/\/www.whizlabs.com\/blog\/aws-lambda-and-api-gateway-training-course\/?nowprocket=1\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">AWS Lambda, and API Gateway<\/span><\/a><span style=\"font-weight: 400;\">\u2014has 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 <\/span><span style=\"font-weight: 400;\">\u00a0AWS Certified Developer &#8211; Associate <\/span><span style=\"font-weight: 400;\">course. For additional hands-on experience, you can experiment and implement your serverless scenarios using our <\/span><a href=\"https:\/\/www.whizlabs.com\/labs\/library\/\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">AWS hands-on labs<\/span><\/a><span style=\"font-weight: 400;\"> and <\/span><a href=\"https:\/\/www.whizlabs.com\/labs\/sandbox\/aws\/aws-sandbox\/\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">AWS sandboxes<\/span><\/a><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;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&#8217;s dive into the deployment steps with a simple scenario. Scenario &#8211; Serverless architecture using AWS Lambda and Amazon API Gateway As a [&hellip;]<\/p>\n","protected":false},"author":408,"featured_media":97091,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"default","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[10],"tags":[],"class_list":["post-97036","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cloud-computing-certifications"],"uagb_featured_image_src":{"full":["https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/AWS-Lambda-1.png",6400,3200,false],"thumbnail":["https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/AWS-Lambda-1-150x150.png",150,150,true],"medium":["https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/AWS-Lambda-1-300x150.png",300,150,true],"medium_large":["https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/AWS-Lambda-1-768x384.png",768,384,true],"large":["https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/AWS-Lambda-1-1024x512.png",1024,512,true],"1536x1536":["https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/AWS-Lambda-1-1536x768.png",1536,768,true],"2048x2048":["https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/AWS-Lambda-1-2048x1024.png",2048,1024,true],"profile_24":["https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/AWS-Lambda-1.png",24,12,false],"profile_48":["https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/AWS-Lambda-1.png",48,24,false],"profile_96":["https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/AWS-Lambda-1.png",96,48,false],"profile_150":["https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/AWS-Lambda-1.png",150,75,false],"profile_300":["https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/AWS-Lambda-1.png",300,150,false],"tptn_thumbnail":["https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/AWS-Lambda-1-250x250.png",250,250,true],"web-stories-poster-portrait":["https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/AWS-Lambda-1-640x853.png",640,853,true],"web-stories-publisher-logo":["https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/AWS-Lambda-1-96x96.png",96,96,true],"web-stories-thumbnail":["https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/08\/AWS-Lambda-1-150x75.png",150,75,true]},"uagb_author_info":{"display_name":"Anitha Dorairaj","author_link":"https:\/\/www.whizlabs.com\/blog\/author\/anitha-dorairaj\/"},"uagb_comment_info":3,"uagb_excerpt":"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&#8217;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,&hellip;","_links":{"self":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/97036","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/users\/408"}],"replies":[{"embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/comments?post=97036"}],"version-history":[{"count":10,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/97036\/revisions"}],"predecessor-version":[{"id":97097,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/97036\/revisions\/97097"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/media\/97091"}],"wp:attachment":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/media?parent=97036"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/categories?post=97036"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/tags?post=97036"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}