Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Build Serverless Applications Using AWS Lambda | Serverless and Event Driven Architectures
AWS Certified Developer - Associate

bookBuild Serverless Applications Using AWS Lambda

Welcome to this lesson on building serverless applications using AWS Lambda—a core component of modern cloud-native development.

What is AWS Lambda?

Note
Definition

AWS Lambda is a serverless compute service provided by Amazon Web Services. "Serverless" doesn't mean there are no servers involved; rather, it means you don't have to manage the underlying infrastructure. You simply upload your code, define how it should be triggered (e.g., when a file is uploaded to S3, or an HTTP request hits your API), and AWS takes care of provisioning, scaling, and maintaining the servers for you.

Lambda automatically scales based on the number of incoming requests, and you are only billed for the time your code runs—measured in milliseconds. This makes it cost-efficient and highly suitable for event-driven or on-demand workloads.

Key Concepts in AWS Lambda

To use Lambda effectively, it's important to understand a few core concepts:

1. Function Code

Your application logic is written as a function and packaged—usually as a .zip file or container image. This is what AWS executes when the Lambda is triggered.

2. Trigger

A trigger is an event that causes your Lambda function to run. Common examples include:

  • An object uploaded to an S3 bucket;
  • A message arriving in an SQS queue;
  • An HTTP request through Amazon API Gateway.

3. Handler

The handler is the function within your code that Lambda calls when it executes your function. It acts as the entry point to your application logic.

For example, in Python, if your file is lambda_function.py and your main function is lambda_handler, the handler would be defined as:

lambda_function.lambda_handler

4. Runtime

The runtime defines the programming language and environment for your Lambda function. AWS supports several runtimes including Python, Node.js, Java, .NET, Go, and others.

Basic Lambda Workflow

Here's how a typical Lambda deployment process looks:

  1. Write your code in a supported language;
  2. Package the code as a .zip file;
  3. Deploy it to AWS Lambda using the AWS CLI or Console;
  4. Configure a trigger (e.g., S3, API Gateway, CloudWatch Events);
  5. AWS automatically runs your function when the event occurs.

This model supports high availability, scalability, and fault tolerance out of the box.

Common Use Cases

AWS Lambda is very versatile. You can use it for:

  • Resizing images when they're uploaded to S3;
  • Handling backend logic for APIs;
  • Processing real-time data streams using Kinesis or DynamoDB Streams;
  • Running scheduled tasks with CloudWatch Events (similar to cron jobs).

It's perfect for event-driven applications where fast, reactive logic is critical.

Hands-On: Deploying a Lambda Function Using the AWS CLI

Now let's walk through a complete, real-world example of deploying a Lambda function using the AWS Command Line Interface (CLI).

Prerequisites

Before starting, make sure:

  • You have the AWS CLI configured with valid credentials;
  • You have permissions to create IAM roles and Lambda functions.

1. Write the Lambda Function

Let's begin by creating a simple Python function:

echo 'def lambda_handler(event, context):\n return {"statusCode": 200, "body": "Hello from Lambda!"}' > lambda_function.py

This creates a file called lambda_function.py with a simple handler that returns a 200 HTTP status code and a message.

2. Zip the Function

Lambda requires your function to be packaged as a ZIP file:

zip function.zip lambda_function.py

3. Create an IAM Role

Lambda needs execution permissions, which it gets by assuming an IAM role. We’ll define a trust policy to allow Lambda to assume the role:

trust-policy.json

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "lambda.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

Then create the role:

aws iam create-role \
 --role-name lambda-ex-role \
 --assume-role-policy-document file://./4.1-trust-policy.json

4. Attach Execution Policy

Attach the managed policy that allows your function to write logs to CloudWatch:

aws iam attach-role-policy \
 --role-name lambda-ex-role \
 --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole

5. Deploy the Lambda Function

Now, deploy your Lambda function:

aws lambda create-function \
 --function-name HelloLambda \
 --runtime python3.9 \
 --role arn:aws:iam::<account-id>:role/lambda-ex-role \
 --handler lambda_function.lambda_handler \
 --zip-file fileb://./function.zip
Note
Note

Replace <account-id> with your actual AWS account ID.

6. Invoke the Lambda Function

To test your function:

aws lambda invoke \
 --function-name HelloLambda \
 output.json && cat output.json

This writes the output to output.json. You should see:

{"statusCode": 200, "body": "Hello from Lambda!"}

Success! You've just deployed and invoked your first Lambda function entirely via the CLI.

Monitoring and Logging with CloudWatch

