4.22 Build A Serverless Webpage Using API Gateway & Lambda
Last updated
Was this helpful?
Last updated
Was this helpful?
Go to Lambda, create function.
Create a function name.
Create a role name, this will allow the Lambda function to execute itself.
For the Policy Templates, you can choose the Simple Microservice permissions, this is the most common and basic one.
Go to Lambda, Functions, you can see the functions you created. When you update your function, you can click Save to save your function. For each function:
Configuration tab
You can create your function code here. The Handler should be "index.function-name-you-want-to-trigger-in-your-code", for example, in the "index.py" file you have a function called "func", so the handler should be "index.func", and this is the way we are going to execute our function.
Environment variables. These variables are key-value pairs that are accessible from your function code. They are useful to store configuration settings without the need to change function code.
Tags
Execution role
Basic Settings. You can set how much memory your function code can use and how long the function code execution timeout duration is. The maximum timeout duration is 5 minutes, which is same with the longest time a Lambda function can run.
Triggers
Depend on your region, the potential triggers can vary. In exam, you need to remember what kinds of triggers are there. North-Virginia region has all kinds of triggers.
In this lab, we use API Gateway as the trigger to trigger our Lambda function.
You can set the security of the API to be "Open", which means your API endpoint will be publicly available and can be invoked by all users.
Click create, and then a new API Gateway trigger will be created. Then you can see the ARN (Amazon Resources Name) of the API Gateway trigger.
Click the name of the API Gateway and go into API Gateway to see more details. The API Gateway can be seen as a proxy between your lambda function and the clients browser. The API Gateway actually can be seen as a router (just like the functionality of @RequestMapping annotation), and you can create multiple Methods in it, and each method could be used to handle POST or GET request (just like the functionality of @GetMapping or PostMapping annotation). API Gateway can route the requests to your particular Lambda function. The router in URL could be /lambda_function_name. The integration type could determine where you would like to pass the request to (Lambda function, HTTP, Mock (mock a server response) or AWS Service).
Use Lambda Proxy Integration, you can select the lambda region to be your current region, and then type in your lambda function name.
You can control the requests type to your API Gateway. You can delete any type of method in "Action", and create new method e.g. GET method, so that you can allow only GET method to arrive to your API Gateway.
Go to "Action" and click "Deploy API". You can choose Deployment stage to be "prod", and give this deployment a description. You can visit the lambda function through the API Gateway and see the response using the invoke URL of this API.
Go to S3. Choose one of your S3 buckets. Copy the invoke URL of your API Gateway to the landing page "index.html", so that the request can be sent to the API Gateway and then API Gateway will route the requests to particular Lambda functions. (Lab files can be find on Udemy). Upload landing page "index.html" to the bucket, and don't forget to set the permission of the object to be public (you can also set the permission of the bucket so that all objects in this bucket can be public). After you upload it, click it and visit the "link" of the "index.html" (You may also build a static website using S3 here to implement this step). For now, the request will be sent to API Gateway, and then routed to Lambda function, and the function code will return a response to API Gateway, and then API Gateway will return the response to "index.html" which is located in S3 and update it, so then the user can see the changes.
Go to Route53. We will use Route53 to redirect DNS requests for our DNS name. In other words, users can visit "index.html" through a simpler URL instead of the link of your S3 bucket object "index.html", and then Route53 will redirect the request to the object "index.html".
Register Domain. Firstly you need to choose a domain name. Different suffix will cost you differently.
When you registered a domain, you will see it in your hosted zones. Click your registered domain, and Create Record Set. Select Alias "Yes", and you should remember that if you want to set the Alias Target to be the S3 bucket which contains your "index.html" object, the bucket name must be same as your domain name which is just configured in previous step.
Vendor takes care of provisioning and management of servers.
Vendor is responsible for capacity provisioning and automated scaling. We no longer have to worry about our auto-scaling group configurations, API Gateway and Lambda will scale automatically. You don't have to worry about peak performance
Moving away from servers and infrastructure concerns should be your goal.
"No server is easier to manage than no server." -- Werner Vogels.
Serverless Computing: Lambda
Serverless API Proxy: API Gateway
Serverless Storage: S3
Serverless Database: DynamoDB, Aurora