API using AWS Lambda and API Gateway: A Guideline
Published: 2022-08-16 03:56:42

If you are interested to deploy an API using AWS Lambda and API Gateway; that will trigger two AWS Lambda Versions and they would base on the weight specified in percentage. The Web Code Genie who serves as Amazon Cloud Computing India will help you with a step-by-step guideline. Our company uses Google Cloud Computing as a service to create secure cloud solutions having no security risk for business. This article will give you knowledge about API using AWS Lambda and API Gateway Architecture, pre-requisite, Configuration, and Output that are discussed below.

## Architecture
Its architecture will consist of users, Lambda version 1, Lambda Version 2, and a centralized API Gateway.

**Pre-requisite**
    • You need to have an AWS account for implementing the configurations.
    • You must know AWS API Gateway and AWS Lambda.
    • You should have installed and configured AWS CLI on your PC.

**Configuration**

·   	To configure you need to log in to the AWS Management Console and in the search bar; search about Lambda and select it.
·   	With this, you will be directed to the AWS Lambda Management Console.

**STEP 1.** Creation of Lambda Function
·   	Here we have to create a Lambda Function which gives you the message “Hello World” by following the below steps you can create Lambda Function.
·   	Click on Create Function button
·   	After clicking, a web page will appear that will allow you to create a Lambda function.
·   	From the different options select Author from Scratch.
·   	Now in the Basic Information provide a name for the function such as Test-Lambda. Choose the Runtime to be Python 3.9.
·   	Click the Create Function button at the bottom and leave all the configurations to default.
·   	Then edit the code in the Lambda function to return “Hello World from Version 1” and deploy the code.

**Step 2** Create Lambda Versions
·   	When you edit the code, at the top click on Actions and Publish New Version.
·   	Now give the version Description if that is demanded and click on Publish button.
·   	Here you have to edit the code again of the Lambda function and deploy it as Version 2.
·   	Now publish a new Lambda Version by following the same process as did before. Click Action, then Publish Lambda Version, and at the end click Publish button on the dialog box appeared.

**Step 3** Create Lambda Alias
·   	First, click Actions, and a drop-down list will appear; from which choose Create Alias.
·   	You can give any name to an Alias such as Test-Alias and choose any version from Version.
·   	Next click Weighted Alias, choose Additional-Version and select any version from the drop-down list. Then specify the weight to be 50% and click Save Button.

**Step 4** Create a REST API using API Gateway
·   	In the [AWS Management Console](https://webcodegenie.com/services/cloud-computing-agency), in the search bar search API Gateway to reach the AWS API Gateway console.
·   	Now select API type to be REST API and select the Build button.
·   	Select the protocol to be Rest; in Create new API choose New API.
·   	In settings specify a name to the API as test-API.
·   	Let all the settings default and click on create API.
·   	Now create a Method by choosing Actions and selecting Create Method.
·   	From the drop-down list choose GET Method and click the tick button.
·   	You have to choose Integration Type for Lambda Function; after it select the Lambda function that we created i.e. test-lambda.
·   	Give a name to the Lambda Function

<lambda-function-name>: ${stageVariables.lambdaAlias}

and click the Save button.
·   	A dialogue box will show with a CLI command. Copy pastes it in a secure place for future reference. Then select the OK button.
·   	Deploy the API by selecting Actions and selecting Deploy API.
·   	Deploy the API, when you create a new Stage; give the Stage a Name such as Test.
·   	On the left Navigation panel choose Stages; and select Stage Variables.
·   	Now select the Add Stage variable; give the name as Lambda Alias and the Value as Test-Alias.
·   	Let run the Command, which you copied and pasted for future reference.
·   	Now, open the Command Prompt with the installed and configured AWS CLI with the region deployed the Architecture.
·   	Here you will run the command after changing the function name mentioned in the command. You will replace ${stageVariables.lambdaAlias} with the Lambda Alias name you created earlier i.e. test-Alias. 
·   	You will have a response that would be in JSON format. 
Congratulation! By following all these steps, you have successfully configured the above-mentioned Architecture. 

## Output
·   	If you want to check the deployed API; navigate the AWS API Gateway Console, by clicking on the API you just have created.
·   	On the left navigation panel, choose Stages that you just have created i.e. Test. 
·   	You will be able to see an Invoke URL. 
·   	Now open the URL in the browser and refresh the browser multiple times, you will come up with different responses from different Lambda Versions.

[Amazon Cloud Computing India](https://webcodegenie.com/services/cloud-computing-agency) has provided a step-by-step guideline for API using AWS Lambda and API Gateway. This company has chosen Google Cloud Computing for its data as it is risks free.