Welcome back to the #90DaysOfDevOps series! On this milestone day, we'll be exploring the world of AWS Lambda and the Serverless Framework. Today's lab is all about getting hands-on experience with deploying a simple function on AWS Lambda using the Serverless Framework. So, let's jump right in!
Setting the Stage
Before we get our hands dirty, let's make sure we have the necessary tools in place. Ensure you have the Serverless Framework installed, AWS credentials configured, and Node.js installed on your machine.
Installing Serverless Framework
Open your terminal and run the following command:
npm install -g serverless
This command installs the Serverless Framework globally on your machine.
Configuring AWS Credentials
Make sure you have your AWS credentials configured. If not, you can set them up by running:
serverless config credentials --provider aws --key YOUR_ACCESS_KEY --secret YOUR_SECRET_KEY
Replace YOUR_ACCESS_KEY and YOUR_SECRET_KEY with your actual AWS access key and secret key.
Creating a Simple Function
Now, let's create a simple function that we'll deploy using AWS Lambda and Serverless Framework.
Open your terminal and navigate to the directory where you want to create your project.
Run the following command to create a new Serverless project:
serverless create --template aws-nodejs --path my-lambda-function
This command sets up a basic Node.js project structure with a sample function.
Navigate into your project directory:
cd my-lambda-function
Open the handler.js file in your preferred code editor. You'll see a basic Lambda function written in JavaScript.
Customize the function as needed. For example, you can modify the hello function to:
module.exports.hello = async (event) => {
return {
statusCode: 200,
body: JSON.stringify({
message: 'Hello, DevOps Enthusiast!',
input: event,
}),
};
};
( Note - This handler.js file is automatically created when you initialize a new Serverless project using the serverless create command. If you navigate to the directory where your Serverless project is created and then into the project folder (e.g., my-lambda-function in the example), you'll find the handler.js file there.)
Deploying the Function
With our simple function ready, let's deploy it to AWS Lambda.
Open your terminal and navigate to your project directory (if not already there).
Run the following command to deploy your function:
serverless deploy
This command packages your function and deploys it to AWS Lambda.
Once the deployment is complete, you'll see a summary that includes the endpoint URL of your Lambda function.
Testing the Deployed Function
Now that our function is deployed, let's test it.
Copy the endpoint URL from the deployment summary.
Open your browser or use a tool like curl or Postman to make a GET request to the endpoint.
For example:
curl YOUR_ENDPOINT_URL
You should receive a response similar to:
{"message":"Hello, DevOps Enthusiast!","input":{}}
Congratulations! You've successfully deployed a simple function using AWS Lambda and the Serverless Framework. This hands-on experience will deepen your understanding of serverless architecture and pave the way for more complex projects in the future.
I hope you enjoyed this practical lab and gained valuable insights into the world of serverless computing.
Stay tuned for more DevOps adventures in the days to come!
Thank you for reading!
*** Explore | Share | Grow ***
Comments