Aws – Serverless Framework

Steps to deploy project in lambda using serverless

Access key ID – ****
Secret access key – ****
region : ap-south-1

npm install -g serverless

1. Configure AWS

Error :
E:\Prabha\Local\AWS\serverles>sls config credentials –provider aws –key **** –secret ****
Serverless: Setting up AWS…
Serverless: Failed! ~/.aws/credentials already has a “default” profile. Use the overwrite flag (“-o” or “–overwrite”) to force the update

Solutions : -o
E:\Prabha\Local\AWS\serverles>serverless config credentials –provider aws -o –key **** –secret ****
Serverless: Setting up AWS…

2. serverless –help / sls –help

3. Serverless Create Template : E:\Prabha\Local\AWS\serverles>sls create –template aws-nodejs

4. Invoke function : sls invoke –function hello

5. Deploy : E:\Prabha\Local\AWS\serverles>sls deploy

Serverless: Configuration warning at 'functions.events': should be object
Serverless:  
Serverless: Learn more about configuration validation here: http://slss.io/configuration-validation
Serverless:  
Serverless: Packaging service...
Serverless: Excluding development dependencies...
 
  Serverless Error ---------------------------------------

  Either "handler" or "image" property needs to be set on function "events"

  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues

E:\Prabha\Local\AWS\serverles>sls deploy
Serverless: Deprecation warning: Starting with next major version, API Gateway naming will be changed from "{stage}-{service}" to "{service}-{stage}".
            Set "provider.apiGateway.shouldStartNameWithService" to "true" to adapt to the new behavior now.
            More Info: https://www.serverless.com/framework/docs/deprecations/#AWS_API_GATEWAY_NAME_STARTING_WITH_SERVICE
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Creating Stack...
Serverless: Checking Stack create progress...
........
Serverless: Stack create finished...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service myaws-nodejs.zip file to S3 (411 B)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
.................................
Serverless: Stack update finished...
Service Information
service: myaws-nodejs
stage: prod
region: ap-south-1
stack: myaws-nodejs-prod
resources: 12
api keys:
  None
endpoints:
  GET - https://zectf4zo64.execute-api.ap-south-1.amazonaws.com/prod/first
functions:
  hello: myaws-nodejs-prod-hello
layers:
  None

**************************************************************************************************************************************
Serverless: Announcing Metrics, CI/CD, Secrets and more built into Serverless Framework. Run "serverless login" to activate for free..
**************************************************************************************************************************************

Steps To configure Lambda with Serverless

Download AWS CLI
Install AWS CLI
aws configure - we need to configure AWS settings
	- AWS Access Key ID
	- AWS Secret Access Key
	- Default region name
	- Default output from format
My Security Credentials
	Access key ID − ***
	Secret access key − ***
Check : aws --version

aws lambda create-function

Creating and Deploying using Serverless Framework

npm install -g serverless
serverless
sls
sls create --help
Create User with Roles and Policy
sls config credentials --provider aws --key accesskey --secret secretkey
sls create --template aws-nodejs

Serverless.yml has the configuration

{
	service: aws-nodejs 
	provider:
	 name: aws
	 runtime: nodejs6.10
	functions:
		hello:
			handler: handler.hello
			
			events:
			 - http:
				 path: firstApi
				 method: GET
			 - http:
				 path: postApi
				 method: POST
	stage: prod
	region: us-east-1
	
	resources:
	 Resources:
	   uploadBucket:
		 Type: AWS::S3::Bucket
		 Properties:
		   BucketName: ${self:service}-${self:provider.stage}-uploads
}
sls deploy

sls invoke --function hello

sls invoke local --function hello 

It creates the iam role for AWS Lambda and log group for AWS cloudwatch. S3 bucket is created which has the code details stored and the configuration details.
This is created by the command sls deploy. You need not specify the iam role, instead it is created by default during the deploy stage.

There is a new thing added here called events. We have specified the event as http, along with its path and method.
The path is the end-point which we will use when the api gateway path is created

It gives the Get url with the end-point as the path details. The stage is prod so same is used in the url. The name of the function is aws-api-prod-hello.

