Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to Deploy an AWS Lambda Function with Serverless Framework

Serverless computing has revolutionized the way developers build and deploy applications in the cloud. It takes away the complexities of server management, allowing developers to focus solely on writing code and delivering value to their users.In the realm of Serverless computing, AWS Lambda stands out as a leading platform for running serverless functions in a scalable and cost-effective manner.Serverless Framework is a powerful tool that simplifies the deployment and management of serverless applications across various cloud providers, including Amazon Web Services (AWS). This guide aims to walk you through the process of using the Serverless Framework to deploy a simple Python Function to AWS Lambda, expose it via API Gateway, and monitor it using AWS CloudWatch.The purpose of this guide is to provide you with a step-by-step tutorial on deploying a serverless Python function on AWS using the Serverless Framework. Whether you're new to serverless computing or looking to expand your skills, this tutorial is designed to help you with the following:By the end of this article, you'll have a clear understanding of how to leverage the Serverless Framework to deploy and manage serverless applications on AWS. You'll also gain practical experience in deploying serverless functions and exposing them through an API endpoint, paving the way for building and scaling serverless applications in your projects.Now, let's dive into the prerequisites needed to get started with this tutorial.To follow along with this tutorial, you'll need the following:You'll need to set the AWS credentials for the AWS CLI if you have not done that already. You'll be using it along with the Serverless Framework to deploy the resources on AWS. You can create the AWS credentials file by entering the following command in the terminal:The IAM role is also used by the Serverless Framework to deploy the resources on AWS. Enter the following command to create the role:This policy allows the role to be used by the AWS Lambda service.Enter the following command to attach the AWSLambdaBasicExecutionRole policy to the role:To verify that the role has been created successfully, you can run the following command to get information about the IAM role:Here's what the information should look like:This project is a simple python function that is deployed to AWS Lambda, API Gateway, and CloudWatch using the Serverless Framework. The function is triggered by an HTTP GET request and returns a simple string. The function is deployed to the eu-west-1 region.First, install Serverless Framework using npm:Next, create a new Serverless Framework project using the serverless command and then follow the prompt:Then choose AWS Python Starter from the template list. Give it any name of your choice – I used serverless-lab.After the command runs successfully, you will see the two main components created: serverless.yaml, and handler.py.To keep things organized, let's create a folder named functions, and create a file named __init__.py inside it. You can do that using this command:Create your first function by creating a file named first_function.py inside the functions folder:Then open the first_function.py file, and paste the following Python code to define the function you'll deploy:This code above is a simple Python function that returns a JSON object with status code and body values. As you can see, we inserted the two parameters — event and context — required from the functions as a Serverless Framework convention.Next, open the handler.py file and delete its content and paste the following Python code to define the handler that will be invoked when the function is triggered:The code above exposes the function you created in the first_function.py file. We imported the function, and exposed it to the framework.To start with the configuration, open the serverless.yaml file and delete all of its content and paste the following YAML code to define the microservice you will deploy:Let's break down each section line by line:service specifies the name of your Serverless service or project. In this case, it's named "serverless-lab," which will be used as the service name when deploying to AWS.provider defines the AWS provider for your service. It specifies various configuration settings for AWS Lambda functions and other AWS resources.functions defines the AWS Lambda functions in your service.You can use the command below to deploy the microservice on AWS:After a while, the deployment will be completed and you can see information like the endpoint, hosted on API Gateway, to trigger the function you just deployed.The framework deployed the function on AWS Lambda and, because you attached an HTTP trigger to it. It has deployed an API on API Gateway to let the function be reachable.From the deployment, you have a single function named first_function, and a single HTTP GET endpoint. Using the GET endpoint (the endpoint generated in the terminal after deploying the function) in your browser, you can call the function:The image above shows the functionality created in the deployed function running in the browser.The log group is automatically saved on AWS CloudWatch because there is a print statement defined in the function. Enter the following command to access the function's logs:AWS CloudWatch is the native AWS logging service that lets you monitor and access logs from your applications. You can find log groups, and you can also apply filter expressions on logs to retrieve those you need.You can delete the microservice and resources you just deployed using the serverless remove command. Check out my GitHub repository to see the full codeIn this comprehensive guide, we've explored the powerful world of serverless computing and demonstrated how to harness its capabilities using the Serverless Framework and Amazon Web Services (AWS). You've embarked on a journey from setting up your development environment to deploying a simple Python function as an AWS Lambda-backed API, all while gaining insights into monitoring and logging with AWS CloudWatch.This guide serves as a starting point for your serverless journey. As you become more proficient with the Serverless Framework and AWS, you'll be able to build and deploy sophisticated serverless applications that scale dynamically and meet the demands of modern, cloud-native architectures.As always, I hope you enjoyed the article and learned something new. If you want, you can also follow me on LinkedIn or Twitter.I build and write about software development, blockchain, DevOps/cloud computing, and tech communities. If you read this far, tweet to the author to show them you care. Tweet a thanks Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started freeCodeCamp is a donor-supported tax-exempt 501(c)(3) charity organization (United States Federal Tax Identification Number: 82-0779546)Our mission: to help people learn to code for free. We accomplish this by creating thousands of videos, articles, and interactive coding lessons - all freely available to the public. We also have thousands of freeCodeCamp study groups around the world.Donations to freeCodeCamp go toward our education initiatives, and help pay for servers, services, and staff. You can make a tax-deductible donation here.



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

How to Deploy an AWS Lambda Function with Serverless Framework

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×