aws-sdk-go-v2

AWS Golang SDK v2 - How to get Total Memory of RDS DB Instance?


I'm collecting information and metrics about RDS Instances from CloudWatch and RDS services.

I tried to get metrics and information using the following methods:

Unfortunately, there is no information about the Total Memory of DB Instance.


Solution

  • I'm assuming you want to get the memory for the underlying EC2 that you run your RDS. If you need the memory allocated to the DB, it'll depend on the DB engine type. For example, in PostgreSQL, you need to query wal_buffers.

    To get the instance details for your RDS, you need to get the instance type from rds.DescribeDBInstances and then query for the instance type details from ec2.DescribeInstanceTypes

    import (
        "context"
        "log"
        "strings"
    
        "github.com/aws/aws-sdk-go-v2/aws"
        "github.com/aws/aws-sdk-go-v2/service/ec2"
        ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
        "github.com/aws/aws-sdk-go-v2/service/rds"
    )
    
    func getRdsMemoryMB(cfg aws.Config) int64 {
        rdsClient := rds.NewFromConfig(cfg)
        // get the instance details
        input := rds.DescribeDBInstancesInput{
            // if you omit this paramter, DescribeDBInstances will return the info for all instances
            DBInstanceIdentifier: aws.String("instance-1-ARN"),
        }
        output, err := rdsClient.DescribeDBInstances(context.Background(),
            &input,
            func(opt *rds.Options) { opt.Region = "us-east-1" })
        if err != nil {
            log.Fatal(err)
        }
        // get the instance type details
        ec2Client := ec2.NewFromConfig(cfg)
        instanceName := strings.TrimPrefix(*output.DBInstances[0].DBInstanceClass, "db.")
        params := ec2.DescribeInstanceTypesInput{
            InstanceTypes: []ec2types.InstanceType{ec2types.InstanceType(instanceName)},
        }
        ec2Output, err := ec2Client.DescribeInstanceTypes(context.Background(), &params)
        if err != nil {
            log.Fatal(err)
        }
        return *ec2Output.InstanceTypes[0].MemoryInfo.SizeInMiB
    }