Amazon Web Services provide numerous services on their infrastructure to build your own application.
Right from providing computing platform to storage to various Developer tools, it provide everything that helps you to build your own application.
The pricing is also very reasonable that is the best part and as an individual, most of thing that are needed to build application is free of cost for the first year and then Amazon charges very low rates for the year after.
Create an account with Amazon Web Services and note it will require credit Card information for the same.
Once the Account is create, Login to the AWS Management Console.
Starting from setting up the server to coding or uploading the code,,,,,,everything is provided by AWS.
While for big website, we will never need a single server and we all need multiple server and that too in up and running position.
The biggest disadvantage of this task is managing all the server, updating the operating system, clearing the log, cache and maintaining it 24X7, so that it is up and running.
This task is omitted if we use Amazon Web Service for hosting our website.
AWS Lambda is the service that manages multiple server in Amazon Web Services.
The maintenance can be said as clearing the log, cache, maintaining the operating system updated and many more task.
API GateWay
API Gateway is the service that is present in AWS for API creation and uploading the already created API.We can create all type of API by Amazon Web Services.
WHY Amazon Web Services
Microsoft Azure, Google Cloud Platform are also the platform that are present other than Amazon Web Services, but AWS is most popular because
- Market Leader
- Aggressive Pricing
- Large number of Serverless Services
- Rapid innovation and Rapid inclusion of new Feature/Services
- S3 is simple storage service that helps to cater the front end of the application and it has simple static pages and other scripts pages.
- API Gateway is the service that helps us to create API and deploy them on the server which will help us the to render data from database or the insert the data into database.
- AWS Lambda is the service execute code on demand.
- DynamoDB is the service that provide database related help while creating
- Cognito is the service that provide the user to authenticate the user
- Route 53 allow us to configure our own domain, i.e. create our own choice of url
- CloudFront is the service that provide us to improve the performance of our website by storing cache.
API keys in API Gateway is the unique number by which developers are identified.
API keys are used by AWS to identify the user and limit them with the access or provide more access if they are paid for the service.
By creating API on AWS, we have multiple option of giving the domain name to API so that default AWS URL that is generated should not be used, although we can use that also.
Also we have option to give the permission to our create API that how much has to be used, how much hit by client is acceptable, regarding publishing of log file and various other stuff.
We have to write every piece of code in Resource Section.
Stages are the one we can say as environment where deployment has to be done, for example QA, primary in QA, development, pre-production and other.
Authorizers allows us to add authentication to our API, the users who can use this API
Models define to shape of the data which we are using in this API. The type of data which is coming in request and data which is going through response.
Documentation is a place where other developer will get to know that what this API is all about, how it will be used, parameter that are passed and response type.
Binary Support will come into play if we are using files with the API. Files can be of any type and it will come with the request or else it is going with the response.
Dashboard is the place where we get the high level overview i.e. number of usage of the API, number of hits and many other things.
First box is the Method Request which states that how the API must look like, whether this
Particular API is meeting the criteria that are prescribed, the criteria can be anything, parameter or data model or criteria for schema specification
Also we can do the authentication for the API so that only valid users are able to do the usage of this API.We can say Method Request is like gatekeeper who keep an eye on the data and their validity.
If the authentication is not met, we simply block the rest of the work that has to be performed.
Second box is Integration Request is used to map the data according to the way we want to use it.Also we can hardcode the data according to the use.The transformation of data can be of any type for instance, authentication data, header data, parameter data and many other.
The last box is Method Response is the last response box and it will pass all the response whether it is pass or fail and we can give the final touch of how data has to be rendered in front of client.
The third cyclic box is Integration Response and is used to extract the data from the integration request and pass it to method response and here only success response are passed.Also we can transform the data according to the need of client and send it to method response. Integration Response can also be useful in allowing the access control allow for header data type, methods which this particular API can handle and parameter datatype.
There are two places where we can change the request and response.
In Integration request, we can change the parameter that are passed by client and
In Integration response, we can modify the response that our function processed, irrespective of the result from Integration Response.
There are four different type of function that can be attached to an AWS API Gateway, which are
- AWS Service-Call another AWS service that is already created.
- Mock-We don't do anything except passing some mock data and returning the random hardcoded response.
- Lambda-Which we will learn in coming module
- HTTP-It is a function or API that is created on some other server and we will call the same by passing the url.
Cross Origin Resource Sharing(CORS)
It can be understood that data present on one server cannot be accessed from another server.
So while creating an API, we come across various use cases where we have to get the data from another API and use this data.So if we check the CORS checkbox, we can access other API as well which is on some other server.
While creating resource in API from AWS, we come across the checkbox CORS, so we usually check this checkbox.
While sending PUT,POST and DELETE request in chrome browser, chrome doesn't only send the core request but it also send the pre flight request which check that whether the API request should be send, check whether the parameter are correct and many other things.
Pre flight request is the extra request to your API.
The CORS checkbox in AWS API Gateway will also the pre flight request that are being send by chrome browser and it will handle the pre flight request very seriously.
For more AWS API Gateway
Creating a First API in API Gateway in AWS
AWS Lambda
Whenever we create our first APi, we have to create an Lambda function
AWS Lambda is a service that allow us to host our code on it and run it on a certain event.
Please get a clear picture of a above diagram to understand Lambda
Suppose there is a event trigger for instance S3 event, like upload a file, now this request of file can be request any operation on that particular file.
Suppose it compress the file which is the input.So the code to compress the input file is written in AWS Lambda and it can be written in any of the four programming language, NodeJS, Python, Java or C sharp.
and once the processing code is executed, the result can be anything, for example, returning that file, or interact with other AWS services, or mail that file to used, anything or everything,
THIS IS HOW LAMBDA WORKS.
Above diagram shows, that AWS API Gateway trigger multiple function and for the respective API ,there is a separate Lambda function.
How to create AWS Lambda Function
After the API is created, its time to deploy the API,
Deploy the Created API by adding the AWS Lambda function.
Once Deployed, please change the POST method permissions so that we can access it outside the AWS website.
and the change of permission is Adding access allow control header variable that comes with Options but it is not present in POST request.
So add this variable in Fourth box i.e. Integration Response and add * in third box i.e. Integration Request.
Logs of AWS Lambda are present in CloudWatch Service of AWS.
How To add dummy Response to API Gateway that has integration type of Mock?
- This can be done from creating a API with Mock integration and then Going to INTEGRATION RESPONSE, click on the first dropdown of first row and then it will expand , go to BODY MAPPING TEMPLATE, select application/json and in the notepad, hardcode the response.
Now once we have deployed the API, adding the lambda function,
ideal condition would be that it return the response that is there in Lambda function,
but still we have two place where we can change the response thing.
Remember, The two boxes, second box INTEGRATION REQUEST and third box INTEGRATION RESPONSE.
In INTEGRATION REQUEST , under
BODY MAPPING TEMPLATE, we can set the template as application/json and hardcode the value in json format and then we can see the response of lambda according to us.
Similarly in
INTEGRATION RESPONSE, we can also, change the data which we are getting from Lambda function through Body mapping template.Click on Body Mapping Template and just see the things that can be looked upon.