This is my first ever blog. I hope you find it valuable.
Setting up the Workstation
- Install Node.js - Download Node.js in my case - Windows Installer .msi 64-bit on workstation
- Install Serverless Download Serverless Getting Started on workstation -
npm install -g serverless
- Root user or root equivalent user access in AWS
Create an IAM Role - AWS Service
Access key will enable serverless to connect with AWS
- Go to IAM -> Create users -> Add a user name: jayp1_serverless -> Select AWS access type -> Select AWS credential type -> Access key - Programmatic access
Description: > Access Key - Programmatic Access - Enables an access key ID and secret access key for the AWS API, CLI, SDK, and other development tools.
Setup serverless configuration to connect with AWS
serverless config credentials --provider aws --key <access-key> --secret <secret-key>
Create a directory:
mkdir aws_2022 && cd aws_2022
and initialize Serverless projectserverless
Choose AWS-Python-Starter
Name it default
Deploy - Yes
C:\Users\jp\Documents\aws_2022>serverless
Creating a new serverless project
? What do you want to make? AWS - Python - Starter
? What do you want to call this project? aws-python-project
✔ Project successfully created in aws-python-project folder
? Do you want to login/register to Serverless Dashboard? No
? Do you want to deploy now? (Y/n) y
? Do you want to deploy now? Yes
Deploying aws-python-project to stage dev (us-east-1)
× Stack aws-python-project-dev failed to deploy (78s)
Environment: win32, node 16.14.2, framework 3.12.0, plugin 6.2.1, SDK 4.3.2
Docs: docs.serverless.com
Support: forum.serverless.com
Bugs: github.com/serverless/serverless/issues
Error:
CREATE_FAILED: IamRoleLambdaExecution (AWS::IAM::Role)
API: iam:GetRole User: arn:aws:iam::673040272970:user/jayp1_serverless is not authorized to perform: iam:GetRole on resource: role aws-python-project-dev-us-east-1-lambdaRole because no identity-based policy allows the iam:GetRole action
View the full error: https://us-east-1.console.aws.amazon.com/cloudformation/home?region=us-east-1#/stack/detail?stackId=arn%3Aaws%3Acloudformation%3Aus-east-1%3A673040272970%3Astack%2Faws-python-project-dev%2F6669ede0-c3c3-11ec-9584-0a8914e80a47
- Note the IamRole error , to fix is to update the role permissions by giving the access key user - jayp1_serverless.
- Go to IAM -> Users -> jayp1_serverless -> Add Permissions -> Choose Attach existing policy directly
Creating a new serverless project
? What do you want to make? AWS - Python - Starter
? What do you want to call this project? aws-python-project-2022
✔ Project successfully created in aws-python-project-2022 folder
? Do you want to login/register to Serverless Dashboard? No
? Do you want to deploy now? (Y/n) Y
? Do you want to deploy now? Yes
Deploying aws-python-project-2022 to stage dev (us-east-1)
✔ Service deployed to stack aws-python-project-2022-dev (111s)
functions:
hello: aws-python-project-2022-dev-hello (1.7 kB)
What next?
Run these commands in the project directory:
serverless deploy Deploy changes
serverless info View deployed endpoints and resources
serverless invoke Invoke deployed functions
serverless --help Discover more commands
- Go to Lambda -> Functions -> aws-python-project-2022-dev-hello (filename-stage-function) - try to check in default region - N.Virginia
- Invoke lambda function on local workstation
serverless invoke --function hello
C:\Users\jp\Documents\aws_2022>cd aws-python-project-2022
C:\Users\jp\Documents\aws_2022\aws-python-project-2022>serverless invoke --function hello
{
"statusCode": 200,
"body": "{\"message\": \"Go Serverless v3.0! Your function executed successfully!\", \"input\": {}}"
}
-
There are two concerned files for developer - handler.py and serverless.yml
- handler.py contains a function hello which returns a json message.
import json
def hello(event, context):
body = {
"message": "Go Serverless v3.0! Your function executed successfully!",
"input": event,
}
return {"statusCode": 200, "body": json.dumps(body)}
- serverless.yml is generated or created automatically while we create a serverless project. To understand serverless YML keywords Serverless Framework - AWS Lambda Guide
service: aws-python-project-2022
frameworkVersion: '3'
provider:
name: aws
runtime: python3.8
functions:
hello:
handler: handler.hello
- service - project configuration file
- provider : which cloud provider in our case its AWS
- name : aws
- runtime : python3.8
Deploying a new lambda function in desired region
Create a new lambda function getting_started in new handler file application.py which will be deployed in Mumbai region: ap-south-1
- Create a function - getting started in application.py
import json
def getting_started(event, context):
body = {
"message": "Go Serverless v3.0! Getting Started with Serverless \
execution!",
"event": event,
}
return {"statusCode": 200, "body": json.dumps(body)}
- Modify serverless.yml
service: aws-python-project-2022
frameworkVersion: '3'
provider:
name: aws
runtime: python3.8
region: ap-south-1
functions:
hello:
handler: handler.hello
getting_started:
handler: application.getting_started
- Deploy changes on AWS
serverless deploy
- Invoke getting_started on workstation
serverless invoke --function getting_started
C:\Users\jp\Documents\aws_2022\aws-python-project-2022>serverless invoke --function getting_started
{
"statusCode": 200,
"body": "{\"message\": \"Go Serverless v3.0! Getting Started with Serverless execution!\", \"event\": {}}"
}
API Gateway and AWS Lambda through Serverless Framework
Event - Http - GET* Method will invoke
getting_started
functionSetting up the APIs in API Gateway events under function and listing one such event
-http
withpath: /data
andmethod: GET
functions:
hello:
handler: handler.hello
getting_started:
handler: application.getting_started
events:
- http:
path: /data
method: GET
- Deploy the changes on AWS
serverless deploy
notice the endpoint
C:\Users\jp\Documents\aws_2022\aws-python-project-2022>serverless deploy
Deploying aws-python-project-2022 to stage dev (ap-south-1)
✔ Service deployed to stack aws-python-project-2022-dev (48s)
endpoint: GET - https://ni5xsk9f08.execute-api.ap-south-1.amazonaws.com/dev/data
functions:
hello: aws-python-project-2022-dev-hello (2 kB)
getting_started: aws-python-project-2022-dev-getting_started (2 kB)
Improve API performance – monitor it with the Serverless Dashboard: run "serverless"
- Checking the endpoint: https://ni5xsk9f08.execute-api.ap-south-1.amazonaws.com/dev/data
- Go to API Gateway -> Select Mumbai region -> APIs
- Check the invocation counts, error logs etc. in Monitor in Lambda console.
Top comments (0)