Serverless with AWS Lambda & API Gateway (Amazon Web Services)

18th July 2019   Servers

If you have been developing web applications in your whole career, chances are you already know how much boring server management is and how it can give you too many responsibilities like keeping your operating systems up to date and every software inside your server.

Not only that, A server is very expensive and unreliable! So if you are developing web applications and you are gaining too much traffic, obviously, your web server could potentially go down and you will need to upgrade your server which means another expense to your wallet. In contrast, if you are not getting web traffic at all, then you are just paying for a server capacity that you are not actually using.

 

Introduction

In this article, we’ll be providing you an interesting alternative, a solution or an idea in which you can run your web applications without managing servers.

Well, obviously you WILL still going to be using servers because there has to be a place where your application can be reached. However, there is a service called AWS also known as Amazon Web Services.

What is Amazon Web Services (AWS)?

What is Amazon Web Services (AWS)?

Amazon Web Services is a cloud computing department of Amazon that provides on-demand cloud computing platforms to individuals, companies, and governments, on a metered pay-as-you-go basis. Currently, you can create an account for AWS for free without paying for 12-months. However, this depends on how much memory or server capacity you are using. So if you are under the 12-month free tier then it’s better to keep your usage low.

AWS also offers a service where a group of servers is rigged together which allows you to provide a code on demand. So if you get a request, you can execute a certain code or do something as a response to that request, without actually provisioning or managing servers.

 

Why use AWS?

There are plenty of reasons why you should start using AWS. One of the reasons is how convenient for developers to use servers without actually managing it. But of course, that’s not the only reasons.

Per-hour Billing

Billing is one of another reason why you should also use AWS. By using AWS, you will get a per-hour billing. Every instance or service you use has a micro-billing which is very transparent. Even S3 buckets are charged on a GB basis. It might sound very expensive but in our experience, Amazon Web Services is one of the least expensive services compared to other host providers.

Easy to Sign Up

Signing up to Amazon Web Services is very easy. Just visit AWS official website and click the sign-up button at the right top corner. Though by signing up to AWS, you are required to provide your credit card details.

After signing up to AWS, you will be able to setup your servers in just 2-minutes without procuring any hardware which is very fast compared to other service providers.

Billing Interface

With Amazon Web Services, you will be provided an integrated dashboard which gives you reports about your current billing. With this interface, you may pull out your reports based on which services you used or based on any parameters.

Performance

There’s no doubt that AWS is fast. With the help of AWS tools, Autoscaling, and elastic load balancing, your application can be scaled up and down based on demand. With Amazon’s massive infrastructure, you have access to compute and storage the resources whenever you need them.

Security

AWS provides VPC, which can be used to host services on a private network which is not accessible from the internet but can communicate with the resources in the same network. This will restrict access to the resources from any bad users from the internet. The only way to access these resources is by using the Amazon VPN or some open source services like OpenVPN or NordVPN.


In this article, we’ll be more focusing on AWS Lambda (also known as Lambda for short).

What is AWS Lambda?

AWS Lambda is a serverless computing service provided by AWS where you can basically provide functions which encapsulates the code you want to execute depending on certain triggers. And while we don’t need to deal with the internals of how Lambda works, it is important to have a general idea of how your functions will be executed using Lambda.

Let’s start with Lambda’s technical specifications. Currently, Lambda supports the following runtime:

  • Node.js: v10 and v8.10
  • Java 8
  • Python: 3.7, 3.6, and 2.7
  • .NET Core: 1.0.1 and 2.1
  • Go 1.x
  • Ruby 2.5

You may also use other languages in Lambda by implementing a custom runtime. The Lambda execution environment provides a runtime interface for getting invocation events and sending responses.

What are some good uses for AWS Lambda?

There are plenty of good uses for AWS Lambda. Whenever there’s an event in your application, you may send a request to Lambda and it will return you a function.

For example:

AWS Lambda Triggers an Event on Image Upload

In the example above, an event was triggered when an image is uploaded to S3 Bucket and as a response, Lambda resized and cropped the image and return it to the user.

There are a couple more good uses for Lambda, like:

  • Send a notification when the users interact with some functions.
  • Send receipt to the seller when you purchase an item
  • Show more posts as the user scrolls down the page
  • Get user’s data on login or send data on logout.
  • Backup data every midnight

What is more amazing with AWS Lambda is that you can use it with other AWS services such as API Gateway.

What is API Gateway?

Amazon API Gateway is a fully managed service that makes it easy for developers to build, distribute, maintain, monitor, and secure APIs at any scale. With just a few clicks in the AWS Management Console, you can create REST and WebSocket APIs that act as a “front door” for applications to access data, business logic, or functionality from your backend services, such as workloads running on Amazon Elastic Compute Cloud (Amazon EC2), code running on AWS Lambda, any web application, or real-time communication applications.

