Deploying Lambda functions on AWS is a great way to run code without provisioning or managing servers. AWS Lambda executes code in response to events such as changes in data, shifts in state, or user actions. In this guide, we will walk through the steps to deploy a Lambda function on AWS, including creating the function, writing code, deploying with AWS CLI and AWS SAM, and setting up triggers. We will also reference code examples hosted on GitHub.
Step 1: Setting Up Your AWS Environment
- Create an AWS Account: If you haven’t already, sign up for an AWS account here.
- Install the AWS Command Line Interface (CLI): Download and install the AWS CLI from this link. Follow the instructions for your operating system.
- Configure the AWS CLI: Open your terminal or command prompt and configure the AWS CLI with your credentials:
aws configure
Enter yourAWS Access Key ID
,AWS Secret Access Key
,Default region name
(e.g.,us-east-1
), andDefault output format
(e.g.,json
).
Step 2: Create a Lambda Function
- Go to the AWS Lambda Console: Navigate to the AWS Lambda console.
- Create a New Lambda Function:
- Click on Create function.
- Choose Author from scratch.
- Enter a function name (e.g.,
MyLambdaFunction
). - Select a runtime (e.g.,
Python 3.8
,Node.js 14.x
). - Set up an execution role:
- Choose Create a new role with basic Lambda permissions.
- Configure Your Lambda Function:
- Once created, you will be taken to the Lambda function configuration page.
- Here, you can configure the function’s code, environment variables, memory, timeout, and other settings.
Step 3: Write and Deploy the Lambda Code
Python Example: Here is a basic “Hello, World!” Lambda function written in Python. Refer to the code on GitHub: lambda function
Node.js Example: For a similar function in Node.js, refer to this file:
After pasting your code in the Lambda console, click Deploy to save the changes.
Step 4: Test the Lambda Function
- Create a Test Event:
- Click on the Test button.
- Choose Configure test event and create a new event (you can use the default settings for now).
- Run the Test:
- Click on Test to execute the Lambda function with the test event.
- The output should display a
200
status code and the response body, such as “Hello, World!”.
Step 5: Set Up Triggers for the Lambda Function
- Add a Trigger:
- Go to the Function overview section and click on Add trigger.
- Choose a trigger source (e.g., API Gateway, S3, CloudWatch Events).
- Configure the Trigger:
- Follow the specific steps to set up the trigger. For example, if you choose API Gateway, it will guide you through creating a REST API that triggers the Lambda function when a specific endpoint is hit.
Step 6: Deploy the Lambda Function Using AWS CLI or AWS SAM (Serverless Application Model)
Deploying with AWS CLI: You can use the following script to create a zip file of your Lambda function and deploy it via the AWS CLI. Refer to the GitHub link:
Deploying with AWS SAM: If you prefer using AWS SAM for deployment, use the template.yaml
file available on GitHub:
And the corresponding Python code:
Step 7: Monitor and Manage the Lambda Function
- Monitor Using CloudWatch:
- Lambda automatically integrates with Amazon CloudWatch, providing logs for monitoring function execution.
- View Logs:
- Go to the CloudWatch console.
- Navigate to Logs > Log groups >
/aws/lambda/MyLambdaFunction
.
- Set Up Alarms:
- You can set up alarms to monitor the performance and errors of your Lambda function.
Additional Resources
- AWS Lambda Documentation: AWS Lambda Developer Guide
- AWS SAM Documentation: AWS Serverless Application Model (SAM)
- AWS CLI Documentation: AWS CLI Command Reference