Serverless Infrastructure in AWS
Maintaining servers are hard but AWS eases managing the the infrastructure but it is still hard to scale and orchestrate. Think you post your startup to #Producthunt and got a sh*tload of traffic.
With Lambda and Api Gateway you can create your own infrastructure to have a serverless architecture in AWS.
Lambda enables you to run Java, Node.js, Python, Go and C# code when triggered. It can handle requests like thousands per second and you only pay the duration of the execution. With Lambda, the most common use cases are image manipulation operations like creating thumbnails or resizing ( an ugly solution can be found here ) , Analyzing Logs with CloudWatch, Stream Processing, System Anomaly Detections, Data Conversions and much more. Today I will try to create a simple endpoint with API Gateway and Lambda that only costs when there is traffic and that scale very easily.
Step 1: ( Assuming you already have an AWS account ) As a start, we will create a simple Lambda function that says hello. We navigate to https://eu-central-1.console.aws.amazon.com/lambda/home and create a function.
Step 2: We just enter a few simple details. For the sake of simplicity, I will create a Node Runtime lambda. You can leave other parts as is for now. After creating the Function, the page should be similar to this one :
After we created our sample function let’s create our API Gateway side.
Amazon API Gateway is very easy to use and manage API Gateways. You can create restful endpoints to your clients very easily. It has integration with EC2, Lambdas and other publicly addressable web services.
Step 3: We just create a new sample API from scratch. The created API will have no resources or actions defined. We will do it now to enable our clients to connect our resources.
Step 4: After action is created, it asks us to connect with another service. We can now select the hello-function Lambda function that we created above and connect both.
So the final view of our API Gateway will be something similar to this.
Step 5: Let’s deploy our API to directly to production ;). For this, we click on the actions button on top, and click deploy API. We just enter the fields as follows :
After creating the prod stage, our gateway end point is created.
As we created our AWS Lambda function and API Gateway. Let’s see them together on Lambda function page.
As you can see Lambda and API Gateway is connected. so it is time to do some testing :)
As we successfully integrated those two, now we can update our Lambda function to
‘callback(null, ‘Hello from Lambda with Updated Response!’);’
and just save. When we try to access to our API Gateway, we will see the updated response.
With the help of API Gateway, you can manage your stage deployments, parameters, apply authorization, limiting, mocking and trigger any other event etc. This can scale as much you need.
With the help of AWS Lambda, you can run any serverless code and scale easily.
You can find more details about AWS Lambda and API Gateway below.
https://aws.amazon.com/lambda/features/
Cheers !