1.Deploy changes faster with sls deploy function : sls deploy function -f myFuncName
2.Tail your live service logs : serverless logs -f myFuncName -t
serverless remove --stage dev --region ap-south-1

AWS-Cmd
aws cloudformation list-stacks
aws cloudformation delete-stack --stack-name apolloslsapp01-dev
aws cloudformation describe-stacks --stack-name myaws-nodejs-prod
aws cloudformation list-stacks --stack-status-filter DELETE_FAILED
aws cloudformation list-stacks --stack-status-filter DELETE_IN_PROGRESS
aws cloudformation list-stacks --stack-status-filter DELETE_COMPLETE

aws cloudformation list-stacks --stack-status-filter CREATE_FAILED
aws cloudformation list-stacks --stack-status-filter CREATE_COMPLETE
aws cloudformation list-stacks --stack-status-filter CREATE_IN_PROGRESS
aws cloudformation list-stacks --stack-status-filter UPDATE_COMPLETE

GraphQL as an API gateway in microservices architecture – Shipra Gupta
QUERY GRAPHQL WITH APOLLO HOOKS + REACT | Full Stack Serverless #7
Full-Stack Serverless

Switch aws-cli User

Step 1 : set the name and region in serverless.yml file
Step 2 : remove .serverless folder in visual studio code 
Step 3 : execute the command to see the list of profiles  - "aws configure list-profiles"
Step 4 : execute the command to set the aws-cli account - "aws configure"
			aws_access_key_id = ******
			aws_secret_access_key = *******
step 5 : execute the command "sls deploy"

commands :

%USERPROFILE%\.aws\credentials

aws configure
aws configure --profile api-cognito

aws configure list-profiles
aws configure list 

export AWS_PROFILE=api-cognito

To switch to aws-cli user :

1. remove dir ".serverless"
2. aws configure
3. aws_access_key_id = xxxx
aws_secret_access_key = xxxx
4. sls deploy

The command returns information about your AWS account and identity, your credentials are valid

cmd: aws sts get-caller-identity

{
    "UserId": "xxxx",
    "Account": "xxxxx",
    "Arn": "arn:aws:iam::xxxx:user/abc-mongo"
}

AWS-CLI User Switch

To Remove Lambda Function : 
1. Delete the S3 buket
2. Go to CloudFormation
	1. Delete the Stack name
3. Create a IAM User with policies for new lambda-function
	1. It will give a access_key & Secrete key
	2.Copy those credentials
4. Go to command prompt in windows
	1. Download & install "AWS-CLI"
	2. "aws configure" access_key & secrete_key
	3. Install npm & Configure "Serverless"
	4. Crearte serverless.yml file
5. Execute the command "sls deploy"
6. Test the URL

Note : Incase deploy another lambda function for another policy, take the access & secrete_key of the IAM User policy
1.	Change the "aws configure" access_key & secrete_key
2.	Execute the command "sls deploy"

Reference

AWS | Typescript Apollo Lambda
Running a scalable & reliable GraphQL endpoint with Serverless
How to deploy Apollo Server with AWS Lambda
Creating and Deploying using Serverless Framework
How to deploy multiple micro-services under one API domain with Serverless

Serverless CLI – AWS – deploy
Serverless CLI – AWSPlugin Install
How to deploy multiple micro-services under one API domain with Serverless
Quick-tips-for-faster-serverless-development
Serverless Examples

Complete Coding-Serverless Framework with AWS PlayList
The Serverless Framework with AWS PlayList
How to deploy your React App to AWS with the Serverless Framework – Full Tutorial with CloudFront

How to deploy an Apollo GraphQL server running in AWS Lambda with Amplify Functions
Git
How to build a low-latency serverless GraphQL API on AWS with Node.js, AppSync, Lambda, and DynamoDB
Adding Serverless Webpack to your Project – fix Lambda Upload Limits

Youtube – AWS Lambda Tutorial: How to deploy Node.js REST APIs on aws lambda Function

Leave a Reply

Your email address will not be published. Required fields are marked *