Every Lambda invocation is automatically logged to Amazon CloudWatch. These logs include:

  • Start and end time of the invocation;
  • Memory usage;
  • Output from print() or logging statements.

CloudWatch is essential for:

  • Debugging;
  • Performance optimization;
  • Monitoring failures or unexpected behavior.

What to Know for the AWS Developer Exam

Here are some key points to remember:

  • Know which services can trigger Lambda (e.g., S3, API Gateway, DynamoDB Streams, CloudWatch Events);

  • Understand how Lambda assumes an IAM role using a trust policy;

  • Be able to package and deploy a Lambda function using the CLI;

  • Be familiar with Lambda configuration options, including:

    • Timeout;
    • Memory allocation;
    • Environment variables.
  • Understand the differences between deploying via the AWS Console and the CLI.

That's it for this lesson! You've now seen how to build, deploy, and run a serverless function from start to finish using AWS Lambda and the AWS CLI. In the next lesson, we'll explore more advanced patterns and integrations.

1. What is the main advantage of AWS Lambda being a "serverless" compute service?

2. Which of the following cannot directly trigger an AWS Lambda function?

3. In a Lambda function configuration, what is the handler responsible for?

4. What is the correct AWS CLI command to invoke a Lambda function and view its output?

5. What does the following IAM trust policy allow?

6. Lambda charges you based on the number of requests and the time your function runs.

7. CloudWatch logs are only available for EC2 instances, not for Lambda functions.

8. You must always use the AWS Management Console to deploy Lambda functions.

question mark

What is the main advantage of AWS Lambda being a "serverless" compute service?

Select the correct answer

question mark

Which of the following cannot directly trigger an AWS Lambda function?

Select the correct answer

question mark

In a Lambda function configuration, what is the handler responsible for?

Select the correct answer

question mark

What is the correct AWS CLI command to invoke a Lambda function and view its output?

Select the correct answer

question mark

What does the following IAM trust policy allow?

Select the correct answer

question mark

Lambda charges you based on the number of requests and the time your function runs.

Select the correct answer

question mark

CloudWatch logs are only available for EC2 instances, not for Lambda functions.

Select the correct answer

question mark

You must always use the AWS Management Console to deploy Lambda functions.

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 4. Chapitre 1

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Awesome!

Completion rate improved to 6.25

bookBuild Serverless Applications Using AWS Lambda

Glissez pour afficher le menu

Welcome to this lesson on building serverless applications using AWS Lambda—a core component of modern cloud-native development.

What is AWS Lambda?

Note
Definition

AWS Lambda is a serverless compute service provided by Amazon Web Services. "Serverless" doesn't mean there are no servers involved; rather, it means you don't have to manage the underlying infrastructure. You simply upload your code, define how it should be triggered (e.g., when a file is uploaded to S3, or an HTTP request hits your API), and AWS takes care of provisioning, scaling, and maintaining the servers for you.

Lambda automatically scales based on the number of incoming requests, and you are only billed for the time your code runs—measured in milliseconds. This makes it cost-efficient and highly suitable for event-driven or on-demand workloads.

Key Concepts in AWS Lambda

To use Lambda effectively, it's important to understand a few core concepts:

1. Function Code

Your application logic is written as a function and packaged—usually as a .zip file or container image. This is what AWS executes when the Lambda is triggered.

2. Trigger

A trigger is an event that causes your Lambda function to run. Common examples include:

  • An object uploaded to an S3 bucket;
  • A message arriving in an SQS queue;
  • An HTTP request through Amazon API Gateway.

3. Handler

The handler is the function within your code that Lambda calls when it executes your function. It acts as the entry point to your application logic.

For example, in Python, if your file is lambda_function.py and your main function is lambda_handler, the handler would be defined as:

lambda_function.lambda_handler

4. Runtime

The runtime defines the programming language and environment for your Lambda function. AWS supports several runtimes including Python, Node.js, Java, .NET, Go, and others.

Basic Lambda Workflow

Here's how a typical Lambda deployment process looks:

  1. Write your code in a supported language;
  2. Package the code as a .zip file;
  3. Deploy it to AWS Lambda using the AWS CLI or Console;
  4. Configure a trigger (e.g., S3, API Gateway, CloudWatch Events);
  5. AWS automatically runs your function when the event occurs.

This model supports high availability, scalability, and fault tolerance out of the box.

Common Use Cases

