Hello World - Python Example from Scratch
https://github.com/koushikvikram/serverless-python-hello-world
Hello World Example - Node.js template
Prerequisites:
- Install Docker
- Install Serverless CLI
- Create an AWS account and a AWS profile for the same. If not, you can use this guide to set it up or install and configure the aws-cli.
cd
into your project directory and create an application using serverless CLI with the help of one of the templates already available with serverless using following command.
serverless create --template aws-nodejs-docker
This will create following files:
- app.js file to hold business login in handler method.
- Dockerfile, to package the Node.js app in a docker image.
- serverless.yml, for declaring deployment configuation variables.
From the same directory, run
serverless package
To deploy the serverless application, run the following command from the same directory
serverless deploy
Upon completion, we can see the following resources created in the AWS console:
- CloudFormation stack with its artifacts in S3 bucket.
- Lambda Function created with type docker.
- Roles and policies
- CloudWatch Log Groups for application logs.
- Docker Image in ECR
We can test the app by running the following command in the same directory
serverless invoke -f hello -d '{"msg": "Cheers !!"}'
RESPONSE:
{
"statusCode": 200,
"body": "{\n \"message\": \"Hello, world! Your function executed successfully! Cheers !!\"\n}"
}
Finally, we can deprovision/clean up all the deployed components by running the following command in the same directory
serverless remove
Reference:
- Serverless Framework by Anuj Kumar, Medium
- How to deprovision/clean up deployed components (Website)? #485
Things to Consider
To prevent accidental deletion, either:
- Create separate CloudFormation Stack for data storage components like S3 and DynamoDB.
- Set stackpolicy in serverless.yml to prevent replacing or deleting these components.
Production-Ready Serverless Course
Source: Production-Ready Serverless - Operational Best Practices