Lädt...


🔧 Modify the AWS RDS Instance size using Lambda Function


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

This article gives you an overview of the AWS Lambda function to modify the RDS instance class using Python language in Lambda function without stopping the RDS instance.

Let’s follow this article to modify RDS instance using Lambda function.

Steps to create AWS Lambda Function for AWS RDS Instance class

we use the following steps to configure a lambda function.

Step1: Create an IAM Policy

The first step is to create IAM policy to gain access to RDS actions and AWS CloudWatch log events.

Navigate to IAM in the services and click on Policies => Create Policy.

RDS Policy

Same policy add CloudWatch Logs

Creating policy

Step2: Create an IAM Role and attach “Lambda_RDS_modification_policy”

In this step, we are creating an IAM role and attach the policy created in the previous step. Click on Roles -> Create Role:

IAM Role

Select policy

Create IAM Role with selected trusted entities

IAM Role created

Step3: Create an AWS Lambda Function

Now we will create AWS Lambda function to modify RDS instance class. First you have to select “Author from scratch” -> Function Name -> Runtime (Python3.7 or 3.8) -> Existing Role “RDS_Lambda_Role”

Lambda function creation

Click on Create Function

Lambda Function is created

Add inline policy in existing IAM Role

Now, open a new tab for the IAM role and edit the existing Role RDS_Lambda. In the summary page, click on Add Inline Policy

Add inline policy

In the Inline policy editor, paste the following JSON. Here, you note that we used the AWS lambda ARN in the resource section. You can copy ARN for your existing lambda ARN.

Lambda ARN follows the format: arn:aws:lambda:Region-AWS Account:function:lambda_function_Name

{   
   "Version": "2012-10-17",
   "Statement": [
{
      "Effect": "Allow",
      "Action": "lambda:GetFunctionConfiguration",
      "Resource": "arn:aws:lambda:us-east-1:11111111111:function:RDSStartFunction"
      }
   ]
}

Manual RDS Schedule Policy

RDS Schedule Role

Step4: Function Code: Scroll down and paste the Python code inside the editor. You need to select appropriate language in the run time. I go with the latest version Python 3.8

This is the Python code to modify RDS instance class

Python Code:

Here we are using 2 environment variables:

  • DBinstance

  • DBinstanceClass

import sys
import botocore
import boto3
import json
from botocore.exceptions import ClientError
def lambda_handler(event, context):
    rds = boto3.client('rds')
    lambdaFunc = boto3.client('lambda')
    print ('Trying to get Environment variable')
    try:
        funcResponse = lambdaFunc.get_function_configuration(
            FunctionName='RDS_Instance_Modification_Function'
       )
        DBinstance = funcResponse['Environment']['Variables']['DBInstanceName']
        DBinstanceClass = funcResponse['Environment']['Variables']['DBinstanceClass']

        print (f'Starting RDS service for DBInstance : {DBinstance}')
        print (f'RDS instance class : {DBinstanceClass}')


        response = rds.modify_db_instance(DBInstanceIdentifier=DBinstance, DBInstanceClass=DBinstanceClass, ApplyImmediately=True)

        print (f'Success :: {response} ') 
        return json.dumps(dict(abc=123))
    except ClientError as e:
return json.dumps(dict(error=str(e)))

    return json.dumps(dict(abc=12345))

Creating Environment Variables:

Adding env variables

Step5: I already created one testdb RDS instance for the testing

RDS Test Instance

Step6: Now we will test the Lambda Function

Click on the “Test” button

Test Lambda Function

First time when you run it and as you already entered the “db.t2.micro” so the result will be like:

Result of test function

But now I will change the “DBinstanceClass” to “db.t2.small” so this time it will successfully modify the AWS RDS instance “testdb” class to “db.t2.small”

Modify the RDS Instance Type

Now you will get the following logs after running the lambda function

Logs of function

Here you will see that the RDS instance is now “modifying” status. It will take some time to show you the result as this instance size will be change from “db.t2.micro” to “db.t2.small”

Modifying RDS Instance

Final result: after modification you will see that now the instance size is “db.t2.small” and status is now “Available”

Modified Successfully

...

🔧 Modify the AWS RDS Instance size using Lambda Function


📈 83.95 Punkte
🔧 Programmierung

🔧 Modify the AWS RDS Instance size using Lambda Function


📈 83.95 Punkte
🔧 Programmierung

🔧 Automating AWS EC2 and RDS Instance Management with AWS Lambda


📈 49.98 Punkte
🔧 Programmierung

🔧 Como escalar las consultas de AWS Lambda RDS Proxy conéctese a RDS (Aurora)


📈 46.05 Punkte
🔧 Programmierung

🔧 Evaluation metric, objective function, loss function, cost function, scoring function, error function


📈 45.11 Punkte
🔧 Programmierung

🔧 Connecting AWS Lambda with Amazon RDS using AWS CDK and Node.js


📈 41.02 Punkte
🔧 Programmierung

🔧 AWS services EC2 | S3 | RDS | Lambda Function | VPC


📈 40.52 Punkte
🔧 Programmierung

🔧 Creating a free-tier AWS RDS PostgreSQL instance using Terraform


📈 36.96 Punkte
🔧 Programmierung

🔧 How to Create and Launch an EC2 Instance with IAM Role Attachment Using AWS Instance Connect


📈 35.31 Punkte
🔧 Programmierung

🔧 Learning AWS - DVA - Day 11: Amazon Aurora - RDS Security - RDS Proxy


📈 34.67 Punkte
🔧 Programmierung

🔧 Learning AWS - DVA - Day 11: Amazon Aurora - RDS Security - RDS Proxy


📈 34.67 Punkte
🔧 Programmierung

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


📈 33.92 Punkte
🔧 Programmierung

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


📈 33.92 Punkte
🔧 Programmierung

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


📈 33.92 Punkte
🔧 Programmierung

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


📈 33.92 Punkte
🔧 Programmierung

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


📈 33.79 Punkte
🔧 Programmierung

🔧 Automating Instance Management Using AWS Lambda and EventBridge


📈 33.79 Punkte
🔧 Programmierung

🔧 Create a secure AWS RDS instance with CDK


📈 33.02 Punkte
🔧 Programmierung

🔧 Creating an AWS MySQL RDS Instance with Terraform


📈 33.02 Punkte
🔧 Programmierung

🔧 Spring Boot 3 application on AWS Lambda - Part 9 Develop application with Spring Cloud Function AWS


📈 31.56 Punkte
🔧 Programmierung

🔧 AWS Lambda with RDS Data API (TypeScript)


📈 31.5 Punkte
🔧 Programmierung

🔧 Run Flyway DB migrations with AWS Lambda and RDS - Part 1


📈 31.5 Punkte
🔧 Programmierung

🔧 Securing AWS Infrastructure: How to Safely Connect Lambda Functions to RDS Databases via Secrets Manager


📈 31.5 Punkte
🔧 Programmierung

🔧 Seamless Integration: Connecting AWS Lambda to RDS and Writing Data Effortlessly


📈 31.5 Punkte
🔧 Programmierung

🐧 How to Modify Secrets with AWS Secret Manager Using AWS Console?


📈 31.3 Punkte
🐧 Linux Tipps

matomo