AWS Lambda is very versatile. You can use it for:

  • Resizing images when they're uploaded to S3;
  • Handling backend logic for APIs;
  • Processing real-time data streams using Kinesis or DynamoDB Streams;
  • Running scheduled tasks with CloudWatch Events (similar to cron jobs).

It's perfect for event-driven applications where fast, reactive logic is critical.

Hands-On: Deploying a Lambda Function Using the AWS CLI

Now let's walk through a complete, real-world example of deploying a Lambda function using the AWS Command Line Interface (CLI).

Prerequisites

Before starting, make sure:

  • You have the AWS CLI configured with valid credentials;
  • You have permissions to create IAM roles and Lambda functions.

1. Write the Lambda Function

Let's begin by creating a simple Python function:

echo 'def lambda_handler(event, context):\n return {"statusCode": 200, "body": "Hello from Lambda!"}' > lambda_function.py

This creates a file called lambda_function.py with a simple handler that returns a 200 HTTP status code and a message.

2. Zip the Function

Lambda requires your function to be packaged as a ZIP file:

zip function.zip lambda_function.py

3. Create an IAM Role

Lambda needs execution permissions, which it gets by assuming an IAM role. We’ll define a trust policy to allow Lambda to assume the role:

trust-policy.json

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "lambda.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

Then create the role:

aws iam create-role \
 --role-name lambda-ex-role \
 --assume-role-policy-document file://./4.1-trust-policy.json

4. Attach Execution Policy

Attach the managed policy that allows your function to write logs to CloudWatch:

aws iam attach-role-policy \
 --role-name lambda-ex-role \
 --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole

5. Deploy the Lambda Function

Now, deploy your Lambda function:

aws lambda create-function \
 --function-name HelloLambda \
 --runtime python3.9 \
 --role arn:aws:iam::<account-id>:role/lambda-ex-role \
 --handler lambda_function.lambda_handler \
 --zip-file fileb://./function.zip
Note
Note

Replace <account-id> with your actual AWS account ID.

6. Invoke the Lambda Function

To test your function:

aws lambda invoke \
 --function-name HelloLambda \
 output.json && cat output.json

This writes the output to output.json. You should see:

{"statusCode": 200, "body": "Hello from Lambda!"}

Success! You've just deployed and invoked your first Lambda function entirely via the CLI.

Monitoring and Logging with CloudWatch

Every Lambda invocation is automatically logged to Amazon CloudWatch. These logs include:

  • Start and end time of the invocation;
  • Memory usage;
  • Output from print() or logging statements.

CloudWatch is essential for:

  • Debugging;
  • Performance optimization;
  • Monitoring failures or unexpected behavior.

What to Know for the AWS Developer Exam

Here are some key points to remember:

  • Know which services can trigger Lambda (e.g., S3, API Gateway, DynamoDB Streams, CloudWatch Events);

  • Understand how Lambda assumes an IAM role using a trust policy;

  • Be able to package and deploy a Lambda function using the CLI;

  • Be familiar with Lambda configuration options, including:

    • Timeout;
    • Memory allocation;
    • Environment variables.
  • Understand the differences between deploying via the AWS Console and the CLI.

That's it for this lesson! You've now seen how to build, deploy, and run a serverless function from start to finish using AWS Lambda and the AWS CLI. In the next lesson, we'll explore more advanced patterns and integrations.

1. What is the main advantage of AWS Lambda being a "serverless" compute service?

2. Which of the following cannot directly trigger an AWS Lambda function?

3. In a Lambda function configuration, what is the handler responsible for?

4. What is the correct AWS CLI command to invoke a Lambda function and view its output?

5. What does the following IAM trust policy allow?

6. Lambda charges you based on the number of requests and the time your function runs.

7. CloudWatch logs are only available for EC2 instances, not for Lambda functions.

8. You must always use the AWS Management Console to deploy Lambda functions.

question mark

What is the main advantage of AWS Lambda being a "serverless" compute service?

Select the correct answer

question mark

Which of the following cannot directly trigger an AWS Lambda function?

Select the correct answer

question mark

In a Lambda function configuration, what is the handler responsible for?

Select the correct answer

question mark

What is the correct AWS CLI command to invoke a Lambda function and view its output?

Select the correct answer

question mark

What does the following IAM trust policy allow?

Select the correct answer

question mark

Lambda charges you based on the number of requests and the time your function runs.

Select the correct answer

question mark

CloudWatch logs are only available for EC2 instances, not for Lambda functions.

Select the correct answer

question mark

You must always use the AWS Management Console to deploy Lambda functions.

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 4. Chapitre 1
some-alt