Serverless Architecture using Serverless Framework and AWS Lambda

Serverless Architecture

The aim of this document is to setup Serverless Architecture to implement API end points and/or Lambda functions using Serverless Framework and AWS.

Requirements:

  1. Create a microservice/script using Serverless Framework
  2. Implement End points or Single Function
  3. Test locally
  4. Deploy it so that it works as AWS Lambda
  5. It should be possible to use external modules in it

Here are the steps to achieve the requirements in best possible way.

Install Serverless framework:

  1. We will use a great Framework for this purpose: https://www.serverless.com/
  2. Install it in local system globally using command: 
    npm i -g serverless

Create IAM user for Serverless Framework:

  1. To make serverless framework deploy on AWS, you need to Create an IAM User in your AWS Console
  2. Name the user anything like “serverless-user”
  3. Select Programming Access
  4. Attach existing policies directly and choose: AdministratorAccess
  5. Copy the Access Key ID and Secret Access Key.
  6. Enter IAM keys in the Serverless configuration using command:
    serverless config credentials — provider aws — key xxxxxxxxxxxxxx — secret xxxxxxxxxxxxxx

Create a new Microservice in serverless framework:

Now we are ready to code, we are working with nodejs, so we will use this command:
serverless create — template aws-nodejs — path LambdaTransformator

In this we have assumed function name as “LambdaTransformator”, you can name it anything you like.

Configure your Microservice using serverless.yml:

  1. Add/Uncomment “stage” and “region” in it
  2. Change the name of the function and file/handler
  3. Add events with http
  4. Add the serverless-offline in plugins section

Installing dependencies required by the project:

  1. This step will vary as per your project requirements
  2. Get into the project folder and then run following commands:
     npm init -y
     npm i — save mongodb serverless-http
     npm i — save-dev serverless-offline

Create project structure:

  1. In package.json, Change the name of the “main” to app.js or index.js (assuming this is the main file in your project)
  2. Create index.js and write your code in it!

Test Locally:

  1. One of the best feature of Serverless Framework is that you can test your code locally, to do this write this line in package.json inside “scripts” section:
    “offline”: “sls offline start — skipCacheInvalidation”
  2. Then run this command to run your code locally:
    npm run offline
    Note: If this does not work, in the root directory of your project run sls and you should see a list of commands. If you configured it correctly, sls offline and sls offline start should be available.
    Also make sure you have added plugin: serverless-offline
  3. To test the code, go to in Postman. We can call the API end point using URL: http://127.0.0.1:3000/

Deploy the code on AWS:

  1. If everything works great, you can deploy the function on AWS using following command:
    sls deploy
  2. By default when you deploy, API Gateway is added as trigger and Execution role is automatically created. You can go to API Gateway to check the API URL.
  3. Now test by hitting API End point and check CloudWatch logs.

That’s’ it! Now when you want to deploy new API on AWS or new Lambda function on AWS, you can use Serverless Framework and use steps mentioned above, and following things will happen:

  1. You can test the code locally.
  2. You can deploy in a single command and all the necessary things like API Gateway configuration, Role creation, Lambda function creation will be done automatically.
  3. All the external modules are copied over as well.

As you can see these things match the Requirements mentioned earlier in the document.