Tutorial

Serverless from scratch

Spark

From my perspective the startup boom is done.

Why serverless?

Today you cannot build your start up from your garage nor starbucks.

You need something more solid to get clients, attract investors and create an epic about your company.

Fortunately there are more platforms to help you speed up your company and improve your product.

But again, you need to have a solid and proved product and there is when serverless technology enters in the game.

This technology will allow you to create an MVP with operations costs near to zero.

How do I start with Serverless?

We are going to build a basic scaffolding with serverless framework using Javascript getting help of docker to avoid install node in our system.

I will show you how to build you application from scratch and deploy it to AWS step by step.

Creating a serverless app from scratch

Let’s create the folder to work:

mkdir my-first-serverless

The we have to create our template setting AWS as provider:

cd my-first-serverless

docker run --rm -it \
    -v "$PWD":/usr/src/app \
    mirdrack/serverless serverless create --template aws-nodejs

This command will create a three files:

.gitignore. To make git ignore files.

handler.js. A javascript module with a simple routine.

serverless.yml. A configuration file for our application to define environment variables, external resources, map function to specific endpoints or define permissions.

Deploying fast

The biggest feature of serverless framework is the deploy, with one simple command with can deploy our application to AWS.

Update the service option in the serverless.yml file.

From:

service: aws-nodejs # NOTE: update this with your service name

To:

service: my-first-serverless

To deploy the application run:

docker run --rm -it \
     -v "$PWD":/usr/src/app \
     -e AWS_ACCESS_KEY_ID={aws_access_key_id} \
     -e AWS_SECRET_ACCESS_KEY={aws_secret_access_key} \
     -e AWS_REGION=us-east-1 \ 
     mirdrack/serverless serverless deploy --stage dev

We are spin-in up a container with our code and AWS values as environment variables, the framework will the these values and will deploy our application to the specified region.

And you will get an output like this:

Serverless: Packaging service…
Serverless: Excluding development dependencies…
Serverless: Creating Stack…
Serverless: Checking Stack create progress…
 …..
Serverless: Stack create finished…
Serverless: Uploading CloudFormation file to S3…
Serverless: Uploading artifacts…
Serverless: Uploading service my-first-serverless.zip file to S3 (386 B)…
Serverless: Validating template…
Serverless: Updating Stack…
Serverless: Checking Stack update progress…
 ……………
Serverless: Stack update finished…
Service Information
service: my-first-serverless
stage: dev
region: us-east-1
stack: my-first-serverless-dev
resources: 5
api keys:
   None
endpoints:
   None
functions:
   hello: my-first-serverless-dev-hello
layers:
   None
Serverless Enterprise: Run serverless login and deploy again to explore, monitor, secure your serverless project for free.

That’s it! You just deployed a serverless application to AWS!

The problem is your application is not accessible so we are going to create and endpoint to hit your new development.

The API Gateway

API Gateway is an AWS resource that allows connect the outside world with our system.

The lambdas resides in an unreachable place so we have to map them with the API Gateway.

This resource has more features but for now we will just focus on the basics.

Add the next bold lines to the serverless.yml file:

functions:
hello:
handler: handler.hello
events:
- http:
path: v1/hello
method: get

As you can perceive we are tying your function to an event.

This event will be an http get request in the path v1/hello, when the API Gateway receives that event it will trigger your function.

Redeploy your app like the first time and you will get:

Service Information
service: my-first-serverless
stage: dev
region: us-east-1
stack: my-first-serverless-dev
resources: 11
api keys:
None
endpoints:
GET - https://zodq7toxu4.execute-api.us-east-1.amazonaws.com/dev/v1/hello

functions:
hello: my-first-serverless-dev-hello
layers:
None

The output is very similar but you will get some new lines(bold), a list of the endpoints that your API will listen

Serverless framework will map your function to the specified endpoint you set due to this, you will be able to hit your endpoint.

If you query the endpoint in your browser or through a curl command you will get a http response with a message and the event object expressed in json.

You can customize the body of your response and return html, xml, more json, it’s up to you but think about this:

You have an endpoint listen the internet and returning a customizable response without a server.

And if you go to the pricing section in AWS you will notice unless you get 1 million of request is going to be zero

This is a perfect scenario for your startup if you want a MVP and prove the viability of your idea.

Next steps

You just build a serverless application from scratch that does nothing but you got the idea.

In the next chapters of this tutorial I will teach you more about:

  • Run your project in local.
  • Include more code and distribute the code of your application.
  • Add a third party package.
  • How to create a custom domain.

So stay tuned and let me know if you think we can add something to the list.

If you want to check the repository is going to be in Github.

Photo by Jamie Street on Unsplash

Share this entry: