Serverless is the industry's buzzword these days, It doesn’t solve all the problems, but enough to become important. What exactly is server-less programming and how did we get where we are today, I have covered it here.
What this post is about?
This post is not about when you should or should not use a serverless. I’m assuming you’ve decided to use serverless. This post is also not about which cloud solution should you use, AWS Lambda, Google Cloud Functions, or Azure Functions. I’m assuming you’ve chosen AWS Lambda. This is not even about comparing AWS SAM or AWS CDK & Serverless framework.
This post is about covering all the steps and showing how easy is to have an AWS lambda up and running with Serverless Framework.

Install Serverless
The easiest way to install Serverless is by running the following command, assuming you already have Node installed on your machine.
If you don’t have Node JS installed, install it from here. After installing Node, run the following command to install Serverless
npm install -g serverless
Create an AWS IAM user for Serverless
Serverless Framework deploys the lambda using the policy attached to the IAMcredentials in your AWS CLI profile. So we need an IAM user.
Create a policy (Optional but recommended)
Theoretically, you can configure your AWS CLI with an IAM user having Administrator Access, but it is not recommended. It's advisable to fine-tune the level of access to the minimum required levels.
Serverless Framework requires access to the following AWS services
S3 - to upload & store serverless artifacts and Lambda code
EC2 - to execute Lambda in VPC
IAM - to manage policies for the Lambda IAM Role
Lambda - to manage Lambda functions
API Gateway - to manage API endpoints
CloudFormation - to create changes and update stack
CloudWatch Logs - to store Lambda execution logs
CloudWatch Events - to manage CloudWatch event triggers
Let’s create a custom policy and attach it to our IAM user. This might be the only time you need to log in to the AWS console.
To create a custom policy, Go into IAM, click policies, click on create policy and go to the JSON tab.

Copy below JSON in the above section.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"cloudformation:*",
"s3:*",
"logs:*",
"iam:*",
"apigateway:*",
"lambda:*",
"ec2:DescribeSecurityGroups",
"ec2:DescribeSubnets",
"ec2:DescribeVpcs",
"events:*"
],
"Resource": [
"*"
]
}
]
}
NOTE: I am not an AWS expert, I wasn’t sure about fine-grained policies, so I used
*
for many services. Let me know in the comments if something can be finetuned
Create User
Create a new user with credential type as Access Key as shown below.

As shown below, On the second screen, click on the last tab and attach the policy that we created in the above step.

Follow the instructions, until the last step. In the last step, make sure todownload the Key and Secret CSV. It's required in the later steps.
Install AWS CLI
As I mentioned above, the Serverless framework requires a locally configured AWS CLI, Let’s install AWS CLI on our machines. Here are the installation instructions for Mac, Linux & Windows.
Configure CLI
After AWS CLI is installed, type the following command and use the ID and secrets from the CSV that we downloaded in one of the above steps.
aws configure
Create a java project
Now all the setup is done.
The Serverless Framework will create all boilerplate code including the request-response java classes, sample handler class, pom files, and a lot more, by just the following command. (use anything you like as name & path)
serverless create \
-t aws-java-maven \
--name your-lambda-api \
--path aws-java-your-lambda-api

The serverless framework creates the following directory and files with prepopulated details.

And now we have a complete project setup to add our business logic.
Update Code
Open the project in your favorite IDE. Update the highlighted lines in your pom.xml with your artifact details.

This post is only about using the serverless framework and not about coding the lambda, so I will not discuss the content of these java files, I’ve another post coding a lambda that covers it.
Configuring the lambda
The serverless framework reads the serverless.yml file to create a cloud formation template for us and pushes it to AWS to spin up all required infrastructure. That is the file we need to configure.
The actual content in this yml will depend upon what your lambda function is doing, how many services it requires access to, etc. The good thing is, that the YML file created by the framework has all the instructions as commented content as below screenshot.

Deploy the lambda
Once you are done configuring this YML as per your requirement and adding code for your lambda, run the following command to create the artifact. For this example, I’m using the default code from the framework, without any change
mvn package
Now you can run just one below command and serverless will do the rest.
serverless deploy

Test your lambda
There are many ways to test, from CLI, to curl but the easiest way is, to log in to the AWS console and go to the lamda section. you will see your lambda, click on Test.
This default hello word lambda is created by serverless.

This was a quick summary for using the Serverless framework, explore the default YML file provided in the code, it has a good amount of documentation to configure for your need.
Check out my other post to see the actual code for writing lambdas. Lets me know your feedback about this post in the comments.
Comments