Lädt...


🔧 Deploying a Serverless Application with AWS Lambda and EventBridge: A Detailed Guide Day 30 of my 90 -Day Devops Journey


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Welcome to day 30! This guide details creating a serverless application using Node.js, AWS Lambda, and EventBridge, triggered by S3 file uploads.

Before You Begin:

  • AWS Account: You need an active AWS account.

  • IAM User with Permissions: Create an IAM user with sufficient permissions. Avoid using root credentials. The user needs
    permissions to access S3 (reading objects), Lambda (creating functions, invoking functions), and EventBridge (creating rules and
    targets). A well-defined policy minimizing permissions is crucial for security. Use the AWS console's IAM service to create a user
    and attach a policy granting these permissions.

Image description

Step 1: Create the S3 Bucket

  1. Navigate to the S3 console: Access the AWS Management Console and go to the S3 service.
  2. Create bucket: Click "Create bucket."
  3. Bucket name: Choose a globally unique name (e.g., your-unique-name-eventbridge-bucket). It must be lowercase and globally unique. Avoid periods (.) in the name.
  4. Region: Select a region geographically close to your users.
  5. Object Ownership: Choose "Bucket owner enforced."
  6. Versioning: Consider enabling versioning for data protection.
  7. Encryption: Enable server-side encryption (SSE-S3 or SSE-KMS) to protect your data at rest.
  8. Block Public Access: Crucially, ensure "Block all public access" is enabled. This prevents unauthorized access to your bucket.
  9. Tags (Optional): Add tags for organization and cost allocation.
  10. Create bucket: Click "Create bucket."

Image description

Step 2: Create the Lambda Function (Node.js)

  1. Navigate to the Lambda console: Go to the AWS Lambda service.
  2. Create function: Click "Create function."
  3. Author from scratch: Choose this option.
  4. Function name: Use a descriptive name (e.g., processS3Upload).
  5. Runtime: Select Node.js 16.x (or a compatible version).
  6. Permissions:
    • Create a new role: Choose this option.
    • Role name: Give it a descriptive name (e.g., lambda_s3_eventbridge_role).
    • Policy: Create a custom policy or use a managed policy and add necessary permissions. The policy must grant permissions to:
      • s3:GetObject (to read objects from S3)
      • logs:CreateLogGroup, logs:CreateLogStream, logs:PutLogEvents (to write logs to CloudWatch)
  7. Code:
   const AWS = require('aws-sdk');
   const s3 = new AWS.S3();

   exports.handler = async (event) => {
       try {
           const record = event.Records[0];
           const bucketName = record.s3.bucket.name;
           const key = decodeURIComponent(record.s3.object.key.replace(/\+/g, ' ')); // Decode URL-encoded key

           console.log(`File ${key} uploaded to bucket ${bucketName}`);

           const params = { Bucket: bucketName, Key: key };
           const data = await s3.getObject(params).promise();
           // Process the file data from data.Body.  Example:
           const fileContent = data.Body.toString();
           console.log(`File content: ${fileContent}`); // Or perform more complex processing

           return { statusCode: 200, body: 'File processed successfully' };
       } catch (error) {
           console.error('Error processing file:', error);
           return { statusCode: 500, body: 'Error processing file' }; // Handle errors appropriately
       }
   };
  1. Create function: Click "Create function."

Image description

Step 3: Create the EventBridge Rule

  1. Navigate to the EventBridge console: Go to the AWS EventBridge service.
  2. Create rule: Click "Create rule."
  3. Name: Give it a name (e.g., s3FileUploadRule).
  4. Event source: Select "AWS Events."
  5. Event pattern: Use this JSON pattern (replace with your bucket name):
   {
       "source": ["aws.s3"],
       "detail-type": ["Object Created"],
       "detail": {
           "bucket": {
               "name": ["your-unique-name-eventbridge-bucket"]
           }
       }
   }
  1. Target:
    • Choose a target: Select "Lambda function."
    • Select function: Choose your Lambda function (processS3Upload).
  2. Create rule: Click "Create rule."

Image description

Step 4: Test the Setup

  1. Upload a file to your S3 bucket.
  2. Check CloudWatch Logs for your Lambda function. You should see logs indicating successful execution.

