Oct 07, 2020 | 3 min read

Getting Started with Serverless Framework Using AWS

By: Zachary Sersland

Getting Started with Serverless Framework Using AWS

Serverless applications are becoming more and more popular, but developers quickly find themselves asking, “How do I manage all of these functions?” Serverless Framework is a (relatively) painless way to develop and deploy your serverless application to AWS, Azure, Google Cloud, and other providers. However, getting started isn’t always easy. Here is an overview of how to get started with Serverless Framework using AWS and some common mistakes to avoid.

Setup

To setup Serverless Framework, start with installing Node.js and NPM on your machine. Confirm they’re installed by opening a Command Prompt and entering “node -v” and “npm -v.” From there, install Serverless Framework simply by entering “npm install -g serverless” in Command Prompt.

When it’s ready, navigate where you want your project to live and enter “serverless.” If you’re not using Lambda with Node.js or Python, find the syntax to create your project type by entering “serverless create –help.” For example, when creating a C Sharp project, you’d enter “serverless create -t aws-csharp.” If your project already exists, run the “serverless” command in the project folder and remove the autogenerated project files.

When serverless is set up, it’s best practice to create a dedicated IAM profile to use with the Framework. This user should only have permission to access Lambda and the resources your Lambda methods will use, such as S3 API Gateway. Install the AWS CLI, if you haven’t already, and store the credentials in a profile on your local machine. These profiles can be used during deployment with Serverless Framework by using the --aws-profile flag in the “serverless deploy” command.

Navigating the YAML File

Each YAML file contains a Service, which is the equivalent of a project in the Serverless Framework. The Service will define one or more Functions with their associated Events, as well as define any Resources your functions will use. Define your function with a memorable name, a handler with the format “package.Class::method,” and the path to the package artifact. You can also override any default Lambda properties and set environmental variables.

Events are anything that triggers the Lambda function . Serverless Framework lets you set up any event you could configure through AWS, including an API Gateway request, an S3 upload, or a CloudWatch alert. A useful aspect of Serverless Framework is that, in addition to defining the events, you can define the resources the events will use.

AWS resources, including S3 buckets, IAM roles, and API Gateway Methods, can be created in the Resources section. In addition to saving time in the AWS console, defining resources programmatically lets you spin up and tear down new staging environments quickly and easily.

Deploying Your Project

When everything looks good in your YAML file, deploy your project by entering “serverless deploy” in your command prompt. This will upload the artifacts to the new S3 bucket created in the deployment process and create the Lambda method with any defined resources.

Variables are a useful way to specify resource names or environments in your YAML file and can make deployments much less error-prone. For example, Lambda methods can have many environmental variables that vary between environments. When defining a variable called SERVICE_ENDPOINT, instead of switching the name between dev.website.com and staging.website.com, you can use ${opt:stage}.website.com.

There’s a lot more to know about variables, as well as Serverless Framework in general. You can learn more about setting up Serverless Framework at the Serverless website or Serverless-Stack.com. Contact DragonSpears if you need help getting started with serverless applications or the Serverless Framework.

About Zachary Sersland

Zachary Sersland is a senior developer and team lead at DragonSpears. He earned his degree in computer science from Northwestern University and has been working in software development and consulting ever since. His focus is primarily in .Net and AWS, but he’s also taken on projects using Azure, PHP, and even Python. His favorite aspects of working at DragonSpears are the company’s development of leaders at every level and the opportunity to work with such talented teammates. He’s a movie fan, having seen every movie on the AFI and BFI Top 100 Films lists, and in high school, he earned money as a church organist.