Showing posts with label #Nodejs18. Show all posts
Showing posts with label #Nodejs18. Show all posts

Saturday, February 7, 2026

Rotating Secrets in AWS Secrets Manager Using AWS Lambda

 Rotating Secrets in AWS Secrets Manager Using AWS Lambda

When building applications in the cloud, one of the most common security mistakes is hardcoding credentials or leaving them unchanged for long periods. It might work during development, but in production, static credentials become a serious risk.

Imagine this scenario:
Your application connects to a database using a username and password stored in environment variables. Months pass, teams change, logs get shared, and suddenly someone realizes the credentials have never been rotated.

This is exactly the type of problem AWS Secrets Manager is designed to solve. It not only stores secrets securely, but also allows you to automatically rotate them using AWS Lambda.

In this guide, we’ll walk through a beginner-friendly, practical approach to rotating database credentials using AWS Secrets Manager and AWS Lambda with Node.js.

Automated Secret Rotation with AWS Lambda

Importance of Secret Rotation

Before jumping into implementation, let’s understand why secret rotation is important.

Risks of Static Credentials

  • Credentials may be exposed in logs or code.

  • Old credentials may still work after employees leave.

  • Compromised credentials may remain valid indefinitely.

Benefits of Automatic Rotation

  • Credentials change periodically.

  • Reduced blast radius in case of compromise.

  • No manual intervention required.

  • Fully managed and auditable.

AWS Secrets Manager automates this process by integrating with Lambda functions.

AWS Secret Rotation Mechanism

When you enable rotation in AWS Secrets Manager:

  1. AWS triggers a Lambda function.

  2. The Lambda performs the rotation logic.

  3. Secrets Manager manages secret versions.

AWS follows a four-step rotation process:

  1. createSecret: Generate new credentials.
  2. setSecret: Apply new credentials to the database.
  3. testSecret: Verify the new credentials work.
  4. finishSecret: Mark the new version as active.

Your Lambda function must handle each of these steps.

Architecture Overview

Simple flow:

  1. Application reads DB credentials from Secrets Manager.

  2. Secrets Manager triggers Lambda on rotation schedule.

  3. Lambda:

    • Generates a new password.

    • Updates the database.

    • Updates the secret value.

Step 1: Create a Secret in AWS Secrets Manager

  1. Goto AWS Secrets Manager.

  2. Click store a new secret.

  3. Choose credential for RDS database.

  4. Enter:

    • Username

    • Password

  5. Choose your Database.
  6. Name the Secret and Finish the setup.

Step 2: Create the Rotation Lambda Function

Create a Lambda function with:

  1. Runtime: Node.js 18+
  2. Role with Permissions to Secrets Manager and Database.

Step 3: Lambda Rotation Function (Node.js Example)

Below is a simplified rotation Lambda example for a MySQL database.

          const AWS = require('aws-sdk');
          const crypto = require('crypto');
          const mysql = require('mysql2/promise');
          
          const secretsManager = new AWS.SecretsManager();
          
          exports.handler = async (event) => {
            const step = event.Step;
            const secretId = event.SecretId;
            const token = event.ClientRequestToken;
            
            console.log(`Rotation step: ${step}`);
            
            switch (step) {
              case "createSecret":
                await createSecret(secretId, token);
                break;
              
              case "setSecret":
                await setSecret(secretId, token);
                break;
              
              case "testSecret":
                await testSecret(secretId, token);
                break;
              
              case "finishSecret":
                await finishSecret(secretId, token);
                break;
              
              default:
                throw new Error("Invalid step");
            }
          };

Step Logic Implementations

1. Create Secret

Generate a new password.

            async function createSecret(secretId, token) {
              const newPassword = crypto.randomBytes(16).toString('hex');
              
              const secret = await secretsManager.getSecretValue({
                SecretId: secretId,
                VersionStage: "AWSCURRENT"
              }).promise();
              
              const currentSecret = JSON.parse(secret.SecretString);
              
              const newSecret = {
                ...currentSecret,
                password: newPassword
              };
              
              await secretsManager.putSecretValue({
                SecretId: secretId,
                ClientRequestToken: token,
                SecretString: JSON.stringify(newSecret),
                VersionStages: ["AWSPENDING"]
              }).promise();
              
              console.log("New secret version created.");
            }