API Gateway handles all the tasks involved in accepting and processing up to hundreds of thousands of concurrent API calls, including traffic management, authorization, and access control, monitoring, and API version management. API Gateway has no minimum fees or startup costs. You pay only for the API calls you receive and the amount of data transferred out and, with the API Gateway tiered pricing model, you can reduce your cost as your API usage scales.

How API Gateway Works?

New-API-GW-Diagram

 

API Gateway Features

Easy API Creation and Deployment

With API Gateway, you can quickly and easily create a custom API to your code running in AWS Lambda and then call the Lambda code from your API. API Gateway can execute AWS Lambda code in your account, start AWS Step Functions state machines, or make calls to AWS Elastic Beanstalk, Amazon EC2, or web services outside of AWS with publicly accessible HTTP endpoints. Using the API Gateway console, you can define your REST API and its associated resources and methods, manage your API lifecycle, generate your client SDKs, and view API metrics.

Support for REST APIs and WebSocket APIs

With API Gateway, you can create REST APIs that leverage HTTP request types or WebSocket APIs that enable you to build real-time two-way communication applications, such as chat apps and streaming dashboards. A REST API is a group of resources and methods or endpoints. They can be deployed to different stages and cloned to new versions. A WebSocket API maintains a persistent connection between connected clients. You can define backend integrations with AWS Lambda functions, Amazon Kinesis, or any HTTP endpoint to be invoked when messages are received from the connected clients.

Resiliency

API Gateway helps you manage traffic to your backend systems by allowing you to set throttling rules based on the number of requests per second for each HTTP method in your APIs. In addition, you can set up a cache with customizable keys and time-to-live in seconds for your API data to avoid hitting your backend services for each request. API Gateway handles any level of traffic received by an API, so you are free to focus on your business logic and services rather than maintaining infrastructure.

API Lifecycle Management

API Gateway lets you run multiple versions of the same API simultaneously so that applications can continue to call previous API versions even after the latest versions are published. API Gateway also helps you manage multiple release stages for each API version, such as alpha, beta, and production. Each API stage can be configured to interact with different backend endpoints based on your API setup. Specific stages and versions of an API can be associated with a custom domain name and managed through API Gateway. Stage and version management allows you to easily test new API versions that enhance or add new functionality to earlier API releases and ensures backward-compatibility as user communities transition to adopt the latest release.

SDK Generation

API Gateway can generate client SDKs for a number of platforms, which you can use to quickly test new APIs from your applications and distribute SDKs to third-party developers. The generated SDKs handle API keys and sign requests using AWS credentials. API Gateway can generate client SDKs for Java, JavaScript, Java for Android, Objective-C or Swift for iOS, and Ruby. You can use AWS CLI to generate and download an SDK of an API for a supported platform by calling the get-sdk command.

 

API Operations Monitoring

After an API is deployed and in use, API Gateway provides you with a dashboard to visually monitor calls to the services. The API Gateway console is integrated with Amazon CloudWatch, so you get backend performance metrics such as API calls, latency, and error rates. Because API Gateway uses CloudWatch to record monitoring information, you can set up custom alarms on API Gateway APIs. API Gateway can also log API execution errors to CloudWatch Logs to make debugging easier.

AWS Authorization

To authorize and verify API requests to AWS services, API Gateway can help you leverage signature version 4 — the same technology used by AWS for its services. Using signature version 4 authentication, you can use AWS Identity and Access Management (IAM) and access policies to authorize access to your APIs and all your other AWS resources. You can also use AWS Lambda functions to verify and authorize bearer tokens such as JWT tokens or SAML assertions.

API Keys for Third-Party Developers

API Gateway helps you manage the ecosystem of third-party developers accessing your APIs. You can create API keys on API Gateway, set fine-grained access permissions on each API key, and distribute them to third-party developers to access your APIs. You can also define plans that set throttling and request quota limits for each individual API key. The use of API keys is completely optional and must be enabled on a per-method level.

Conclusion

Whenever you develop web applications, you are required to have a server where it is reachable by the internet. Some server providers will give you good features and some providers don’t. It’s just a matter of experimentation. But if you are going new to web development then It is much better to immediately try Amazon Web Services, as it will save you a lot of time and a lot of expenses.

React to this topic
Bernard Polidario
About Bernard Polidario

A full-stack developer who has been around programming community for 10+ years and still has a lot of passion for programming and design. I am the founder of WeeklyHow and I can be reached at [email protected]

https://weeklyhow.com

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*
*