Serverless Architecture using Serverless Framework and AWS Lambda
The aim of this document is to setup Serverless Architecture to implement API end points and/or Lambda functions using Serverless Framework and AWS.
- Create a microservice/script using Serverless Framework
- Implement End points or Single Function
- Test locally
- Deploy it so that it works as AWS Lambda
- 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:
- We will use a great Framework for this purpose: https://www.serverless.com/
- Install it in local system globally using command:
npm i -g serverless
Create IAM user for Serverless Framework:
- To make serverless framework deploy on AWS, you need to Create an IAM User in your AWS Console
- Name the user anything like “serverless-user”
- Select Programming Access
- Attach existing policies directly and choose: AdministratorAccess
- Copy the Access Key ID and Secret Access Key.
- 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:
- Add/Uncomment “stage” and “region” in it
- Change the name of the function and file/handler
- Add events with http
- Add the serverless-offline in plugins section
Installing dependencies required by the project:
- This step will vary as per your project requirements
- 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:
- In package.json, Change the name of the “main” to app.js or index.js (assuming this is the main file in your project)
- Create index.js and write your code in it!
- 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”
- 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
- 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:
- If everything works great, you can deploy the function on AWS using following command:
- 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.
- 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:
- You can test the code locally.
- 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.
- All the external modules are copied over as well.
As you can see these things match the Requirements mentioned earlier in the document.