2. Set Secret

Update the database with the new password.

            async function setSecret(secretId, token) {
              const pendingSecret = await secretsManager.getSecretValue({
                SecretId: secretId,
                VersionStage: "AWSPENDING"
              }).promise();
              
              const secret = JSON.parse(pendingSecret.SecretString);
              
              const connection = await mysql.createConnection({
                host: secret.host,
                user: secret.username,
                password: secret.password,
                database: secret.dbname
              });
              
              await connection.execute(
                `ALTER USER '${secret.username}'@'%' IDENTIFIED BY '${secret.password}'`
              );
              
              await connection.end();
              
              console.log("Database password updated.");
            }

3. Test Secret

Verify the new credentials work.

              async function testSecret(secretId, token) {
                const pendingSecret = await secretsManager.getSecretValue({
                  SecretId: secretId,
                  VersionStage: "AWSPENDING"
                }).promise();
                
                const secret = JSON.parse(pendingSecret.SecretString);
                
                const connection = await mysql.createConnection({
                  host: secret.host,
                  user: secret.username,
                  password: secret.password,
                  database: secret.dbname
                });
                
                await connection.execute("SELECT 1");
                await connection.end();
                
                console.log("New credentials verified.");
              }

4. Finish Secret

Promote the new version.

              async function finishSecret(secretId, token) {
                await secretsManager.updateSecretVersionStage({
                  SecretId: secretId,
                  VersionStage: "AWSCURRENT",
                  MoveToVersionId: token
                }).promise();
                
                console.log("Secret rotation completed.");
              }

Step 4: Enable Rotation in Secrets Manager

  1. Open your secret.

  2. Click Enable rotation.

  3. Choose:

    • Rotation Lambda: your function.

    • Rotation schedule: e.g., every 30 days.

AWS will now automatically rotate the secret.

Step 5: Test the Rotation

Trigger rotation manually:

  1. Go to Secrets Manager.

  2. Select your secret.

  3. Click Rotate secret immediately.

Then:

  • Check Lambda logs in CloudWatch.

  • Confirm database password updated.

  • Confirm new secret version marked as AWSCURRENT.

Best Practices for Secret Rotation

1. Use Least Privilege IAM Roles

Your Lambda should only have access to:

  • The specific secret

  • The specific database

2. Use Secure Password Generation

Always use crypto.randomBytes() instead of hardcoded values.

3. Monitor Rotation Failures

Set CloudWatch alarms for:

  • Lambda errors

  • Rotation failures

4. Test Rotation in Non-Production First

Always validate rotation in a staging environment.

Common Beginner Mistakes

  • Forgetting to test the new credentials.

  • Not updating database permissions properly.

  • Using overly broad IAM roles.

  • Ignoring rotation logs.

  • Not verifying the AWSCURRENT stage.

Conclusion

Secret rotation is one of the simplest and most effective ways to improve your application’s security posture. With AWS Secrets Manager and Lambda, you can automate the entire process without manual intervention.

Do not treat secrets as static values. Treat them as short-lived credentials that should be rotated automatically.

Wednesday, December 25, 2024

Understanding Serverless Architecture on AWS: A Beginner's Guide

Understanding Serverless Architecture on AWS: A Beginner's Guide

Serverless architecture has transformed how developers build and deploy applications. With no need to manage infrastructure, developers can focus solely on writing code and delivering business value. AWS, as a leading cloud provider, offers a suite of services tailored for serverless solutions. In this blog, we will explore the fundamentals of serverless architecture, its key components on AWS, and build a practical example using Node.js to resize images—a common use case in real-world applications.

Serverless Architecture

Serverless architecture allows developers to build applications without worrying about provisioning, scaling, or managing servers. Instead of dealing with traditional infrastructure, you rely on managed cloud services to handle compute, storage, and other backend functionalities. With serverless, you only pay for what you use, making it cost-efficient and scalable by default.

