Connect to EC2 instance and install Serverless refer link
Install AWS CLI and configure it with the AWS console.
Create an IAM User and provide the required policies to access or provision AWS services.
Sign up for a Serverless console
We will be deploying a TaskMaster application that uses the Dynamodb table to store the content details by inserting, getting and putting/deleting**.** So let's clone the code from GitHub.
npm install
git clone https://github.com/rjthapaa/serverless-taskmaster-aws-app.git
All the source code files are in the src directory and we are exporting the code by using a module called handler so that we can use these src files outside the code
All the serverless project configurations are done in the serverless.yml file by DevOps engineers.
$ nano serverless.yml
Create a dynamodb table with partition key as Id and attach its arn in yml
org: rjthapaa
app: serverless-taskmaster-aws-app
service: serverless-taskmaster-aws-app
frameworkVersion: '3'
provider:
name: aws
runtime: nodejs14.x
region: us-east-1
iamRoleStatements:
- Effect: Allow
Action:
- dynamodb:Query
- dynamodb:Scan
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
Resource:
- arn:aws:dynamodb:us-east-1:917033291688:table/KaamKaro
functions:
hello:
handler: src/hello.handler
events:
- httpApi:
path: /
method: get
kaamBharo:
handler: src/kaamBharo.handler
events:
- httpApi:
path: /kaam
method: post
kaamDikhao:
handler: src/kaamDikhao.handler
events:
- httpApi:
path: /kaam
method: get
kaamKhatamKaro:
handler: src/kaamKhatamKaro.handler
events:
- httpApi:
path: /kaam/{id}
method: put
resources:
Resources:
KaamKaro:
Type: AWS::DynamoDB::Table
Properties:
TableName: KaamKaro
BillingMode: PAY_PER_REQUEST
AttributeDefinitions:
- AttributeName: id
AttributeType: S
KeySchema:
- AttributeName: id
KeyType: HASH
org: username of the serverless dashboard
app: Name of the application in serverless
Service: Service name of the application that we want to create
framework version: Version of the framework that we want to use for serverless
provider: Details like name of provider, runtime, region and roles
Functions: Used to create a lambda function using an object
resources: Like which kind of resources that you want for the project
Login to the Serverless using the below command and use the HTTP link to open the Serverless
sls login
click on Continue to the dashboard
# Deploy the serverless
$ sls deploy
Error: Application not found. Because we don't have any application created by name serverless-taskmaster-aws-app
Back to the Serverless dashboard and Click on create app
- Click on the existing project
- Run the following command in terminal
serverless --org=rjthapaa
- Type Yes to deploy the project
- Serverless is successfully deployed and you can check it in CloudFormation, S3 bucker, API and DynamoDB.
Click on the update > Edit template in designer > View in designer to view the layout Stack of cloud formation
S3 Bucket
API Gateway
Dynamodb
A Table named Kaamkaro is created
Logs can also be checked by navigating to Logs \> Log groups under Cloudwatch
POSTMAN (API Testing Tool)
We will be using POSTMAN UI for testing our APIS link
Signup and log in to the Postman console
Click on + create a new connection and provide the name
- Click on Add a request and give it a name as hello
Copy the GET - HTTP URL from the terminal and paste it in the box and click on send, so that we will get a response at the bottom
Use can also make use of the environment as a variables
Click on add
Provide the name for the environment and paste the HTTP URL which is common for all hello, get, post, put and click on save
now you can use {{dev-url}} in the action section and click on sent to get the response
Internal error - Go to cloud watch > kaamdikhao logs
uuid dependency module for nodejs is not found, so we should install the npm
uuid is used to generate a unique id for the table
npm install
Run the Serverless deployment again
sls deploy
Now back to POSTMAN
Create a new request as a post for POST - https://vf1jqnkncl.execute-api.us-east-1.amazonaws.com/kaam
Select the body > raw > JSON and add the content for the table in the box and click on send
We have inserted the content "Add new project" into the table
Open the Dynamodb table and click on explore item select scan and run
Create a new request as a put for PUT - https://vf1jqnkncl.execute-api.us-east-1.amazonaws.com/kaam/{id}
ID will be from the Post of Kaam
Create a new req for GET - https://vf1jqnkncl.execute-api.us-east-1.amazonaws.com/kaam and send it will get the results of post
GET is to get the data from the server
POST is to insert the data into the server
PUT to update the data from the server
Serverless also maintains its state like terraform.
You can also monitor the serverless project using the below command
$ sls dev
# dev is becauase serverless-taskmaster-aws-app is in dev mode
Use the http link and monitor Dev
Now go and hit the API get/kaam link in POSTMAN invocations will increase.
Get back to the terminal and use the below command to remove all the deployments
sls remove
GIT and AWS integration with AWS
We can also integrate the GIT and Serverless, select any service of the app and click on settings
Click on CICD and connect to GIT and AWS
Click on Connect
Add the AWS provider IAM USER Role Access details and click on create AWS Provider
Once the integration is done with GIT and AWS, any commits in Git repo will trigger the serverless and deployment will be run.
You also check the status of the deployment by clicking on the deploying...
Deployment is successful
Check the S3 bucket, API, dyanamodb table and cloud formation
To remove the deployed app use sls remove command inside the project termina
Now we will be creating a prod branch and deploying the application on the Prod environment
An error occurred because we are using the same table named kaamkaro for both Dev and prod. Make changes to the table name and the prod deployment will be successful
sls remove --stage=prod
sls remove --stage=dev
Removing all the deployment stacks from Dev and prod environment in AWS console