Home Blogs Integrate Lambda with any services like S3, and then integrate Lambda with API Gateway

Integrate Lambda with any services like S3, and then integrate Lambda with API Gateway

by Anup Maurya
4 minutes read

Integrating Lambda with various services like S3 and then connecting it with API Gateway allows for seamless and scalable serverless architectures. In this blog post, we will explore the process of integrating Lambda with S3, and then integrating Lambda with API Gateway. Finally, we will retrieve the Lambda function event using API Gateway.

Serverless computing has gained immense popularity due to its ability to abstract away the infrastructure management and provide developers with the flexibility to focus on writing code. AWS Lambda, one of the leading serverless computing platforms, allows developers to run code without provisioning or managing servers.

Integrating Lambda with S3

AWS S3 (Simple Storage Service) is an object storage service that offers industry-leading scalability, data availability, security, and performance. By integrating Lambda with S3, we can trigger Lambda functions whenever an event occurs in the S3 bucket, such as object creation, deletion, or modification.

To integrate Lambda with S3, follow these steps:

  • Create an S3 bucket: Start by creating an S3 bucket in your AWS account. Note down the bucket name as we will need it later.
  • Create a Lambda function: Next, create a Lambda function by selecting the appropriate runtime and configuring the function’s code. Remember to enable the necessary permissions for accessing S3.
No alt text provided for this image
Creating new lambda function
No alt text provided for this image
Adding permissions for lambda function to access S3 using IAM
No alt text provided for this image
Selecting relevent permissions to use S3 for lambda function
No alt text provided for this image
Adding S3 full Access permission to our lambda function
  • Configure S3 trigger: Within the Lambda function configuration, add an S3 trigger. Specify the bucket name and the event(s) that should trigger the Lambda function.This can be done by creating an event trigger in s3 bucket using our lambda function
No alt text provided for this image
Adding S3 event notification name
No alt text provided for this image
Adding “All objects create event” for triggering S3
No alt text provided for this image
Choosing our lambda function to connect it to our S3 bucket
  • Write Lambda function code: Now, write the code for your Lambda function. This code will be executed whenever the specified event occurs in the S3 bucket. You can perform various operations on the S3 object, such as processing, analyzing, or transforming the data.
import boto3
def lambda_handler(event, context):
    s3 = boto3.resource('s3')
    bucket = s3.Bucket('<my_bucket_name>')
    objects_uploaded = [obj.key for obj in bucket.objects.all()]
    print("Objects uploaded are:", objects_uploaded)
    return {
        'uploaded': objects_uploaded
    }

Replace <my_bucket_name> with your bucket name.

No alt text provided for this image
python code in lambda function to output names of s3 uploaded items
  • Test the integration: Upload an object to the S3 bucket and observe the Lambda function being triggered. Check the logs and verify that the function executed successfully.
No alt text provided for this image
Uploading file in bucket
  • Integrating Lambda with API Gateway:

AWS API Gateway is a fully managed service that enables developers to create, publish, and manage APIs at any scale. It acts as a front door for applications to access data, business logic, or functionality from backend services, including Lambda functions.

To integrate Lambda with API Gateway, follow these steps:

No alt text provided for this image
Selecting REST API for building New API
  • Create an API Gateway: Start by creating an API Gateway in your AWS account. Configure the necessary settings, such as the API name, endpoint type, and security options.
No alt text provided for this image
Creating new REST API
  • Create a resource and method: Within the API Gateway, create a resource and method that will be associated with your Lambda function. For example, you can create a GET method for retrieving data from Lambda.
No alt text provided for this image
Creating new resource for API
No alt text provided for this image
Creating GET Method for our API
  • Configure integration: In the method configuration, choose the integration type as “Lambda Function” and select the Lambda function you want to integrate. Configure any additional settings, such as request/response mapping templates or authentication.
No alt text provided for this image
  • Deploy the API: After configuring the integration, deploy the API Gateway to make it accessible to clients. This step generates a unique endpoint URL that clients can use to interact with your Lambda function.
No alt text provided for this image
Created API Gateway

Retrieving Lambda function event using API Gateway

Once the Lambda function is integrated with API Gateway, you can retrieve the Lambda function event by invoking the API endpoint. Clients can send requests to the API Gateway endpoint, which will trigger the associated Lambda function and return the response.

To retrieve the Lambda function event using API Gateway, follow these steps:

  • Obtain the API Gateway endpoint URL: After deploying the API Gateway, note down the endpoint URL. This URL will be used to send requests to the API.
No alt text provided for this image
  • Send a request to the API endpoint: Use any HTTP client, such as cURL or Postman, to send a request to the API Gateway endpoint. Make sure to include any required headers or query parameters as specified in your API configuration.
No alt text provided for this image
  • Receive the response: Once the Lambda function execution completes, the API Gateway will return the response to the client who made the request. The response can be in various formats, such as JSON, XML, or binary data.Here we get a list of files uploaded successfully in our bucket.
No alt text provided for this image
Returning Lambda using API Gateway

that’s all by following these steps, you can easily integrate Lambda with services like S3 and then connect it with API Gateway. This integration allows for a powerful combination of serverless computing and API management, enabling you to build scalable and flexible applications. Retrieving the Lambda function event using API Gateway provides a seamless way to trigger and interact with your Lambda functions.

related posts

Leave a Comment

Enable Notifications OK No thanks