Key Benefits of Serverless:

  • Cost Efficiency: Pay only for the execution time of your code, with no idle server costs.

  • Scalability: Automatically scale based on demand.

  • Reduced Operational Overhead: No need to manage servers, patch operating systems, or handle scaling.

  • Faster Development Cycles: Focus on writing code while AWS manages the backend.

Key Benefits of Serverless Architecture

Core AWS Services for Serverless Applications

AWS provides a robust ecosystem for building serverless applications:

  1. AWS Lambda: The compute layer to run your code in response to events.

  2. Amazon API Gateway: Build and manage APIs to interact with your application.

  3. Amazon S3: Scalable storage service for hosting files, such as images and videos.

  4. Amazon DynamoDB: NoSQL database for serverless applications.

  5. AWS Step Functions: Orchestrate workflows across multiple AWS services.

  6. Amazon CloudWatch: Monitor and log your application’s performance.

Core Components of AWS Serverless Architecture

Use Case: Building a Serverless Image Resizing Service

Let’s dive into a practical example where we’ll build a serverless application to resize images. This use case showcases how AWS Lambda, Amazon S3, and Node.js can work together to solve a real-world problem.

Architecture Overview:

  1. Users upload images to an S3 bucket.

  2. An S3 event triggers an AWS Lambda function.

  3. The Lambda function processes the image (resizing it) and stores the resized version in another S3 bucket.

Step-by-Step Guide to Building the Service

Step 1: Set Up Your S3 Buckets

  1. Create two S3 buckets:

    • source-bucket: For uploading the original images.

    • destination-bucket: For storing resized images.

  2. Enable event notifications on the source-bucket to trigger a Lambda function whenever a new object is uploaded.

Step 2: Write the Lambda Function

We’ll use Node.js for our Lambda function. The function will:

  • Fetch the uploaded image from source-bucket.

  • Resize the image using the sharp library.

  • Upload the resized image to destination-bucket.

Install the required Node.js libraries locally:

       npm install sharp @aws-sdk/client-s3

Here’s the code for the Lambda function:

          import { S3Client, GetObjectCommand, PutObjectCommand } from '@aws-sdk/client-s3';
          import sharp from 'sharp';
          
          const s3 = new S3Client();
          
          export const handler = async (event) => {
            try {
              // Extract bucket and object key from the event
              const sourceBucket = event.Records[0].s3.bucket.name;
              const objectKey = event.Records[0].s3.object.key;
              // Replace with your destination bucket name
              const destinationBucket = 'destination-bucket'; 
              
              // Get the image from the source bucket
              const getObjectCommand = new GetObjectCommand({
                Bucket: sourceBucket,
                Key: objectKey,
              });
              const imageResponse = await s3.send(getObjectCommand);
              
              // Read the image body
              const imageBuffer = await imageResponse.Body.transformToByteArray();
              
              // Resize the image using sharp
              const resizedImage = await sharp(imageBuffer)
                .resize(300, 300) // Resize to 300x300
                .toBuffer();
              
              // Upload the resized image to the destination bucket
              const putObjectCommand = new PutObjectCommand({
                Bucket: destinationBucket,
                Key: `resized-${objectKey}`,
                Body: resizedImage,
                ContentType: 'image/jpeg',
              });
              await s3.send(putObjectCommand);
              
              console.log(`Successfully resized and uploaded ${objectKey}`);
              } catch (error) {
                console.error('Error processing image:', error);
                throw error;
            }
          };

Step 3: Deploy the Lambda Function

  1. Create a Lambda function in the AWS Management Console.

  2. Upload the Node.js code as a .zip file.

  3. Assign the function an IAM role with the necessary permissions to:

    • Read from source-bucket.

    • Write to destination-bucket.

Step 4: Configure the Event Trigger

In the S3 source-bucket settings, configure an event notification to trigger the Lambda function whenever an object is created.

Step 5: Test the Application

  1. Upload an image to the source-bucket.

  2. Verify that the resized image appears in the destination-bucket.

  3. Check the CloudWatch logs for detailed logs of the Lambda function’s execution.

