AWS Serverless Web Application Tutorial

by Radoslaw Fabisiak

Intro to AWS Serverless Web Application tutorial

Today we’ll go throught AWS Serverless Web Application tutorial.

Nowadays, almost every industry actively uses IT. We build web and mobile apps, manufacturing applications, e-commerce, and even software to handle factories.

Development is not an easy process, costs a lot of money, very often it is stressful, and still, it’s not all that we need to handle.

Most of our IT solutions need maintenance, and IT infrastructure to handle our software.

This point, in many cases, is very painful, creates never-ending expenses and risks.

But what if I’ll tell you the costs of IT infrastructure can be reduced even by 90%?

Would you believe if I tell, modern architecture can reduce risk as well?

We are thankful to one hero, Serverless computing.

Serverless is a relatively new thing, and not many companies are going with it daily, but a lot of them are beginning the important migration.

The scale of migration may not be that huge, like movement from monoliths to microservices, or from 100% server-side apps into front-end frameworks like Angular or React.JS.

But it is just because not everyone company knows about the enormous benefits of serverless computing.

Today I’ll give you some knowledge of that topic, I will answer the most popular questions, and I’ll tell you about popular solutions in use with Serverless.

Let’s start!

And if you prefer video, here is the youtube version:

What is Serverless Computing

Serverless computing is a FAAS(function as a service) model of back-end type service that is provided in cloud on-demand.

It means, psychically, there is still back-end in usage, but you do not need to buy the whole server, for the whole month, configure that, and be responsible for the server security.

Instead of that, you can just upload written code for your back-end and pay just for the time when functions of your code are executed.

What is Serverless Architecture

Serverless architecture is a software or architecture design pattern where we relay parts of our project on serverless computing.

In this case, we do not need to maintain the whole IT infrastructure.

We do not need to maintain servers, and databases, because in theory, we do not have them, we just use maintained solutions packed into AWS services.

We do not need to scale our projects manually, like buying more instances or migrating to the bigger ones (AWS can provide auto-scalability).

Every part of our IT infrastructure can be scalable, secure, and comfortable to maintain when we need more features, we can just develop and deploy the next service.

Is Serverless cheaper

Serverless computing works a bit differently than standard servers that we used to use for back-end.

When with regular servers, we rent the instance, and if we want to have our service available always, we need to pay for the whole month, 24h per day.

It does not matter if we use it or not.

With the serverless, we do pay only for the time when we execute our code.

If we have only a few visitors in x day, we pay only for the x minutes/hours when they use our service.

In many projects, the cost reduction can be even up to 90%.

But, still, we need to know, in some cases when we have very stable traffic, that we can precisely predict for the whole month, the serverless could be more expensive.

Anyway, it’s not so common when we have a similar number of users every minute, and most projects have peaks of traffic at some hour, so we’d need to buy a bigger server only to handle the traffic peak.

Why use Serverless

Of course, the cost is a critical metric when we plan our IT projects, but the money is not only one benefit when we talk about serverless.

Security is the next benefit that we should see when we consider this type of architecture.

Still, we have a lot to do with security, but much less when we go with standard servers.

For example, with serverless, the system’s security is on the service provider, not on us. That is a huge amount of work, we can have solved.

The next thing that I’d like to tell you about is the scalability and simplicity of deployment (about deployment I’ll tell more in the next sections).

With Serverless, we do not need to care about scalability that much, because if we have more traffic, we just have a bigger invoice to pay.

We do not need to care about buying more, bigger instances, and configure them.

What is AWS CloudFormation and why you should care about it

Do you think setting up a whole IT infrastructure for the company is difficult and requires a lot of work?

Not anymore! Meet the real hero of modern IT, the AWS CloudFormation.

AWS CloudFormation is a service that allows you to generate whole IT infrastructure (yes, all back-end, services, databased, file storage, API, and even firewalls) and even deploy there your software by a few clicks.

You can use the Serverless Framework or AWS SAM to generate the AWS CloudFormation template (just one YAML config file), where you put all the config, and that’s it.

While the infrastructure will be auto-generated for you, so you do not need to spend money and time on advanced dev-ops.

What is Serverless Framework

Serverless Framework is the open-source framework, written by Austen Collins in Node.

It helps you to template, develop, build, and deploy whole Serverless Infrastructure.

With Serverless Framework, you can build not only AWS Serverless applications but also use other service providers, like Google Cloud, Oracle, or Microsoft Azure.

To define the infrastructure with Serverless Framework, we use YAML (.yml) templates and define the structure like in the example below.

Here is an example of Node.JS GraphQL API and PostgreSQL database:

