Open In App

How to Stop AWS Lambda Execution

Last Updated : 25 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

AWS Lambda is a serverless computing service provided by Amazon Web Services that allows developers to run code without the need to provision or manage servers. It is designed to automatically scale with usage, making it highly efficient and cost-effective. It simplifies the process of building and deploying applications, enabling developers to focus on writing code without worrying about the underlying infrastructure. By handling the operational aspects, AWS Lambda allows for quicker development cycles and cost savings.

The key characteristics of AWS lambda are:

  1. It is Serverless
  2. It is Event-driven
  3. It Supports multiple runtimes
  4. It is Scalable

Terminologies

  • AWS Lambda: An event-driven serverless compute service that runs code in response to triggers
  • Invocation: The process of calling and executing a lambda function.
  • Triggers: An event or source that can invoke the lambda functions.

Lambda Function

A Lambda function is a function that is executed in response to a trigger. In AWS Lambda the entry point for the instructions to be executed is typically a handler function named 'lambda_handler'. This function is called when the lambda function is invoked. The 'lambda_handler' is the function where we define the instructions.

Syntax for lambda_handler

The syntax for lambda_handler varies depending on the programming language. Here's a simple example of a lambda_handler function written in python:

Python
def lambda_handler(event,context):
  	#your code
    return{
      'statusCode':200,
      'body':'Success!!!'
    }

Here, the event parameter contains information about the input that triggered the lambda function and the context contains information about the runtime environment.

Use Cases

  • Data Processing: AWS Lambda can process data streams in real-time.
  • Web Applications: It can serve backend logic for web and mobile applications.
  • IoT Applications: AWS Lambda can process data from IoT devices.
  • Automation: Automate tasks such as backups, notifications, and cleanups.

Stopping an AWS Lambda function:

For this article I'm using a simple lambda function that logs the name of a file uploaded to an s3 bucket.

Python
import boto3

# Initialize the S3 client
s3_client = boto3.client('s3')

def lambda_handler(event, context):
    # Log the event details
    print("Received event: " + str(event))
    
    # Get the bucket and object key from the event
    bucket = event['Records'][0]['s3']['bucket']['name']
    key = event['Records'][0]['s3']['object']['key']
    
    # Log the bucket and object key
    print(f"Bucket: {bucket}, Key: {key}")
    
    # Log the name of the uploaded file
    print(f"File uploaded: {key}")

Refer to Amazon Web Services- Creating a Lambda Function for a detailed explanation about how to create and configure your Lambda function. I've added my s3 Bucket as a trigger. Now whenever a file is uploaded to my s3 bucket, the lambda_function prints/logs the name of the file.

Lambda function overview

There are two ways to stop the Lambda function from executing. They are:

1. Removing Triggers

Removing triggers for a lambda function effectively stops it from being invoked by external sources, thus stopping its execution.

Step 1: To remove/delete a trigger go to the Lambda function in the AWS Management Console.

Step 2: Then select the lambda function you are making changes to.

Step 3: In the "Configuration" tab, navigate to the "Triggers" section. Select the trigger you want to remove and click on delete.

By deleting all the triggers of a lambda function you can stop the function from ever being called.

2. Deleting the Lambda Function

You can also stop execution of lambda function by deleting it as a last resort.

To delete a lambda function,

Step-1: Open the Lambda service in AWS and navigate to functions.

Step-2: Select the lambda function you would like to delete and click on actions.

Step-3: Click on delete and follow through with the confirmation prompt.

4_Delete_Lambda
Delete Lambda Function

Thus we've successfully halted the execution of our lambda function.

Troubleshooting Issues

  • Permissions: Make sure your Lambda function has the necessary permissions to access resources like S3 buckets or DynamoDB tables.
  • Logs: Use AWS CloudWatch logs to debug and troubleshoot issues with your Lambda function

For step by step instructions regarding creating AWS Lambda service, refer to AWS Lambda. For a detailed trouble shooting guide please refer to AWS's official documentation.


Next Article
Article Tags :

Similar Reads