AWS Tip

Best AWS, DevOps, Serverless, and more from top Medium writers .

Follow publication

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:

  1. First, make sure you have installed AWS CDK and have initialized a new project.
  2. Create a new TypeScript file, let’s call it “lambda-fetch-csv.ts”.
  3. 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…..

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Published in AWS Tip

Best AWS, DevOps, Serverless, and more from top Medium writers .

Written by Adesh Kotgirwar

Software engineer, passion for technology and its potential to solve real-world problems has driven me to pursue a career in software engineering.

No responses yet

Write a response