...

Step-by-Step Guide: Deploying Lambda Functions on AWS

August 2, 2024 · 5 minutes read

Reviewed by: Dr. Maya

Table of Contents

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

  1. Create an AWS Account: If you haven’t already, sign up for an AWS account here.
  2. Install the AWS Command Line Interface (CLI): Download and install the AWS CLI from this link. Follow the instructions for your operating system.
  3. Configure the AWS CLI: Open your terminal or command prompt and configure the AWS CLI with your credentials: aws configureEnter your AWS Access Key ID, AWS Secret Access Key, Default region name (e.g., us-east-1), and Default output format (e.g., json).

Step 2: Create a Lambda Function

  1. Go to the AWS Lambda Console: Navigate to the AWS Lambda console.
  2. 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.
  3. 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

  1. 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).
  2. 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

  1. 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).
  2. 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

  1. Monitor Using CloudWatch:
    • Lambda automatically integrates with Amazon CloudWatch, providing logs for monitoring function execution.
  2. View Logs:
    • Go to the CloudWatch console.
    • Navigate to Logs > Log groups > /aws/lambda/MyLambdaFunction.
  3. Set Up Alarms:
    • You can set up alarms to monitor the performance and errors of your Lambda function.

Additional Resources

Franck Kengne

Tech Visionary and Industry Storyteller

Seraphinite AcceleratorOptimized by Seraphinite Accelerator
Turns on site high speed to be attractive for people and search engines.