Best Practices for Serverless Applications

  1. Optimize Cold Starts: Use smaller Lambda packages and keep the runtime lightweight.

  2. Secure Secrets: Use AWS Secrets Manager to securely store API keys and credentials.

  3. Enable Monitoring: Use Amazon CloudWatch to track metrics and set alarms for performance issues.

  4. Use IAM Policies: Grant least privilege permissions to Lambda functions and other resources.

  5. Leverage Infrastructure as Code (IaC): Use tools like AWS CloudFormation or Terraform to manage serverless resources programmatically.

Best Practices for Serverless Applications

Conclusion

Serverless architecture is a game-changer for developers looking to build scalable, cost-effective applications without managing infrastructure. By leveraging services like AWS Lambda and S3, we’ve demonstrated how easy it is to create a real-world image resizing service. With the right practices and tools, you can unlock the full potential of serverless applications on AWS.

Are you ready to go serverless? Start exploring AWS’s serverless ecosystem and share your experiences in the comments below!

#AWS #Serverless #Lambda #CloudComputing #NodeJS


Sunday, December 8, 2024

Building a Serverless REST API with AWS Lambda and API Gateway

Building a Serverless REST API with AWS Lambda and API Gateway

In the modern development landscape, serverless architecture has gained immense popularity due to its cost efficiency, scalability, and ease of use. AWS Lambda and API Gateway form a powerful duo for creating serverless REST APIs. This blog will guide you through creating a simple CRUD API using AWS Lambda, API Gateway, and Node.js with TypeScript.

Wednesday, October 2, 2024

Getting Started with AWS Lambda: Simplifying Serverless Computing

Getting Started with AWS Lambda: Simplifying Serverless Computing

A developer

Introduction

In the rapidly evolving world of cloud computing, developers constantly look for ways to build scalable, cost-effective, and easily manageable applications. AWS Lambda—a powerful, serverless computing service that allows you to run your code without worrying about the underlying infrastructure. By taking care of server provisioning, scaling, and management, AWS Lambda lets you focus solely on what matters most—your application logic.

In this guide, we’ll explore the basic usage of AWS Lambda, highlight its ease of creation and maintenance, and look at its essential features such as monitoring and logging. Whether you're an AWS intermediate user or someone starting out with serverless computing, this blog will help you get comfortable with AWS Lambda and make the most of its features.

Understanding AWS Lambda

AWS Lambda is a serverless compute service that allows you to run your code in response to events and automatically manages the compute resources. It executes your code only when triggered by events, such as changes in an S3 bucket, an update in a DynamoDB table, or an HTTP request from an API Gateway.

Key points:

  • No servers to manage: AWS takes care of the infrastructure, including provisioning, scaling, patching, and monitoring the servers.
  • Automatic scaling: Lambda automatically scales up by running more instances of your function to meet demand.
  • Cost-efficient: You only pay for the compute time that your function uses, which is billed in milliseconds. No cost is incurred when your function is idle.

Advantages of Using AWS Lambda

AWS Lambda stands out due to its simplicity and ability to offload the infrastructure management process to AWS. Here are some key reasons why AWS Lambda is favored by developers:

  1. Simplified Development: With Lambda, you can focus purely on your code. There's no need to worry about provisioning or managing servers.
  2. Scalability: AWS Lambda automatically scales to meet the needs of your application, whether you’re processing one event or one million events.
  3. Cost-Effective: Pay only for what you use. AWS Lambda charges for the execution duration of your code, making it a very efficient option for many use cases.
  4. Event-Driven Architecture: AWS Lambda can be easily integrated with other AWS services like S3, DynamoDB, SNS, and more, making it highly suitable for event-driven applications.


Getting Started with AWS Lambda

Setting Up Your First Lambda Function

Let's walk through the steps to create a basic AWS Lambda function that processes an event from an S3 bucket. In this scenario, whenever a new object is uploaded to an S3 bucket, the Lambda function will trigger, retrieve the object details, and log them.