Step 5: Verify CloudWatch Logs

  1. Go to the CloudWatch console.
  2. Navigate to Logs.
  3. Find the log group for your Lambda function.
  4. Verify that the logs show the event details (bucket name, file name) and the output of your Lambda function.

Image description

Additional Enhancements:

  • Error Handling: The improved Lambda code includes error handling. Consider adding more robust error handling and logging.
  • Dead-Letter Queue (DLQ): Configure a DLQ for your EventBridge rule to handle failed events.
  • Asynchronous Processing: For long-running tasks, use SQS or another asynchronous mechanism to avoid Lambda timeouts.
  • Security Best Practices: Regularly review and update IAM permissions to ensure least privilege access.
...

🔧 Spring Boot 3 application on AWS Lambda - Part 6 Develop application with AWS Lambda Web Adapter


📈 46.15 Punkte
🔧 Programmierung

🔧 Deploying Serverless Embedding App with AWS CDK, Lambda and Amazon Aurora PostgreSQL


📈 41.06 Punkte
🔧 Programmierung

🔧 Spring Boot 3 application on AWS Lambda - Part 5 Introduction to AWS Lambda Web Adapter


📈 40.83 Punkte
🔧 Programmierung

🔧 Enhancing AWS Lambda with AWS Lambda Powertools: A Complete Guide to CRUD Operations in DynamoDB


📈 40.22 Punkte
🔧 Programmierung

🔧 Automating Instance Management Using AWS Lambda and EventBridge


📈 40.12 Punkte
🔧 Programmierung

🔧 Automating EC2 Instance Management with AWS Lambda and EventBridge Using Terraform


📈 40.12 Punkte
🔧 Programmierung

🔧 Deploying Java Serverless Functions as AWS Lambda


📈 39.75 Punkte
🔧 Programmierung

🔧 Deploying a Serverless Function with AWS Lambda


📈 39.75 Punkte
🔧 Programmierung

🔧 Spring Boot 3 application on AWS Lambda - Part 2 Introduction to AWS Serverless Java Container


📈 39.68 Punkte
🔧 Programmierung

🔧 Serverless Application using AWS Lambda ,Api Gateway,AWS Amplify


📈 39.68 Punkte
🔧 Programmierung

🔧 Serverless Application using AWS Lambda ,Api Gateway,AWS Amplify


📈 39.68 Punkte
🔧 Programmierung

🎥 Serverless Node.js Tutorial – Neon Serverless Postgres, AWS Lambda, Next.js, Vercel


📈 39.21 Punkte
🎥 Video | Youtube

🔧 Deploying a serverless web application on S3, API gateway, lambda, DynamoDB


📈 39.19 Punkte
🔧 Programmierung

🔧 Say Goodbye to Orphaned Snapshots: Automate Cleanup with Serverless, Terraform, and AWS EventBridge!


📈 38.97 Punkte
🔧 Programmierung

🔧 Step-by-Step Guide to Deploying a Serverless Application on AWS


📈 37.91 Punkte
🔧 Programmierung

🔧 Learn serverless on AWS step-by-step - Schedule tasks with EventBridge Scheduler


📈 37.66 Punkte
🔧 Programmierung

🔧 Weaving Your Cloud Together: Serverless Orchestration with AWS EventBridge


📈 37.66 Punkte
🔧 Programmierung

🔧 Building an AI powered and Serverless meal planner with OpenAI, AWS Step functions, AWS Lambda and CDK


📈 36.97 Punkte
🔧 Programmierung

🔧 Going Serverless with AWS Lambda: My Journey to Scalable Web Apps


📈 36.88 Punkte
🔧 Programmierung

🔧 Using Lambda and EventBridge to automatically stop and start EC2 Instances


📈 35.55 Punkte
🔧 Programmierung

🔧 How to Import Pandas(library) in AWS Lambda Functions - AWS Lambda Layers


📈 35.5 Punkte
🔧 Programmierung

🔧 Deploying Applications on AWS: A Beginner's Guide to EC2, S3, Lambda, and More


📈 35.04 Punkte
🔧 Programmierung

🔧 Deploying AWS Lambda Functions with API Gateway to DynamoDB using AWS SAM on LocalStack


📈 34.9 Punkte
🔧 Programmierung

matomo