What is microservices?
Microservices is a synonym for Service Oriented Architectural (SOA), Microservice architecture, or simply microservices is a distinctive method of developing software systems that try to focus on building single-function modules with well-defined interfaces and operations. The trend has grown popular in recent years as Enterprises look to become more Agile and move towards a DevOps and continuous testing. Microservices are very useful for agile system developers.
Why you need to use microservices
- Easy Deployment – We can deploy in pieces without affecting other running services
- Very Easy To Understand – Functions are less dependent on others
- High Reusability With Different Business Logic – You can share small services like payment or user login systems and much more
- Easily Defect Isolation – When service goes down isolate it faster with microservices.
- Lower Risk Of Change – Avoid locking in technologies or languages and change it without any risk.
When developing microservices with java, you can use several frameworks like Spring Boot, Jersey, Dropwizard, Play Framework, and Restlet.
In this tutorial, we will implement an authentication microservice using AWS Lamda.
Require knowledge of javascript, npm, node, serverless, webpack to implement microservice using lambda
To make users service you need to create below directory structure. Check the below screenshot. In that handler.js, serverless.yml, package.json, user.js file is important.
First, create a package.json file and write below code and save it.
After creating the package.json file you need to open a terminal and run npm install command. It will create node_modules folder and also install serverless
Generate serverless.yml and write the below code. This uses for config routing, access methods like ( GET, POST, OPTION, etc.) and parameters, etc. template parameter is using to define which parameter is going to pass in service. And which is required.
service: login # NOTE: update this with your service name #You can pin your service to only deploy with a specific Serverless version #Check out our docs for more details #frameworkVersion: "=X.X.X" provider: name: aws runtime: nodejs6.10 stage: dev region: us-east-1 iamRoleStatements: - Effect: "Allow" Action: - "ec2:CreateNetworkInterface" - "ec2:DescribeNetworkInterfaces" - "ec2:DeleteNetworkInterface" - "lambda:InvokeFunction" Resource: "*" memorySize: 1536 vpc: securityGroupIds: - sg-CODE subnetIds: - subnet-CODE timeout: 30 plugins: - serverless-offline - serverless-webpack custom: webpackIncludeModules: true functions: login: handler: handler.login events: - http: path: login/ method: GET cors: true integration: lambda request: template: application/json: | { "customerEmail":"$input.params('customerEmail')", "customerPassword":"$input.params('customerPassword')", "stage":"$context.stage" } parameters: headers: Authorization: false query: customerEmail: false customerPassword: false custom: serverless-offline: port: 3000Generate user.js file. It uses to write business logic.
module.exports = {
login: login
};
function login(event, context, callback) {
var mail = JSON.stringify(event.customerEmail);
var pass = JSON.stringify(event.customerPassword);
query.execute(`LOCAL`, 'SELECT * FROM users WHERE email=' + mail + 'AND password =' + pass + ' COLLATE utf8_bin', function (err, results) {
if (err) {
sentry.onError();
context.succeed(err);
} else {
context.succeed(results);
}
})
};
Service calls handler.js file by default. Let’s look into this.
var user = require('./user');
module.exports = {
login: login
};
function login(event, context, callback) {
user.login(event, context, callback);
}