Рет қаралды 18,993
In this video, you will learn
1. Serverless Web Application in AWS
2. We will use S3, DynomoDB, API Gateway and AWS Lambda to make our site serverless.
What we are going to learn in this video?
1. We will host a static web site in the S3 bucket.
2. Place HTML form on the site.
3. Using the post method, request send to API gateway.
4. Trigger the lambda function using the API gateway.
5. Store the data in DynamoDB.
How to host a static website in s3 Bucket?
• AWS Tutorial - Hosting...
On click on the submit button we are going to submit the name, email, and message to DynamoDB from lambda which will be trigger using API Gateway. So that the required URL will provide us by the API Gateway which we will learn in a few minutes.
Using the XMLHTTP request, the data will send.
Open the AWS console page.
Let’s check the s3 bucket first of all.
This is index.html file where are will replace after we generate the URL.
Open DynomoDB and create table.
Specify the table name.
Another we need to specify the partition key which will behave as the primary key. We have the only name, email, and message data. This is only for the demo so let’s put email id as a primary key.
Let’s create the lambda function. Navigate to Create function.
Author from scratch ok
Function name - Give the lambda function name that describes the purpose of your function.
Lok_lambda_function
As you can see there are different options for the runtime. We are going through Python.
Here we need to assign the IAM role to the Lambda function to have access to DynamoDB and API Gateway.
Create a new role for it.
Give a role name:
Lok_lamnda@role
This lambda role will have DynamoDB full access and API gateway administrator role.
Click the create role.
Check the use an existing role. Select the lambda role which we have just created.
Click Create function.
In the left, API gateway triggers the lambda and lambda function to store the data in DynamoDB.
Now let’s move to API Gate which will trigger the lambda.
Open API gateway.
In the Rest API section, navigate to the build button.
Rest protocol ok
Check new API
Give an API name
Lok_api
Description not compulsory
Endpoint type regional fine.
Click create API
In the Resources section, navigate to Action. From the drop-down section, click create the resource.
Provide the resource name. We are creating for contact us section so let’s name as contact us
We are going to call this API using the post method so let’s add the method. Again navigate to action dropdown and click create method.
Select the Post method and click in tick mark.
Integration type lambda function ok
In lambda function, type and choose the lambda function and save it.
Ok from client, method request then integrates request to lambda function and again integrates response to method response and finally to the client get a response.
Here we need the URL to use this API gateway. Still we didn’t get it.
For that, we need to deploy the API to get the required URL. So navigate to the action menu selecting the needed resources. From the dropdown, click in deploy API.
Here in which stage, would you like to deploy the api? New stage. Dev for now.
Click Deploy.
Copy the full URL, open the code. in API paste the URL and save the file.
API Gateway triggers the lambda function and lambda function store the data into DynamoDB which are going to do now.
Here you can see the default python code. We are going to place the python code which will store the data to the DynamoDB table.
I have already written the required codes.
import json
import boto3
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('tablename')
def lambda_handler(event, context):
print (event)
name = event['name']
email = event['email']
message = event['message']
table.put_item(
Item={
'name': name,
'email':email,
'message':message,
}
)
return 'added record successfully'
Let’s upload the new index file.
Now time to test it.
Added record successfully
We upload the HTML form in the s3 bucket. And using the XML HTTP request, we send data to API gateway using URL generated from API gateway. API gateway triggers the lambda function. Using python coding, the lambda function inserts the data to the DynamoDB table.
In this way we can use the serverless architecture in AWS using s3, API gateway, lambda, and DynamoDB.
Host static site in S3 Bucket:
• AWS Tutorial - Hosting...
SUBSCRIBE to our KZfaq channel for more videos:
www.youtube.co...
Like us on Facebook: / master2teach
For more content go to master2teach.com/
E-mail: master2teach@gmail.com