service: ${file(./keys.json):ApiName}

  name: aws
  region: us-east-1
  stage: dev
  memorySize: 256
  runtime: nodejs12.x
  role: LambdaRole
    AURORA_HOST: ${self:custom.AURORA.HOST}
    AURORA_PORT: ${self:custom.AURORA.PORT}
    MYSQL_HOST: ${self:custom.MYSQL.HOST}
    MYSQL_PORT: ${self:custom.MYSQL.PORT}
    DB_NAME: ${self:custom.DB_NAME}
    USERNAME: ${self:custom.USERNAME}
    PASSWORD: ${self:custom.PASSWORD}
  DB_NAME: graphql
  USERNAME: master
  PASSWORD: password
      Fn::GetAtt: [AuroraRDSCluster, Endpoint.Address]
      Fn::GetAtt: [AuroraRDSCluster, Endpoint.Port]
    VPC_CIDR: 10
      Fn::GetAtt: [MySqlRDSInstance, Endpoint.Address]
      Fn::GetAtt: [MySqlRDSInstance, Endpoint.Port]
      Fn::GetAtt: [PostgreSqlRDSInstance, Endpoint.Address]
      Fn::GetAtt: [PostgreSqlRDSInstance, Endpoint.Port]

  - serverless-pseudo-parameters
    LambdaRole: ${file(./resource/LambdaRole.yml)}
    ServerlessInternetGateway: ${file(./resource/ServerlessInternetGateway.yml)}
    ServerlessVPC: ${file(./resource/ServerlessVPC.yml)}
    ServerlessVPCGA: ${file(./resource/ServerlessVPCGA.yml)}
    ServerlessSubnetA: ${file(./resource/ServerlessSubnetA.yml)}
    ServerlessSubnetB: ${file(./resource/ServerlessSubnetB.yml)}
    ServerlessSubnetC: ${file(./resource/ServerlessSubnetC.yml)}
    ServerlessSubnetGroup: ${file(./resource/ServerlessSubnetGroup.yml)}
    ServerlessSecurityGroup: ${file(./resource/ServerlessSecurityGroup.yml)}
    RouteTablePublic: ${file(./resource/RouteTablePublic.yml)}
    RoutePublic: ${file(./resource/RoutePublic.yml)}
    RouteTableAssociationSubnetA: ${file(./resource/RouteTableAssociationSubnetA.yml)}
    RouteTableAssociationSubnetB: ${file(./resource/RouteTableAssociationSubnetB.yml)}
    RouteTableAssociationSubnetC: ${file(./resource/RouteTableAssociationSubnetC.yml)}

    AuroraRDSClusterParameter: ${file(./resource/AuroraRDSClusterParameter.yml)}
    AuroraRDSInstanceParameter: ${file(./resource/AuroraRDSInstanceParameter.yml)}
    AuroraRDSCluster: ${file(./resource/AuroraRDSCluster.yml)}
    AuroraRDSInstance: ${file(./resource/AuroraRDSInstance.yml)}

    MySqlRDSInstance: ${file(./resource/MySqlRDSInstance.yml)}

    PostgreSqlRDSInstance: ${file(./resource/PostgreSqlRDSInstance.yml)}
    handler: handler.server
      - http:
          path: /
          method: post
          cors: true
    handler: handler.playground
      - http:
          path: /
          method: get
          cors: true

What is AWS SAM

AWS SAM, Serverless Application Model, is a very similar tool like Serverless Framework.

Like the Serverless Framework, the Serverless Application Model is an open-source framework that can let us build the Serverless Infrastructure.

As well as the Serverless Framework, the SAM uses YAML templating system.

There are a few differences between them.

The most important is that the Serverless Framework was designed to deploy FaaS(Function as a Service), and AWS SAM was designed to help build the whole infrastructure.

Here is the SAM template example of Node.JS REST API with DynamoDB:

AWSTemplateFormatVersion: 2010-09-09
Description: >-
  Your service

Transform: AWS::Serverless-2016-10-31

    PermissionsBoundary: !Sub 'arn:${AWS::Partition}:iam::${AWS::AccountId}:policy/${AppId}-${AWS::Region}-PermissionsBoundary'

    Type: String

    Type: AWS::Serverless::Function
      CodeUri: ./
      Handler: src/handlers/get-all-items.getAllItemsHandler
      Runtime: nodejs10.x
      MemorySize: 128
      Timeout: 60
      Description: A simple example includes a HTTP get method to get all items from a DynamoDB table.
        - DynamoDBCrudPolicy:
            TableName: !Ref SampleTable
          SAMPLE_TABLE: !Ref SampleTable
          Type: Api
            Path: /
            Method: GET
    Type: AWS::Serverless::Function
      CodeUri: ./
      Handler: src/handlers/get-by-id.getByIdHandler
      Runtime: nodejs10.x
      MemorySize: 128
      Timeout: 60
      Description: A simple example includes a HTTP get method to get one item by id from a DynamoDB table.
        # Give Create/Read/Update/Delete Permissions to the SampleTable
        - DynamoDBCrudPolicy:
            TableName: !Ref SampleTable
          # Make table name accessible as environment variable from function code during execution
          SAMPLE_TABLE: !Ref SampleTable
          Type: Api
            Path: /{id}
            Method: GET
    Type: AWS::Serverless::Function
      CodeUri: ./
      Handler: src/handlers/put-item.putItemHandler
      Runtime: nodejs10.x
      MemorySize: 128
      Timeout: 60
      Description: A simple example includes a HTTP post method to add one item to a DynamoDB table.
        - DynamoDBCrudPolicy:
            TableName: !Ref SampleTable
          SAMPLE_TABLE: !Ref SampleTable
          Type: Api
            Path: /
            Method: POST
    Type: AWS::Serverless::SimpleTable
        Name: id
        Type: String
        ReadCapacityUnits: 2
        WriteCapacityUnits: 2

Cons of Serverless

We know about many benefits of the Serverless, but are there any const?

Yes, there are a few.

The first one you need to know is the initial latency (cold start), which can affect performance about the cold start.

The second one is the whole infrastructure security if many components rely on each other, you need to care about not adding security holes.

We depend on the service provider. It means our whole infrastructure is based on AWS, MS, or Google, and there are not many alternatives.

Conclusion of AWS Serverless Web Application tutorial

Congratulations, now, you know what Serverless is, and why its worth to use it!

I hope I’ve explained the most popular questions about Serverless Architecture, and you’ll be able to know if its the right way for your project or not.

If you need some advice about your future or current project’s architecture, feel free to reach me out.

I’ll be happy with the consultation.

Thanks for reading,

Radoslaw Fabisiak

If you liked it share and comment!