Getting started with Serverless, AWS Lambda and SAM on MacOS
Introduction
As we progress in the age of digital transformation, organizations are migrating more and more of their operations to the cloud. The Serverless Framework, an open-source project that provides a convenient way to build and deploy applications on AWS Lambda, is a perfect tool for this migration. It eliminates the need for server management, which helps to reduce costs significantly. But what is AWS Serverless, and how does it function? In this blog, we'll go over the essentials of the Serverless framework and how to set it up on a MacOS machine. We'll also introduce the AWS SAM and its uses.
What is AWS Serverless Framework and How is it Helpful?
The Serverless framework is a cloud-computing model in which the cloud provider automatically manages the provisioning and allocation of servers. This model allows developers to focus on their core product instead of managing and operating servers or runtimes, either in the cloud or on-premises. With AWS Lambda, you only pay for the compute time that you consume. This eliminates the need to provision and manage servers, making serverless a cost-effective solution for many applications.
Installing Node.js and Python on Your MacOS Machine
Before we start with the Serverless framework, we need to have Node.js and Python installed on our machine.
To install Node.js, we use the package manager npm. Open Terminal and type the following command:
brew install node
For Python, MacOS already comes with a pre-installed version. However, it's a good idea to install a separate instance using Homebrew:
brew install python
To verify successful installation, type node -v
for Node.js and python --version
for Python. You should see the respective version numbers displayed.
Setting Up AWS and Serverless CLI on MacOS Machine
After installing Node.js and Python, it's time to install the Serverless CLI. You can install it globally with npm:
npm i -g serverless
Upon installation, verify the version using serverless -v
command.
To deploy your functions on AWS, you need to set up AWS credentials. Run the following command, replacing KEY
and SECRET
with your actual AWS credentials:
serverless config credentials --provider aws --key KEY --secret SECRET --profile serverless-admin
How to Invoke, Deploy, and See Logs of Serverless Lambda Functions
To create a new service or function, use the sls create
command along with the appropriate template and path. You can get a list of available templates using sls create --help
command. For example, to create a Python service:
sls create --template aws-python --path myService
To deploy this function to AWS, use sls deploy
. If you want to deploy a specific function only, use sls deploy function -f functionName
.
To test a function on AWS, use the sls invoke -f functionName
command. Adding the --log
flag shows the function logs directly in the console. You can also get function logs separately using sls logs -f functionName
.
What is AWS SAM and How to Set it Up on MacOS
AWS SAM (Serverless Application Model) is an open-source framework for building serverless applications. It provides shorthand syntax to express functions, APIs, databases, and event source mappings.
To install AWS CLI and SAM on MacOS, first, install the AWS CLI:
brew install awscli
Next, install AWS SAM:
brew tap aws/tap brew install aws-sam-cli
Initialize a new SAM application using sam init
, choose your desired runtime (e.g., Python), and template. To build your application, navigate to the project directory and use sam build
.
You can invoke your function locally using sam local invoke
. For an API, you can start a local API Gateway with sam local start-api
.
To deploy your application, use sam deploy --guided
. You can then invoke it using sam local invoke "FunctionName"
.
If you need to delete your stack for any reason, you can do so with the aws cloudformation delete-stack --stack-name SAM_APP_NAME --region us-east-1
command.
You can also use SAM with the VSCode AWS Toolkit. Press Cmd+Shift+P
in VSCode, click on "Add Debug Configuration" above a handler function and run Docker to start debugging.
For Python packages management, you can install the serverless-python-requirements
plugin by running sls plugin install -n serverless-python-requirements
. This will automatically bundle required packages.
Conclusion and Why Serverless is Useful for Cost-Saving
As you can see, the Serverless Framework and AWS SAM greatly simplify the process of creating, deploying, and managing serverless applications. Moreover, by leveraging these tools, we can focus more on the application's business logic rather than infrastructure management.
Serverless architecture is not only efficient but also cost-effective. As there are no servers to manage, it eliminates the costs related to idle time. You pay only for the execution time of your functions, which leads to significant cost savings, especially for applications with variable workloads.
We hope this guide has provided you with a clear understanding of how to get started with the Serverless Framework and AWS SAM on a MacOS machine.