Thanks! We'll be in touch in the next 12 hours
Oops! Something went wrong while submitting the form.

Building Your First AWS Serverless Application? Here’s Everything You Need to Know

Hitesh Panjawani

Full-stack Development

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.

Why Serverless

  1. More focus on development rather than managing servers.
  2. Cost Effective.
  3. Application which scales automatically.
  4. Quick application setup.

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.

  1. Lambda: It is used to write business logic / schedulers / functions.
  2. S3: It is mostly used for storing objects but it also gives the privilege to host WebApps. You can host a static website on S3.
  3. API Gateway: It is used for creating, publishing, maintaining, monitoring and securing REST and WebSocket APIs at any scale.
  4. Cognito: It provides authentication, authorization & user management for your web and mobile apps. Your users can sign in directly sign in with a username and password or through third parties such as Facebook, Amazon or Google.
  5. DynamoDB: It is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability.

Three-tier Serverless Architecture

So, 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.

Simple Three-Tier Architecture
 Figure: Simple Three-Tier Architectural Pattern

Presentation Tier

The 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 hosting

1. 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.

Bucket for Static Web Hosting

3. Choose Properties.

Properties for Static Web Hosting

4. Choose Static Website Hosting

Static Web 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.

Static Web Hosting

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.
E.g.

CODE: https://gist.github.com/velotiotech/eb493ae4142dfeaf5f30929cd82a844c.js

Static Web Hosting Rules

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 Tier

The 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:

  1. No operating system to choose, secure or manage.
  2. No servers to right size, monitor.
  3. No risk to your cost by over-provisioning.
  4. No Risk to your performance by under-provisioning.

API Gateway

API 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 Lambda

API 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 Globe

Each 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
Login to Aws Console and head over to Lambda Service and Click on “Create A Function”

AWS Lambda

A. Choose first option “Author from scratch”
B. Enter Function Name
C. Select Runtime e.g. Python 2.7
D. Click on “Create Function”

As your function is ready, you can see your basic function will get generated in language you choose to write.
E.g.

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.

Testing Lambda Function

Now, when you hit the “Test” button again, it runs through testing the function we created earlier and returns the configured value.

Execution Result

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.

Creating API Gateway

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.
B. Choose “Lambda Function” as integration type.
C. Choose the region where we created earlier.
D. Write the name of Lambda Function we created
E. Save the method where it will ask you for confirmation of “Add Permission to Lambda Function”. Agree to that & that is done.
F. Now, we can test our setup. Click on “Test” to run API. It should give the response text we had on the lambda test screen.

Lambda Test Screen

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.

AWS Lambda Deployment

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 Tier

By 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

  1. Amazon RDS
  2. Amazon ElasticCache
  3. Amazon Redshift

IAM-Enabled Data Stores

  1. Amazon DynamoDB
  2. Amazon S3
  3. Amazon ElasticSearch Service

You can use any of those for storage purpose, But DynamoDB is one of best suited for ServerLess application.

Why DynamoDB ?

  1. It is NoSQL DB, also that is fully managed by AWS.
  2. It provides fast & prectable performance with seamless scalability.
  3. DynamoDB lets you offload the administrative burden of operating and scaling a distributed system.
  4. It offers encryption at rest, which eliminates the operational burden and complexity involved in protecting sensitive data.
  5. You can scale up/down your tables throughput capacity without downtime/performance degradation.
  6. It provides On-Demand backups as well as enable point in time recovery for your DynamoDB tables.
  7. DynamoDB allows you to delete expired items from table automatically to help you reduce storage usage and the cost of storing data that is no longer relevant.

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 Patterns

You 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 Backend

Mobile Backend Architecture

1. 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 Website

Amazon S3 Hosted Website

1. 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 Costing

At 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.

Serverless Costing

Note: You can get your costing done from AWS Calculator using following links;

  1. https://calculator.s3.amazonaws.com/index.html
  2. AWS Pricing Calculator

Conclusion

The 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.

Get the latest engineering blogs delivered straight to your inbox.
No spam. Only expert insights.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Did you like the blog? If yes, we're sure you'll also like to work with the people who write them - our best-in-class engineering team.

