September 14, 202410 minute read
How to deploy code in AWS Lambda: the easy way for beginners
author
Yiren Lu@YirenLu
Solutions Engineer

When you’re new to AWS Lambda, you might be tempted to follow the official documentation’s suggestion to create and deploy functions using the AWS Management Console’s graphical user interface (GUI). While this method is straightforward for simple functions, it makes it difficult to track changes and do version control. As your project grows, you’ll find it challenging to manage dependencies and scale your application.

Enter AWS Serverless Application Model (SAM)

Given these limitations, we recommend instead using the AWS Serverless Application Model (SAM) for deploying Lambda functions. AWS SAM is an open-source framework allows you to use a single YAML file to define your entire serverless stack, including Amazon API Gateway APIs, AWS Lambda functions, and Amazon DynamoDB tables.

SAM offers several advantages:

  1. Infrastructure as Code: Your entire application, including Lambda functions, API Gateway, and other resources, can be defined in a single YAML file.
  2. Local Testing: SAM allows you to test your functions locally before deploying.
  3. Simplified Deployment: One command to package and deploy your entire application.
  4. Version Control Friendly: SAM templates and function code can be easily version controlled.
  5. CI/CD Integration: Easily integrate with CI/CD pipelines for automated testing and deployment.

Step 1: Install the AWS CLI

Before we start with SAM, you need to have the AWS CLI installed. This will allow you to interact with AWS services from your command line.

  1. Install the AWS CLI by following the instructions for your operating system: AWS CLI Installation Guide

  2. After installation, configure your AWS credentials:

    aws configure

    You’ll need to enter your AWS Access Key ID, Secret Access Key, default region, and output format.

Step 2: Install AWS SAM CLI

Next, we’ll install the AWS SAM CLI, which is the main tool we’ll use for deploying our Lambda function.

  1. Follow the installation instructions for your operating system: AWS SAM CLI Installation Guide

  2. Verify the installation by running:

    sam --version

Step 3: Initialize a SAM Project

Now that we have SAM installed, let’s create a new project.

  1. Open your terminal and navigate to a directory where you want to create your project.

  2. Run the following command:

    sam init
  3. You’ll be prompted with several options. Here’s a typical selection for a beginner:

    • Choose an AWS Quick Start Template
    • AWS Quick Start Template: Hello World Example
    • Use the most popular runtime and package type? (Python and zip): Y
    • Would you like to enable X-Ray tracing?: N
    • Would you like to enable monitoring?: Y
    • Project name: hello-world-sam (or any name you prefer)

This will create a new directory with your project name, containing a basic Lambda function and SAM template.

Step 4: Explore and Modify the Code

Let’s take a look at the generated code and make a small modification.

  1. Navigate into your project directory:

    cd hello-world-sam
  2. Open the hello_world/app.py file in your favorite text editor. You’ll see a basic Lambda function that returns a JSON response.

  3. Let’s modify the message. Change the message variable to something like:

    message = f'Hello {event["name"]}! Welcome to AWS Lambda with SAM!'
  4. Save the file.

Step 5: Deploy Your Function

Now it’s time to deploy your Lambda function to AWS.

  1. In your project directory, run:

    sam deploy --guided
  2. You’ll be prompted to provide some information:

    • Stack Name: Choose a name for your CloudFormation stack
    • AWS Region: Choose your preferred region
    • Confirm changes before deploy: Y
    • Allow SAM CLI IAM role creation: Y
    • Disable rollback: N
    • Save arguments to samconfig.toml: Y
  3. SAM will now package and deploy your application. This process may take a few minutes.

  4. Once completed, you’ll see outputs including the API Gateway endpoint URL where you can invoke your function.

Congratulations! You’ve just deployed your first Lambda function using AWS SAM.

Conclusion

You’ve now successfully deployed a Lambda function using AWS SAM. This method provides a straightforward, repeatable process for deploying serverless applications. As you become more comfortable with SAM, you can explore more advanced features like local testing, adding more resources to your template, and setting up CI/CD pipelines.

Remember, while AWS Lambda is a powerful tool, you might find that Modal’s platform offers even simpler deployment processes with additional benefits. We encourage you to explore how Modal can further streamline your serverless development and deployment workflows.

Ship your first app in minutes.

Get Started

$30 / month free compute