Creating Lambda Function using CDK
Here’s an example of how you can use AWS CDK (Cloud Development Kit) to create a Lambda function that fetches a CSV file from an S3 bucket:
- First, make sure you have installed AWS CDK and have initialized a new project.
- Create a new TypeScript file, let’s call it “lambda-fetch-csv.ts”.
- Import the necessary AWS CDK modules:

4. Define a new class that extends cdk.Construct:

5. Create a new S3 bucket that will hold your CSV file:

6. Define a new Lambda function:

In this example, we’re using the “NODEJS_14_X” runtime, setting the handler to “index.handler”, and configuring the function to timeout after 10 seconds and use 256MB of memory. We’re also setting two environment variables, “BUCKET_NAME” and “FILE_NAME”, which will be used later to fetch the CSV file.
7. Define a new IAM role that will allow your Lambda function to read from the S3 bucket:

This creates a new IAM role that is assumed by the Lambda service and grants it permission to read from the S3 bucket.
8. Define the Lambda function code:
Create a new directory called “lambda” in your project directory and create an “index.js” file inside it. In this file, add the following code:

This code uses the AWS SDK to fetch the CSV file from the S3 bucket specified in the environment variables, and then processes the data as needed. You can replace the comment with your own code to process the CSV data.
Thank You for Reading…..