Introduction to Serverless - Part 1
What is Serverless?
Serverless is a framework to help develop and deploy AWS lambda functions along with lambda event resources. It manages code and also infrastructure.
Pre-requisites
- Node.js
- AWS account (or) any cloud account
Functions
- A function is a lambda function, it may be Nodejs function or Java methods like that.
- It is independent deployment and execution like microservice.
- This function will be deployed in the cloud.
- Examples of functions
- Saving users to the database
- Upload file to s3
- Add job to Queue
- Process schedulers
Events
- Events is an AWS resource it triggers lambda function.
- Examples of events
- AWS API Gateway HTTP endpoint request
- AWS S3 bucket upload
- AWS DynamoDB table updates
Resources
- Resources are AWS resources used by the lambda function.
- Examples of resources
- AWS DynamoDB Table
- AWS S3 bucket
- AWS SNS Topic
Services
- A service is a collection of lambda functions along with resources.
- Service is like a project file, it contains all lambda function names and resource configurations.
- The service file name should be serverless.yml or serverless.json or serverless.js.
- Sample service file
service: first-app
provider:
name: aws
runtime: nodejs10.x
stage: dev
region: us-east-1
functions:
hello:
handler: handler.hello
events:
- http:
path: getData
method: GET
- http:
path: postData
method: POST
# you can add CloudFormation resource templates here
resources:
Resources:
UploadFiles:
Type: AWS::S3::Bucket
Properties:
BucketName: 3nath-bucket
TableCreation:
Type: AWS::DynamoDB::Table
Properties:
TableName: ${self:service}-users-table
AttributeDefinitions:
- AttributeName: userId
AttributeType: S
- AttributeName: userName
AttributeType: S
KeySchema:
- AttributeName: userId
KeyType: HASH
- AttributeName: userName
KeyType: RANGE
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1