We're looking for talented developers who are passionate about new emerging technologies. If that's you, get in touch with us.

Explore current openings

Building Your First AWS Serverless Application? Here’s Everything You Need to Know

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.

Why Serverless

  1. More focus on development rather than managing servers.
  2. Cost Effective.
  3. Application which scales automatically.
  4. Quick application setup.

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.

  1. Lambda: It is used to write business logic / schedulers / functions.
  2. S3: It is mostly used for storing objects but it also gives the privilege to host WebApps. You can host a static website on S3.
  3. API Gateway: It is used for creating, publishing, maintaining, monitoring and securing REST and WebSocket APIs at any scale.
  4. Cognito: It provides authentication, authorization & user management for your web and mobile apps. Your users can sign in directly sign in with a username and password or through third parties such as Facebook, Amazon or Google.
  5. DynamoDB: It is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability.

Three-tier Serverless Architecture

So, 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.

Simple Three-Tier Architecture
 Figure: Simple Three-Tier Architectural Pattern

Presentation Tier

The 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 hosting

1. 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.

Bucket for Static Web Hosting

3. Choose Properties.

Properties for Static Web Hosting

4. Choose Static Website Hosting

Static Web 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.

Static Web Hosting

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.
E.g.

CODE: https://gist.github.com/velotiotech/eb493ae4142dfeaf5f30929cd82a844c.js

Static Web Hosting Rules

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 Tier

The 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:

  1. No operating system to choose, secure or manage.
  2. No servers to right size, monitor.
  3. No risk to your cost by over-provisioning.
  4. No Risk to your performance by under-provisioning.

API Gateway

API 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 Lambda

API 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 Globe

Each 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
Login to Aws Console and head over to Lambda Service and Click on “Create A Function”

AWS Lambda

A. Choose first option “Author from scratch”
B. Enter Function Name
C. Select Runtime e.g. Python 2.7
D. Click on “Create Function”

As your function is ready, you can see your basic function will get generated in language you choose to write.
E.g.

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.

Testing Lambda Function

Now, when you hit the “Test” button again, it runs through testing the function we created earlier and returns the configured value.

Execution Result

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.

Creating API Gateway

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.
B. Choose “Lambda Function” as integration type.
C. Choose the region where we created earlier.
D. Write the name of Lambda Function we created
E. Save the method where it will ask you for confirmation of “Add Permission to Lambda Function”. Agree to that & that is done.
F. Now, we can test our setup. Click on “Test” to run API. It should give the response text we had on the lambda test screen.

Lambda Test Screen

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.

AWS Lambda Deployment

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 Tier

By 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

  1. Amazon RDS
  2. Amazon ElasticCache
  3. Amazon Redshift

IAM-Enabled Data Stores

  1. Amazon DynamoDB
  2. Amazon S3
  3. Amazon ElasticSearch Service

You can use any of those for storage purpose, But DynamoDB is one of best suited for ServerLess application.

Why DynamoDB ?

  1. It is NoSQL DB, also that is fully managed by AWS.
  2. It provides fast & prectable performance with seamless scalability.
  3. DynamoDB lets you offload the administrative burden of operating and scaling a distributed system.
  4. It offers encryption at rest, which eliminates the operational burden and complexity involved in protecting sensitive data.
  5. You can scale up/down your tables throughput capacity without downtime/performance degradation.
  6. It provides On-Demand backups as well as enable point in time recovery for your DynamoDB tables.
  7. DynamoDB allows you to delete expired items from table automatically to help you reduce storage usage and the cost of storing data that is no longer relevant.

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 Patterns

You 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 Backend

Mobile Backend Architecture

1. 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 Website

Amazon S3 Hosted Website

1. 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 Costing

At 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.

Serverless Costing

Note: You can get your costing done from AWS Calculator using following links;

  1. https://calculator.s3.amazonaws.com/index.html
  2. AWS Pricing Calculator

Conclusion

The 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.

Did you like the blog? If yes, we're sure you'll also like to work with the people who write them - our best-in-class engineering team.

We're looking for talented developers who are passionate about new emerging technologies. If that's you, get in touch with us.

Explore current openings