1. Navigate to the AWS Lambda Console:

  • In the AWS Management Console, search for “Lambda” and select Lambda from the services list.
  • Click the Create function button to start.

2. Choose a Basic Function Setup:

  • Choose the Author from scratch option.
  • Name your function (e.g., ProcessS3Uploads).
  • Select Node.js, Python, or another runtime you're comfortable with.
  • Assign an existing execution role or create a new one. The execution role gives your function permission to access other AWS resources, such as S3 or CloudWatch.

3. Define Your Lambda Function Code: 

Here’s a simple Node.js example to log the details of an uploaded object from S3:
Paste the code into the Code section of the Lambda function editor.
const AWS = require('aws-sdk');
const s3 = new AWS.S3();

exports.handler = async (event) => {
    const bucketName = event.Records[0].s3.bucket.name;
    const objectKey = event.Records[0].s3.object.key;

    console.log(`Object ${objectKey} uploaded to bucket ${bucketName}`);

    return {
        statusCode: 200,
        body: `Object processed successfully.`,
    };
};

4. Set Up the Trigger (S3 Event): 

  • In the Designer section, click on the + Add Trigger button.
  • Choose S3 from the list of available triggers.
  • Configure the trigger to activate whenever an object is uploaded to your S3 bucket.

5. Test Your Lambda Function:

  • Once the function is created, you can test it by manually uploading an object to the specified S3 bucket.
  • The Lambda function should be triggered automatically, and the object details should be logged.

Lambda Logging and Monitoring

As your application scales, monitoring and logging become essential for troubleshooting and performance optimization. AWS provides several tools to help you maintain and debug Lambda functions.

Logging with CloudWatch Logs

AWS Lambda automatically integrates with Amazon CloudWatch Logs, which collects and stores log data from your Lambda function’s execution. Every time your function runs, it generates log data that is sent to CloudWatch Logs.

How to access logs:

  1. In the Lambda Console, go to the Monitoring tab for your function.
  2. Click on the View logs in CloudWatch button.
  3. You’ll be redirected to the CloudWatch Logs, where you can view detailed logs of each function execution, including input events, error messages, and execution time.

By inserting console.log() statements in your Lambda code, you can output important debugging information, making it easier to trace the behavior of your function.

Monitoring Performance with CloudWatch Metrics

Lambda also provides key performance metrics in CloudWatch, such as:

  • Invocations: The number of times your function has been invoked.
  • Duration: The time it takes for your function to complete.
  • Errors: The number of errors encountered during function execution.
  • Throttles: The number of times your function was throttled due to exceeding concurrency limits.

These metrics help you monitor the health and performance of your Lambda function, allowing you to make optimizations when necessary.

AWS X-Ray for Debugging

If you want even deeper insights into your Lambda functions, including how they interact with other services, you can enable AWS X-Ray. X-Ray traces the execution path of your application, capturing details like request latency, service interactions, and errors.

Enabling X-Ray:

  • In the Lambda Console, navigate to the Configuration tab.
  • Under Monitoring tools, toggle the switch to enable X-Ray.

Best Practices for Maintaining AWS Lambda Functions

While Lambda functions are designed to be simple to create and manage, following best practices ensures your serverless applications remain efficient and cost-effective:

1. Keep Functions Lightweight:

  • Keep the logic in your Lambda functions as simple as possible. Offload non-essential logic or complex workflows to other services, like SQS or Step Functions.

2. Use Environment Variables:

  • Store configuration values like database connection strings, API keys, and S3 bucket names in environment variables. This keeps your code clean and prevents hardcoding sensitive data.

3. Leverage Lambda Layers:

  • Use Lambda Layers to include external libraries, dependencies, or shared code that multiple Lambda functions can use, keeping your function deployment package smaller.

4. Use Dead Letter Queues (DLQs):

  • Set up a DLQ (e.g., an SQS queue) for Lambda functions that fail consistently. This helps ensure failed events are not lost and can be retried later.

5. Optimize Cold Starts:

  • To minimize the cold start latency, especially for functions that don’t run frequently, consider using provisioned concurrency to pre-warm instances of your function.

