A serverless architecture is a way to implement and run applications and services or micro-services without need to manage infrastructure. Your application still runs on servers, but all the servers management is done by AWS. Now we don’t need to provision, scale or maintain servers to run our applications, databases and storage systems. Services which are developed by developers who don’t let developers build application from scratch. Show
Why Serverless
Services For ServerLess For implementing serverless architecture there are multiple services which are provided by cloud partners though we will be exploring most of the services from AWS. Following are the services which we can use depending on the application requirement.
Three-tier Serverless ArchitectureSo, let’s take a use case in which you want to develop a three tier serverless application. The three tier architecture is a popular pattern for user facing applications, The tiers that comprise the architecture include the presentation tier, the logic tier and the data tier. The presentation tier represents the component that users directly interact with web page / mobile app UI. The logic tier contains the code required to translate user action at the presentation tier to the functionality that drives the application’s behaviour. The data tier consists of your storage media (databases, file systems, object stores) that holds the data relevant to the application. Figure shows the simple three-tier application. Presentation TierThe presentation tier of the three tier represents the View part of the application. Here you can use S3 to host static website. On a static website, individual web pages include static content and they also contain client side scripting. The following is a quick procedure to configure an Amazon S3 bucket for static website hosting in the S3 console. To configure an S3 bucket for static website hosting1. Log in to the AWS Management Console and open
the S3 console at 2. In the Bucket name list, choose the name of the bucket that you want to enable static website hosting for. 3. Choose Properties. 4. Choose Static Website Hosting Once you enable your bucket for static website hosting, browsers can access all of your content through the Amazon S3 website endpoint for your bucket. 5. Choose Use this bucket to host. A. For Index Document, type the name of your index document, which is typically named index.html. When you configure a S3 bucket for website hosting, you must specify an index document, which will be returned by S3 when requests are made to the root domain or any of the subfolders. B. (Optional) For 4XX errors, you can optionally provide your own custom error document that provides additional guidance for your users. Type the name of the file that contains the custom error document. If an error occurs, S3 returns an error document. C. (Optional) If you want to give advanced redirection rules, In the edit redirection rule text box, you have to XML to describe the rule.
CODE: https://gist.github.com/velotiotech/eb493ae4142dfeaf5f30929cd82a844c.js 6. Choose Save 7. Add a bucket policy to the website bucket that grants access to the object in the S3 bucket for everyone. You must make the objects that you want to serve publicly readable, when you configure a S3 bucket as a website. To do so, you write a bucket policy that grants everyone S3:GetObject permission. The following bucket policy grants everyone access to the objects in the example-bucket bucket. CODE: https://gist.github.com/velotiotech/e0d94858d2fe76ad6fac5e82e8617d4b.js Note: If you choose Disable Website Hosting, S3 removes the website configuration from the bucket, so that the bucket no longer accessible from the website endpoint, but the bucket is still available at the REST endpoint. Logic TierThe logic tier represents the brains of the application. Here the two core services for serverless will be used i.e. API Gateway and Lambda to form your logic tier can be so revolutionary. The feature of the 2 services allow you to build a serverless production application which is highly scalable, available and secure. Your application could use number of servers, however by leveraging this pattern you do not have to manage a single one. In addition, by using these managed services together you get following benefits:
API GatewayAPI Gateway is a fully managed service for defining, deploying and maintaining APIs. Anyone can integrate with the APIs using standard HTTPS requests. However, it has specific features and qualities that result it being an edge for your logic tier. Integration with LambdaAPI Gateway gives your application a simple way to leverage the innovation of AWS lambda directly (HTTPS Requests). API Gateway forms the bridge that connects your presentation tier and the functions you write in Lambda. After defining the client / server relationship using your API, the contents of the client’s HTTPS requests are passed to Lambda function for execution. The content include request metadata, request headers and the request body. API Performance Across the GlobeEach deployment of API Gateway includes an Amazon CloudFront distribution under the covers. Amazon CloudFront is a content delivery web service that used Amazon’s global network of edge locations as connection points for clients integrating with API. This helps drive down the total response time latency of your API. Through its use of multiple edge locations across the world, Amazon CloudFront also provides you capabilities to combat distributed denial of service (DDoS) attack scenarios. You can improve the performance of specific API requests by using API Gateway to store responses in an optional in-memory cache. This not only provides performance benefits for repeated API requests, but is also reduces backend executions, which can reduce overall cost. Let’s dive into each step 1. Create Lambda Function A.Choose first option “Author from scratch” As your function is ready, you can see your basic function will get generated in language you choose to write. CODE: https://gist.github.com/velotiotech/305c137414f410b1a0ed7180bf063a9b.js 2. Testing Lambda Function Click on “Test” button at the top right corner where we need to configure test event. As we are not sending any events, just give event a name, for example, “Hello World” template as it is and “Create” it. Now, when you hit the “Test” button again, it runs through testing the function we created earlier and returns the configured value. Create & Configure API Gateway connecting to Lambda We are done with creating lambda functions but how to invoke function from outside world ? We need endpoint, right ? Go to API Gateway & click on “Get Started” and agree on creating an Example API but we will not use that API we will create “New API”. Give it a name by keeping “Endpoint Type” regional for now. Create the API and you will go on the page “resources” page of the created API Gateway. Go through the following steps: A. Click on the “Actions”, then click on “Create Method”. Select Get method for our function. Then, “Tick Mark” on the right side of “GET” to set it up. Now, to get endpoint. We need to deploy the API. On the Actions dropdown, click on Deploy API under API Actions. Fill in the details of deployment and hit Deploy. After that, we will get our HTTPS endpoint. On the above screen you can see the things like cache settings, throttling, logging which can be configured. Save the changes and browse the invoke URL from which we will get the response which was earlier getting from Lambda. So, here is our logic tier of serverless application is to be done. Data TierBy using Lambda as your logic tier, you have a number of data storage options for your data tier. These options fall into broad categories: Amazon VPC hosted data stores and IAM-enabled data stores. Lambda has the ability to integrate with both securely. Amazon VPC Hosted Data Stores
IAM-Enabled Data Stores
You can use any of those for storage purpose, But DynamoDB is one of best suited for ServerLess application. Why DynamoDB ?
Following is the sample script for DynamoDB with Python which you can use with lambda. CODE: https://gist.github.com/velotiotech/834c0e6b7d66c27c918074676e0b4808.js Note: To run the above script successfully you need to attach policy to your role for lambda. So in this case you need to attach policy for DynamoDB operations to take place & for CloudWatch if required to store your logs. Following is the policy which you can attach to your role for DB executions. CODE: https://gist.github.com/velotiotech/a8da01656bf9cf6c73db3ab22641cb67.js Sample Architecture PatternsYou can implement the following popular architecture patterns using API Gateway & Lambda as your logic tier, Amazon S3 for presentation tier, DynamoDB as your data tier. For each example, we will only use AWS Service that do not require users to manage their own infrastructure. Mobile Backend1. Presentation Tier: A mobile application running on each user’s smartphone. 2. Logic Tier: API Gateway & Lambda. The logic tier is globally distributed by the Amazon CloudFront distribution created as part of each API Gateway each API. A set of lambda functions can be specific to user / device identity management and authentication & managed by Amazon Cognito, which provides integration with IAM for temporary user access credentials as well as with popular third party identity providers. Other Lambda functions can define core business logic for your Mobile Back End. 3. Data Tier: The various data storage services can be leveraged as needed; options are given above in data tier. Amazon S3 Hosted Website1. Presentation Tier: Static website content hosted on S3, distributed by Amazon CLoudFront. Hosting static website content on S3 is a cost effective alternative to hosting content on server-based infrastructure. However, for a website to contain rich feature, the static content often must integrate with a dynamic back end. 2. Logic Tier: API Gateway & Lambda, static web content hosted in S3 can directly integrate with API Gateway, which can be CORS complaint. 3. Data Tier: The various data storage services can be leveraged based on your requirement. ServerLess CostingAt the top of the AWS invoice, we can see the total costing of AWS Services. The bill was processed for 2.1 million API request & all of the infrastructure required to support them. Following is the list of services with their costing. Note: You can get your costing done from AWS Calculator using following links;
ConclusionThe three-tier architecture pattern encourages the best practice of creating application component that are easy to maintain, develop, decoupled & scalable. Serverless Application services varies based on the requirements over development. Which of the following AWS services is serverless?AWS Fargate is a serverless compute engine that works with Amazon Elastic Container Service (ECS) and Amazon Elastic Kubernetes Service (EKS).
Which AWS services should company use to create serverless workflow?AWS Lambda is a serverless compute service that is well suited to event-driven architectures.
What are serverless applications in AWS?A serverless architecture is a way to build and run applications and services without having to manage infrastructure. Your application still runs on servers, but all the server management is done by AWS.
Which AWS resources are serverless?AWS — Serverless services on AWS. AWS Lambda. AWS Lambda lets you run code without provisioning or managing servers. ... . Amazon API Gateway. ... . Amazon DynamoDB. ... . Amazon S3. ... . Amazon Kinesis. ... . Amazon Aurora. ... . AWS Fargate. ... . Amazon SNS.. |