{"id":1670,"date":"2017-10-11T13:20:26","date_gmt":"2017-10-11T13:20:26","guid":{"rendered":"https:\/\/www.techuz.com\/blog\/?p=1670"},"modified":"2019-05-01T05:20:34","modified_gmt":"2019-05-01T05:20:34","slug":"steps-creating-serverless-microservice-aws-lambda","status":"publish","type":"post","link":"https:\/\/www.techuz.com\/blog\/steps-creating-serverless-microservice-aws-lambda\/","title":{"rendered":"Steps for Creating Serverless Microservice Architecture with AWS Lambda"},"content":{"rendered":"<div class=\"article__content content column\">\n<h2><strong>Serverless architecture<\/strong>:<\/h2>\n<p>In serverless architecture, the server will not run all the time. Server will be active only when any request will be there for processing and in remaining time server will in sleep mode. Unlike server architecture, monthly subscription plan need not to be taken as user has to pay that much amount for the active time of server which helps to reduce server cost.<\/p>\n<h2><strong>Microservice<\/strong>:<\/h2>\n<p>Microservice architecture is a method of developing system in a small modules which communicates internally with other modules through lightweight mechanism like HTTP\/REST with JSON.<\/p>\n<h3>Advantages of using Microservice Architecture:<\/h3>\n<p>\u2022 Each module of application can be developed, tweaked and redeployed independently<\/p>\n<p>\u2022 In Microservice architecture, different modules of the application can be built with different technologies.<\/p>\n<p>\u2022 In Microservice architecture, each modules usually mange its unique databases so decentralized data management can be easily done.<\/p>\n<h3>Reason for using Microservice Architecture:<\/h3>\n<p>\u2022 In monolith applications, server-side handles all the HTTP request of applications which executes logic and do CRUD operation on database so if one component need to scale whole application need to scale this comes the reason to use <a href=\"https:\/\/www.techuz.com\/node-js-development\/\" rel=\"\">Microservice architecture<\/a>.<\/p>\n<p>Below are steps for creating AWS account and AWS Lambda function with REST API in AWS cloud with minimal code.<\/p>\n<h2>Steps for Creating AWS Account:<\/h2>\n<h3>Step 1.<\/h3>\n<p>Go to\u00a0<a href=\"https:\/\/aws.amazon.com\/\" target=\"_blank\" rel=\"nofollow noopener\">https:\/\/aws.amazon.com\/<\/a>\u00a0and click on Create a Free Account.<\/p>\n<p><img decoding=\"async\" class=\"loaded\" src=\"https:\/\/cdn.scotch.io\/48464\/CK0H8wrQQguNJWDYwVus_image1.png\" alt=\"\" data-original=\"https:\/\/cdn.scotch.io\/48464\/CK0H8wrQQguNJWDYwVus_image1.png\" data-was-processed=\"true\" \/><\/p>\n<h3>Step 2.<\/h3>\n<p>Now you have to enter your email address and select \u201cI am a new user.\u201d option and then click on Sign in using our secure server.<\/p>\n<p><img decoding=\"async\" class=\"loaded\" src=\"https:\/\/cdn.scotch.io\/48464\/o9QXAlLeTV3kiRN6QcZN_image2.png\" alt=\"\" data-original=\"https:\/\/cdn.scotch.io\/48464\/o9QXAlLeTV3kiRN6QcZN_image2.png\" data-was-processed=\"true\" \/><\/p>\n<h3>Step 3.<\/h3>\n<p>After that, you need to enter your Full Name, retype your email address and set password for your account and then click on Create Account.<\/p>\n<h3><img decoding=\"async\" class=\"loaded\" src=\"https:\/\/cdn.scotch.io\/48464\/oeDRVEYdTCSXjarqMJsv_image3.png\" alt=\"\" data-original=\"https:\/\/cdn.scotch.io\/48464\/oeDRVEYdTCSXjarqMJsv_image3.png\" data-was-processed=\"true\" \/>Step 4.<\/h3>\n<p>In Contact Information tab, select Personal Account and fill your personal details and click on Create Account and Continue.<\/p>\n<p><img decoding=\"async\" class=\"loaded\" src=\"https:\/\/cdn.scotch.io\/48464\/dzEVPi1IQ0OqEgrL66vs_image4.png\" alt=\"\" data-original=\"https:\/\/cdn.scotch.io\/48464\/dzEVPi1IQ0OqEgrL66vs_image4.png\" data-was-processed=\"true\" \/><\/p>\n<h3>Step 5.<\/h3>\n<p>Now you have to enter your bank details for payment and click on Verify Card and Continue.<\/p>\n<h3><img decoding=\"async\" class=\"loaded\" src=\"https:\/\/cdn.scotch.io\/48464\/Y8j5DqVfSqqPMcDRvIRY_image5.png\" alt=\"\" data-original=\"https:\/\/cdn.scotch.io\/48464\/Y8j5DqVfSqqPMcDRvIRY_image5.png\" data-was-processed=\"true\" \/>Step 6.<\/h3>\n<p>In Identity Verification tab, you need to verify your mobile number by entering Captcha and your mobile number with extension. Once your number is verified click on Continue to select your Support Plan.<\/p>\n<p><img decoding=\"async\" class=\"loaded\" src=\"https:\/\/cdn.scotch.io\/48464\/ErZoswxjR3mj2ZcYtPbf_image6.png\" alt=\"\" data-original=\"https:\/\/cdn.scotch.io\/48464\/ErZoswxjR3mj2ZcYtPbf_image6.png\" data-was-processed=\"true\" \/><\/p>\n<h3>Step 7.<\/h3>\n<p>Select your plan according to your need and click on Continue.<\/p>\n<p><img decoding=\"async\" class=\"loaded\" src=\"https:\/\/cdn.scotch.io\/48464\/5sm1ZwoSCqMNywJaZpoA_image7.png\" alt=\"\" data-original=\"https:\/\/cdn.scotch.io\/48464\/5sm1ZwoSCqMNywJaZpoA_image7.png\" data-was-processed=\"true\" \/><\/p>\n<p>This will create your AWS account. For Sign-In click on Sign-In console and enter your email address, select \u201cI am a returning user and my password is:\u201d and enter your password after that click on Sign in using our secure server.<\/p>\n<p><img decoding=\"async\" class=\"loaded\" src=\"https:\/\/cdn.scotch.io\/48464\/dK7diZSRYCsvuecMhrkw_image8.png\" alt=\"\" data-original=\"https:\/\/cdn.scotch.io\/48464\/dK7diZSRYCsvuecMhrkw_image8.png\" data-was-processed=\"true\" \/><\/p>\n<h2>Steps for Creating AWS Lambda Function:<\/h2>\n<h3>Step 1.<\/h3>\n<p>After Sign in, go in Services tab and search for Lambda and then select Lambda.<\/p>\n<h3><img decoding=\"async\" class=\"loaded\" src=\"https:\/\/cdn.scotch.io\/48464\/KaNa4TvOS9eyDwTRxBIC_image9.png\" alt=\"\" data-original=\"https:\/\/cdn.scotch.io\/48464\/KaNa4TvOS9eyDwTRxBIC_image9.png\" data-was-processed=\"true\" \/>Step 2.<\/h3>\n<p>Click on Get started Now.<\/p>\n<h3><img decoding=\"async\" class=\"loaded\" src=\"https:\/\/cdn.scotch.io\/48464\/lakLsbNQYiexqiCsHxKu_image10.png\" alt=\"\" data-original=\"https:\/\/cdn.scotch.io\/48464\/lakLsbNQYiexqiCsHxKu_image10.png\" data-was-processed=\"true\" \/>Step 3.<\/h3>\n<p>In Select blueprint page, select runtime from dropdown and select your blueprint accordingly. Now I am selecting Node.js 6.10 and Blank Function as my blueprint.<\/p>\n<h3><img decoding=\"async\" class=\"loaded\" src=\"https:\/\/cdn.scotch.io\/48464\/4HAT4FkQ9a56Pc7aNSVh_image11.png\" alt=\"\" data-original=\"https:\/\/cdn.scotch.io\/48464\/4HAT4FkQ9a56Pc7aNSVh_image11.png\" data-was-processed=\"true\" \/>Step 4.<\/h3>\n<p>After that, you need to select how you will call Lambda function by clicking on blank square box or you can skip this by clicking on next. I am skipping this step by clicking on next.<\/p>\n<h3><img decoding=\"async\" class=\"loaded\" src=\"https:\/\/cdn.scotch.io\/48464\/nl6X5THQTXyl1vWQnPPr_image12.png\" alt=\"\" data-original=\"https:\/\/cdn.scotch.io\/48464\/nl6X5THQTXyl1vWQnPPr_image12.png\" data-was-processed=\"true\" \/>Step 5.<\/h3>\n<p>In configure function tab, you have to enter function name, description and environment in which you want to run and can change function code in Lamda function code tab. I have created Lambda function which will reverse a string and written logic for same in Lambda function code tab.<\/p>\n<p><img decoding=\"async\" class=\"loaded\" src=\"https:\/\/cdn.scotch.io\/48464\/g0D8lOEdSMOwWCqNYjF2_image13.png\" alt=\"\" data-original=\"https:\/\/cdn.scotch.io\/48464\/g0D8lOEdSMOwWCqNYjF2_image13.png\" data-was-processed=\"true\" \/><\/p>\n<p>Scroll Down and set Role for the function. Select Create new Role from template(s) and enter role name.<\/p>\n<p><img decoding=\"async\" class=\"loaded\" src=\"https:\/\/cdn.scotch.io\/48464\/QNCtXrpLS5u9zhsC4Vwz_image14.png\" alt=\"\" data-original=\"https:\/\/cdn.scotch.io\/48464\/QNCtXrpLS5u9zhsC4Vwz_image14.png\" data-was-processed=\"true\" \/><\/p>\n<p>Click on Advance Tab and here memory for function is set for 128mb and timeout for running function is set for 3s which I think is enough and then click on Next.<\/p>\n<h3><img decoding=\"async\" class=\"loaded\" src=\"https:\/\/cdn.scotch.io\/48464\/WuneZwz8SMGZpZBcaZAk_image15.png\" alt=\"\" data-original=\"https:\/\/cdn.scotch.io\/48464\/WuneZwz8SMGZpZBcaZAk_image15.png\" data-was-processed=\"true\" \/>Step 6.<\/h3>\n<p>After that review of function will come and then click on Create function.<\/p>\n<p><img decoding=\"async\" class=\"loaded\" src=\"https:\/\/cdn.scotch.io\/48464\/gG3ahdETShqFqC7fb2L3_image16.png\" alt=\"\" data-original=\"https:\/\/cdn.scotch.io\/48464\/gG3ahdETShqFqC7fb2L3_image16.png\" data-was-processed=\"true\" \/><\/p>\n<p>You can test your Lamda function by clicking on Test.<\/p>\n<p><img decoding=\"async\" class=\"loaded\" src=\"https:\/\/cdn.scotch.io\/48464\/iARSCqh7SBq1HduZhiks_image17.png\" alt=\"\" data-original=\"https:\/\/cdn.scotch.io\/48464\/iARSCqh7SBq1HduZhiks_image17.png\" data-was-processed=\"true\" \/><\/p>\n<p>You can see your function output in execution result. I have created function of reverse string of \u201cHello from Lambda\u201d so you can see my result.<\/p>\n<p><img decoding=\"async\" class=\"loaded\" src=\"https:\/\/cdn.scotch.io\/48464\/gEBSNvl7So6EEEfl6QER_image18.png\" alt=\"\" data-original=\"https:\/\/cdn.scotch.io\/48464\/gEBSNvl7So6EEEfl6QER_image18.png\" data-was-processed=\"true\" \/><\/p>\n<h2>Steps for Creating API for Lambda Function:<\/h2>\n<h3>Step 1.<\/h3>\n<p>Go in service Tab and search for API Gateway and click on API Gateway.<\/p>\n<p><img decoding=\"async\" class=\"loaded\" src=\"https:\/\/cdn.scotch.io\/48464\/dAkM7ZrTxuDeRFOFPA5A_image19.png\" alt=\"\" data-original=\"https:\/\/cdn.scotch.io\/48464\/dAkM7ZrTxuDeRFOFPA5A_image19.png\" data-was-processed=\"true\" \/><\/p>\n<h3>Step 2.<\/h3>\n<p>Click on Get Started.<\/p>\n<p><img decoding=\"async\" class=\"loaded\" src=\"https:\/\/cdn.scotch.io\/48464\/WmgxgvHQmyIv6gqZIrr1_image20.png\" alt=\"\" data-original=\"https:\/\/cdn.scotch.io\/48464\/WmgxgvHQmyIv6gqZIrr1_image20.png\" data-was-processed=\"true\" \/><\/p>\n<h3>Step 3.<\/h3>\n<p>Click on New API and enter API name, description and then click on Create API.<\/p>\n<p><img decoding=\"async\" class=\"loaded\" src=\"https:\/\/cdn.scotch.io\/48464\/0TQJuqFXSYWFFrESSx5O_image21.png\" alt=\"\" data-original=\"https:\/\/cdn.scotch.io\/48464\/0TQJuqFXSYWFFrESSx5O_image21.png\" data-was-processed=\"true\" \/><\/p>\n<h3>Step 4.<\/h3>\n<p>Here I am creating API whose request type is Get so click on Actions and then click on Create Method in which you have to select GET as method.<\/p>\n<h3><img decoding=\"async\" class=\"loaded\" src=\"https:\/\/cdn.scotch.io\/48464\/fxDHfpH7Qw6eZq9sJQDT_image22.png\" alt=\"\" data-original=\"https:\/\/cdn.scotch.io\/48464\/fxDHfpH7Qw6eZq9sJQDT_image22.png\" data-was-processed=\"true\" \/>Step 5.<\/h3>\n<p>Click on right button below Get method. Then select Lamda Region, enter your Lambda function name and then click on Save.<\/p>\n<p><img decoding=\"async\" class=\"loaded\" src=\"https:\/\/cdn.scotch.io\/48464\/nADKHU0OSX2uDtW1p3a9_image23.png\" alt=\"\" data-original=\"https:\/\/cdn.scotch.io\/48464\/nADKHU0OSX2uDtW1p3a9_image23.png\" data-was-processed=\"true\" \/><\/p>\n<p>For creating API, you need to add permission so click on OK.<\/p>\n<p><img decoding=\"async\" class=\"loaded\" src=\"https:\/\/cdn.scotch.io\/48464\/yXwvSt9jT5Sk15nvJfrs_image24.png\" alt=\"\" data-original=\"https:\/\/cdn.scotch.io\/48464\/yXwvSt9jT5Sk15nvJfrs_image24.png\" data-was-processed=\"true\" \/><\/p>\n<h3>Step 6.<\/h3>\n<p>In Get Method Execution Screen, click on Integration Request.<\/p>\n<p><img decoding=\"async\" class=\"loaded\" src=\"https:\/\/cdn.scotch.io\/48464\/P7RaMPXBTMmV2v69UjR7_image25.png\" alt=\"\" data-original=\"https:\/\/cdn.scotch.io\/48464\/P7RaMPXBTMmV2v69UjR7_image25.png\" data-was-processed=\"true\" \/><\/p>\n<p>You can check whether your Lambda function is selected or not.<\/p>\n<p><img decoding=\"async\" class=\"loaded\" src=\"https:\/\/cdn.scotch.io\/48464\/dCgHqiXsSkqU2S6p8en4_image26.png\" alt=\"\" data-original=\"https:\/\/cdn.scotch.io\/48464\/dCgHqiXsSkqU2S6p8en4_image26.png\" data-was-processed=\"true\" \/><\/p>\n<p>Expand Body Mapping Templates tab and select \u201cWhen there are no templates defined (recommended)\u201d and enter Content Type as \u201capplication\/json\u201d by clicking on Add mapping template and then click on right button.<\/p>\n<p>Enter your JSON object in General template which helps you to pass input in headers and then click on save button.<\/p>\n<p><img decoding=\"async\" class=\"loaded\" src=\"https:\/\/cdn.scotch.io\/48464\/5ej9CpR4yipSQphcbtNw_image27.png\" alt=\"\" data-original=\"https:\/\/cdn.scotch.io\/48464\/5ej9CpR4yipSQphcbtNw_image27.png\" data-was-processed=\"true\" \/><\/p>\n<p>Now API is ready, to Deploy API click on Actions menu and then click on Deploy API.<\/p>\n<p>Select New Stage for Deployment Stage and enter Stage name. You can also enter production description, deployment description and then click on Deploy.<\/p>\n<p><img decoding=\"async\" class=\"loaded\" src=\"https:\/\/cdn.scotch.io\/48464\/vLHhf4VFS2WYrLiU19KZ_image28.png\" alt=\"\" data-original=\"https:\/\/cdn.scotch.io\/48464\/vLHhf4VFS2WYrLiU19KZ_image28.png\" data-was-processed=\"true\" \/><\/p>\n<p>You can call API by clicking on Invoke URL.<\/p>\n<p><img decoding=\"async\" class=\"loaded\" src=\"https:\/\/cdn.scotch.io\/48464\/E1pKyzMyStmhJQRUIBMe_image29.png\" alt=\"\" data-original=\"https:\/\/cdn.scotch.io\/48464\/E1pKyzMyStmhJQRUIBMe_image29.png\" data-was-processed=\"true\" \/><\/p>\n<p>To use input of API in your Lambda function you can get value from event object. I am here modifying my Lambda function by going in Lambda tab and select your function name which you want to edit. Once you edited the code click on Save and Test,<\/p>\n<p><img decoding=\"async\" class=\"loaded\" src=\"https:\/\/cdn.scotch.io\/48464\/DGZXCqBnSRCYFNqqSTmO_image30.png\" alt=\"\" data-original=\"https:\/\/cdn.scotch.io\/48464\/DGZXCqBnSRCYFNqqSTmO_image30.png\" data-was-processed=\"true\" \/><\/p>\n<p>Now you can API by URL followed by parameters in query string.<\/p>\n<p><img decoding=\"async\" class=\"loaded\" src=\"https:\/\/cdn.scotch.io\/48464\/QE91IRbqSeV93WcbLkBd_image31.png\" alt=\"\" data-original=\"https:\/\/cdn.scotch.io\/48464\/QE91IRbqSeV93WcbLkBd_image31.png\" data-was-processed=\"true\" \/><\/p>\n<p><em class=\"markup--em markup--p-em\">Originally published at <a href=\"https:\/\/scotch.io\/@VaibhavShah\/how-to-create-serverless-microservice-with-aws-lamda\" target=\"_blank\" rel=\"nofollow noopener\">Scotch.io<\/a><\/em><em class=\"markup--em markup--p-em\">\u00a0on September 26, 2017.<\/em><\/p>\n<\/div>\n<aside class=\"sidebar column is-narrow\">\n<div class=\"sidebar__box m-b-lg\"><\/div>\n<\/aside>\n","protected":false},"excerpt":{"rendered":"<p>Serverless architecture: In serverless architecture, the server will not run all the time. Server will be active only when any request will be there for processing and in remaining time server will in sleep mode. Unlike server architecture, monthly subscription plan need not to be taken as user has to pay that much amount for &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.techuz.com\/blog\/steps-creating-serverless-microservice-aws-lambda\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Steps for Creating Serverless Microservice Architecture with AWS Lambda&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":1904,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"better_featured_image":{"id":1904,"alt_text":"","caption":"","description":"","media_type":"image","media_details":{"width":1499,"height":749,"file":"2017\/10\/AWS-1-1.jpg","sizes":{"thumbnail":{"file":"AWS-1-1-150x150.jpg","width":150,"height":150,"mime-type":"image\/jpeg","source_url":"https:\/\/www.techuz.com\/blog\/wp-content\/uploads\/2017\/10\/AWS-1-1-150x150.jpg"},"medium":{"file":"AWS-1-1-300x150.jpg","width":300,"height":150,"mime-type":"image\/jpeg","source_url":"https:\/\/www.techuz.com\/blog\/wp-content\/uploads\/2017\/10\/AWS-1-1-300x150.jpg"},"medium_large":{"file":"AWS-1-1-768x384.jpg","width":768,"height":384,"mime-type":"image\/jpeg","source_url":"https:\/\/www.techuz.com\/blog\/wp-content\/uploads\/2017\/10\/AWS-1-1-768x384.jpg"},"large":{"file":"AWS-1-1-1024x512.jpg","width":1024,"height":512,"mime-type":"image\/jpeg","source_url":"https:\/\/www.techuz.com\/blog\/wp-content\/uploads\/2017\/10\/AWS-1-1-1024x512.jpg"},"blog_list":{"file":"AWS-1-1-460x230.jpg","width":460,"height":230,"mime-type":"image\/jpeg","source_url":"https:\/\/www.techuz.com\/blog\/wp-content\/uploads\/2017\/10\/AWS-1-1-460x230.jpg"},"alm-thumbnail":{"file":"AWS-1-1-150x150.jpg","width":150,"height":150,"mime-type":"image\/jpeg","source_url":"https:\/\/www.techuz.com\/blog\/wp-content\/uploads\/2017\/10\/AWS-1-1-150x150.jpg"},"twentyseventeen-thumbnail-avatar":{"file":"AWS-1-1-100x100.jpg","width":100,"height":100,"mime-type":"image\/jpeg","source_url":"https:\/\/www.techuz.com\/blog\/wp-content\/uploads\/2017\/10\/AWS-1-1-100x100.jpg"}},"image_meta":{"aperture":"0","credit":"","camera":"","caption":"","created_timestamp":"0","copyright":"","focal_length":"0","iso":"0","shutter_speed":"0","title":"","orientation":"0","keywords":[]}},"post":1670,"source_url":"https:\/\/www.techuz.com\/blog\/wp-content\/uploads\/2017\/10\/AWS-1-1.jpg"},"_links":{"self":[{"href":"https:\/\/www.techuz.com\/blog\/wp-json\/wp\/v2\/posts\/1670"}],"collection":[{"href":"https:\/\/www.techuz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techuz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techuz.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techuz.com\/blog\/wp-json\/wp\/v2\/comments?post=1670"}],"version-history":[{"count":4,"href":"https:\/\/www.techuz.com\/blog\/wp-json\/wp\/v2\/posts\/1670\/revisions"}],"predecessor-version":[{"id":4047,"href":"https:\/\/www.techuz.com\/blog\/wp-json\/wp\/v2\/posts\/1670\/revisions\/4047"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techuz.com\/blog\/wp-json\/wp\/v2\/media\/1904"}],"wp:attachment":[{"href":"https:\/\/www.techuz.com\/blog\/wp-json\/wp\/v2\/media?parent=1670"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techuz.com\/blog\/wp-json\/wp\/v2\/categories?post=1670"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techuz.com\/blog\/wp-json\/wp\/v2\/tags?post=1670"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}