Going Serverless with AWS Lambda and API Gateway – Dice Insights

One of the goals of cloud computing is to minimize the number of resources you need. This isnt just a matter of eliminating any on-premises servers and drives; cloud-based serverless computing is when you create software that runs only as needed on servers dynamically allocated by the cloud provider (also as needed).

On AWS, you can accomplish serverless programming usingAWS Lambda, a service whereby you upload code in the form of functions that are triggered based on different events that you can configure. One such event is a call coming in through AWS API Gateway, which is the AWS service you use for setting up REST API services. As REST calls come in, the API Gateway can trigger your Lambda code to run. The Lambda code will receive information on the REST call that was made, and your code can respond accordingly.

Youll need to create a new Lambda function; in this example, well use Node.js. Head over to the Lambda console (from the management console, click the Services dropdown and click Lambda), and click the Create Function button; on the next screen, click Author From Scratch and fill in the details, including a name for your function (such as TestAPIServerless) and a version of node.js you want to use (probably the highest version listed).

For this sample, you dont need to set permissions. By default, the permissions will allow Lambda to upload logs to CloudWatch. Then, click Create Function.

The next screen allows you to enter code into a browser-based IDE and test the code. Well just have the code echo back information about the API call itself. In the IDE, replace the starter sample code with this code:

Now scroll up and click the orange Save button. Thats all there is to the Lambda function.

Head over to the API Gateway by clicking the Services dropdown and then API Gateway. In the API Gateway console, scroll down and find REST API, and click Build.

On the next screen, under Choose the Protocol, select REST. Under Create New API, select New API. Under Settings, provide a name such as MyTestAPI. You can fill in Description if you like; for Endpoint Type, choose Regional. Then click Create API.

This creates an empty API. Now youll need to add a couple of endpoint methods. Well start with the root endpoint, which is the default. Click the Actions dropdown, then click Create Method. In the dropdown below that, click GET so we can respond to incoming HTTP GET requests. Click the grey checkmark.

In the next screen, for Integration type, select Lambda Function. Check the Use Lambda Proxy Integration box; this configures the calls with additional data that simplifies the data coming into your Lambda function. In the Lambda Function box, type TestAPIServerless. Then click Save. A message will popup requesting permission for API Gateway to invoke your Lambda function; click OK.

For the next one, well create an API endpoint called /data. Each endpoint is a resource, so in the Actions dropdown, click Create Resource. In the screen that follows, next to Resource Name, type Data. Notice the Resource Path fills in automatically with /data. Now click Create Resource.

Now you have two endpoints; the root with just a slash, and /data. Click /data on the left and follow the same steps as above for adding a Get method. Were calling the same Lambda function, so all the steps will be the same, starting with clicking Create Method through clicking Save.

Deploy and Test the API

From the Actions dropdown, click Deploy API. In the pop-up window, for Deployment Stage, click New Stage. Under that, call the Stage Name APITestStage. You can leave the other fields blank. Click Deploy.

The API gateway provides various ways of testing, which you can explore in the documentation. But for now you can just call your API method using any tool you like, including curl or the browser itself. The URL youll use is displayed right at the top of the screen in a blue box; it will look something like this:

Right-click the URL and open in a new browser tab. Youll see a simple JSON response:

Now add /data?abc=10&def=20 to the end of the URL to invoke the /data endpoint with a couple of query parameters. Press Enter and youll see information about the path and parameters:

Thats it! You now have a serverless API that responds to two different endpoints.

In developing your API, youll likely want to use your own domain rather than long, default URLs.You can read about it in the official docs.

Also, if youre determined to remain serverless, you can make use of managed database services to provide the database backend to your software;heres sample code for accessing AWS RDS. As you bring the pieces together, youll see that you have a complete REST API fully hosted on AWS, but without the trouble of allocating any servers. (And as you develop your AWS-related knowledge, if youre interested in machine learning via Amazons cloud,check out how to get started with SageMaker and more.)

Membership has its benefits. Sign up for a free Dice profile, add your resume, discover great career insights and set your tech career in motion. Register now

Originally posted here:
Going Serverless with AWS Lambda and API Gateway - Dice Insights

Related Posts

Comments are closed.