Before we get into what is Lambda and how to launch a Serverless function on AWS, Serverless most often refers to Serverless applications. Serverless Computing is the next generation cloud computing technology that is also popular as Function-as-a-Service (FaaS). You don’t have to worry about underlying details of your infrastructure instead write and deploy your code on Serverless like Lambda.
There are two types of services in Serverless architecture,
1. FaaS (Function as a Service)
2. BaaS (Backend as a Service)
Function as a Service: This describes simply deploying & running your code without managing the server. In this case developers do not worry about the hosting environment. AWS Lambda, Azure Function and GCP Cloud Function are popular FaaS services. FaaS support event driven workload with a short lived process.
Backend as a Service: This helps to automate the backend development and manage the infrastructure. In this model, the Frontend will interact with the Backend via the API’s. AWS Amplify, Azure Mobile Apps and Firebase are the most popular BaaS services.
The benefits of serverless architecture is to provide a way to keep the business focused on itself and its code application. It will remove the infrastructure responsibility and reduce the cost & increase the delivery speed. Let’s quickly jump into how to create AWS Lambda Function.
- An AWS account (https://portal.aws.amazon.com/billing/signup#/start)
- Some programming experience (Python in this case)
Start by creating your first serverless Python Lambda function
In this article, we will show you how to create a new lambda function. Create the Lambda function on the AWS Lambda homepage by clicking the ‘Create a Function’ button.
This brings you to the function creation screen where you have a few items to configure, before your function is created:
- Select the Author from scratch.
- Enter the Function name.
- Select Runtime (In this example, Python).
- Permissions, Lambda function will create a basic execution role which will access the Cloud watch for logs automatically.
Click on the ‘Create Function’ button to Create. Once the function is successfully created, you will see the Lambda function configuration screen.
You can write the python or upload the code as zip or upload the code from s3 and Test. To test the function, click on Test and configure the test events.
Now you have your first function ready!
Creating Your First Serverless API
Now that you have created your first function, it is time to turn to the API side of things.
Navigate to the API Gateway on AWS console, if you haven’t used the service before then you will see an introductory screen:
Click Get Started, select REST API and Build.
- Enter the API Name.
- Enter Description (Optional).
- Endpoint Type as Regional.
An API Endpoint Type describes the hostname of the API. The API endpoint type can be edge-optimized, regional, or private. Selection of endpoint type will depend on the majority of your API traffic originating from.
Click on ‘Create API’, and this will create an empty API. Now you need to add a few endpoint methods based.
For now, you are going to create a Resource on your API.
- Click on ‘Actions’ and then click ‘Create Resource’.
- Enter Resource name.
Click on ‘Create Resource’. And now you can add various methods on your new resource.
- Click on ‘Actions’ and then click ‘Create Method’.
- Select Method type (‘GET’ in this example).
- Select the Integration Endpoint as Lambda Function.
- Select the Region (where the lambda function is created).
- Enter the Lambda function name, and select.
Click on ‘Save’ and a message will popup requesting permission for API Gateway to invoke your Lambda function; click OK.
AWS shows an entire flow of the API integration. Select any of the stages to modify your configuration if you wish.
To test the API manually, click on the “Test”.
You’ll see the API response on the response body when you click on ‘Test’.
Your final step is to deploy your new API resource and method.
Click on the ‘Actions’ and then click ‘Deploy API’. On the dialog popup, Create New stage and enter the Stage name (Production in this example). Finally Click on Deploy.
Once the API is deployed, API gateway will generate a random subdomain for the API endpoint URL, and the stage name will be added to the end of the URL. You should now be able to make GET requests to a similar URL:
Copy the Invoke URL and test it on the browser.
Test the API via the cURL
curl -H “Content-Type: application/json” -X GET \
In this article, we have understood the functioning of AWS Lambda, Amazon API gateway, working of API Gateway, features of API Gateway, and API Types. Finally, we’ve created an AWS lambda function with Amazon API Gateway as the trigger.
Now that you’re here, you’ve successfully written your first Lambda Python function.
You need to consider other points which are mentioned below:
Methods: You can create different methods available on the API gateway like POST, OPTIONS, PUT, DELETE etc. You need to enable CORS so that POST method API endpoints will work from another domain.
Cross-origin resource sharing (CORS) is a browser security feature which restricts the HTTP requests from cross-origin that are generated from scripts running in the browser.
Add request validation. Refer https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-method-request-validation.html.
Authentication: You can control and manage access to a REST API in API Gateway. Refer https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-control-access-to-api.html
Use your own custom domain. Refer https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-custom-domains.html
Stay tuned for more articles:
Serverless on AZURE and GCP would be shared in later posts