Serverless “Hello World” in AWS
“Hello World” function(Lambda) for AWS using serverless framework
What is Serverless Architecture?
Serverless architectures refer to applications that significantly depend on third-party services (knows as Backend as a Service or “BaaS”) or on custom code that’s run in ephemeral containers (Function as a Service or “FaaS”), the best known vendor host of which currently is AWS Lambda. By using these ideas, and by moving much behavior to the front end, such architectures remove the need for the traditional ‘always on’ server system sitting behind an application. Depending on the circumstances, such systems can significantly reduce operational cost and complexity at a cost of vendor dependencies and (at the moment) immaturity of supporting services.
what is serverless framework?
The Serverless Framework is a CLI tool that allows users to build & deploy auto-scaling, pay-per-execution, event-driven functions.
Write your code, configure events to trigger your functions, then deploy & run those functions to your cloud provider via the Serverless CLI.
Serverless is your toolkit for deploying and operating serverless architectures. Focus on your application, not your infrastructure. — Serverless.com
Serverless Framework supports following Serverless Cloud Providers,
1. Amazon Web Service
2. Google Cloud Platform
3. Microsoft Azure
4. IBM OpenWhisk
Installing Severless Framework:
This requires you to have NodeJS & NPM installed.You can install the Framework with NPM:
$ npm install -g serverless
After the install completes, you’ll have an executable that you can invoke with the command
serverless or the shorthand
Check the version to make sure it’s properly installed:
$ sls version
If you see a version listed, your install was successful!
Now, Let’s understand some core concepts
- Functions : A Function is an AWS Lambda function. It’s an independent unit of deployment, like a microservice. It’s merely code, deployed in the cloud, that is most often written to perform a single job such as:
● Saving a user to the database
● Processing a file in a database
● Performing a scheduled task
- Events : Anything that triggers an AWS Lambda Function to execute is regarded by the Framework as an Event. Events are infrastructure events on AWS such as:
● An AWS API Gateway HTTP endpoint request (e.g., for a REST API)
● An AWS S3 bucket upload (e.g., for an image)
● A CloudWatch timer (e.g., run every 5 minutes)
● An AWS SNS topic (e.g., a message)
● A CloudWatch Alert (e.g., something happened)
When you define an event for your AWS Lambda functions in the Serverless Framework, the Framework will automatically create any infrastructure necessary for that event (e.g., an API Gateway endpoint) and configure your AWS Lambda Functions to listen to it.
- Resources : Resources are AWS infrastructure components which your Functions use such as:
● An AWS DynamoDB Table (e.g., for saving Users/Posts/ data)
● An AWS S3 Bucket (e.g., for saving images or files)
● An AWS SNS Topic (e.g., for sending messages asynchronously)
● Anything that can be defined in CloudFormation is supported by the Serverless Framework
The Serverless Framework not only deploys your Functions and the Events that trigger them, but it also deploys the AWS infrastructure components your Functions depend upon.
- Services : A Service is the Framework’s unit of organization. You can think of it as a project file, though you can have multiple services for a single application. It’s where you define your Functions, the Events that trigger them, and the Resources your Functions use.
Now, Let’s start with our function at AWS— Lambda:
- Create our first function using following command
sls create --template aws-nodejs --path aws
2. If you look into your “aws” folder you can find two files.
handler.js : Is where our code resides
serverless.yml : Is where our configuration of project resides
3. Now let’s deploy our function (make sure you have already set AWS credentials using AWS CLI or by using serverless config). This will not only release our function(Lambda) but also create resources such as S3 bucket, CloudFormation stack, API getway etc.
More information on — https://serverless.com/framework/docs/
4. Now let’s invoke our function and check log
Hurry we have released our first Lambda function and it is up and running.
5. Let’s remove all the resources so to ensure we don't incur any cost