Conclusion

AWS Lambda has transformed the way developers approach serverless computing. By abstracting away the complexities of managing servers, AWS Lambda allows you to focus on writing code that responds to events in real-time. Whether you’re building a simple data processing pipeline or a complex, event-driven microservice, AWS Lambda simplifies the development process, offers seamless scalability, and helps you save costs.

With Lambda’s built-in support for monitoring, logging, and debugging through CloudWatch and X-Ray, maintaining your functions is a breeze. Now that you've got a good handle on getting started with AWS Lambda, it’s time to start building!

Key Takeaways

  1. Simplicity: Lambda is serverless, meaning no infrastructure to manage.
  2. Scalability: Automatically scales based on the number of events.
  3. Cost-Efficiency: Pay only for the compute time your code uses.
  4. Monitoring & Logging: Integrates with CloudWatch and X-Ray for performance insights.

By following the steps outlined in this guide, you'll be able to set up, monitor, and maintain your AWS Lambda functions easily. Whether you're building small functions or architecting large-scale serverless applications, AWS Lambda will be a key tool in your AWS toolkit. 


Happy coding!

Sunday, December 3, 2023

Installing Node.Js 18 in AWS CodeBuild using the Ubuntu build image

Installing Node.Js 18 in AWS CodeBuild using the Ubuntu build image

Node.Js and AWS CodeBuild

AWS CodeBuild is a fully managed build service that can compile source code, run tests, and produce software packages that are ready to deploy. It provides a wide range of pre-built build environments for popular programming languages, frameworks, and tools, including Node.js.

Node.js is a popular open-source JavaScript runtime built on Chrome’s V8 JavaScript engine. It allows developers to build fast, scalable, and highly performant server-side applications using JavaScript. The latest stable version of Node.js is 18.x, which was released on April 19, 2022.

Why Ubuntu not Amazon Linux 2 images?

Amazon Linux 2 is a Linux distribution that is designed to work seamlessly with AWS services. However, it is based on the CentOS/RHEL Linux distribution, which has a slower release cycle for new software versions compared to other Linux distributions. This means that Amazon Linux 2 may not have the latest version of some software packages, including Node.js.

In this article, we’ll learn how to install Node.js 18 with available images in AWS CodeBuild.

Step 1: Create a CodeBuild project

To get started, log in to the AWS Management Console and navigate to the AWS CodeBuild service. Click on the “Create project” button to create a new CodeBuild project.

Give your project a meaningful name, and choose the source code provider, source code location, and build environment. For this example, we’ll use the default “Ubuntu” environment with image identifier “aws/codebuild/standard:6.0”.

Step 2: Specify the build commands

In the “Buildspec” section of your CodeBuild project configuration, you can specify the build commands that CodeBuild should run. We’ll use the following commands to install Node.js 18:

version: 0.2
phases:
install:
commands:
- echo "Installing Node.js 18"
- n 18
build:
commands:
- echo "Build commands go here"

The commands section runs the echo command to indicate that we're installing Node.js 18 and then installs it using n 18 command.

You can replace the echo command in the build phase with your own build commands, such as npm install or yarn build.

Step 3: Start the build

Once you’ve specified the build commands in your CodeBuild project, you can start a new build by clicking on the “Start build” button. CodeBuild will spin up a new instance of the build environment, install Node.js 18, and run the build commands.

After the build completes, you can view the build logs to see the output of the build commands. If there were any errors or warnings, you can use the logs to diagnose the issue and make any necessary changes to your build commands.

Conclusion

In this article, we learned how to install Node.js 18 with available images in AWS CodeBuild. By using the install phase in your buildspec, you can easily install any version of Node.js that is available in the CodeBuild environment.

While Amazon Linux 2 does not currently support Node.js 18, we can still use AWS CodeBuild to install Node.js 18 by using a different build image. By following the steps outlined above, you can easily install Node.js 18 in AWS CodeBuild and use it to build and test your Node.js applications. With Node.js 18, you can take advantage of its new features and improvements to build faster and more efficient applications.