Home > database >  AWS Java Lambda Handler to Start/Stop RDS instances
AWS Java Lambda Handler to Start/Stop RDS instances

Time:07-21

I need to create a Java Lambda Handler to start and stop rds instances for AWS. Very confused on how to do this since most of the online resources are in python. I took a look at the AWS Java SDK to see what functions I need to use (startDBInstance, stopDBInstance), but am unsure how to implement them properly as this is my first time doing so.

This is the outline of the Handler.

package com.example.lambda.demo;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

public class Hello implements RequestHandler<Object, String> {

    @Override
    public String handleRequest(Object input, Context context) {
        context.getLogger().log("Input: "   input);

        // TODO: implement your handler
        return "Hello from Lambda";
    }

} 

CodePudding user response:

You are on the right track in terms of using the Java Lambda runtime. To modify RDS resources from a Lambda function, use the RDS Java API within the Lambda function.

software.amazon.awssdk.services.rds.RdsClient

To stop the instance, for example, you call stopDBInstance().

There is a Java Lambda example here that shows you how to use the AWS Java APIs (this example uses serveral service clients) from within a Lambda function.This shows how to create an IAM user that the Lambda function uses, how to specify AWS dependencies in the POM, etc.

For you use case, you need to use the RDS Java Client. You still need to create an IAM user (that your Lambda function uses) and specify the RDS POM dependency in your project's POM. (All shown in the example below).

https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javav2/usecases/workflow_multiple_channels

TIP: WHen looking for examples that uses the AWS SDK for Java, refer to here:

https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javav2

You will find only Java examples, not Python, etc.

  • Related