Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know RDS free storage

I've created a RDS postgres instance with size of 65GB initially. Is it possible to get free space available using any postgres query..

if not, how can I achieve the same ??

Thank you in advance..

like image 273
Phani Avatar asked Nov 16 '17 13:11

Phani


People also ask

How do I check my free storage on RDS?

After you create the alarm, open the Amazon RDS console, and then choose Databases from the navigation pane. Choose the name of your database, and then choose the Monitoring tab to view the metrics for FreeStorageSpace.

How do I check my AWS RDS storage?

View the VolumeBytesUsed in the RDS consoleChoose Databases from the navigation pane, and then choose the name of the DB instance that you want to monitor. Choose the Monitoring tab. From the CloudWatch section, enter [Billed] Volume Bytes Used, or choose it from the list. The storage use appears on the graph.

Is RDS included in free tier?

AWS Free Tier includes 750hrs of Amazon Relational Database Service (RDS) in a db.

How do I check my RDS data?

Sign in to the AWS Management Console and open the Amazon RDS console at https://console.aws.amazon.com/rds/ . In the navigation pane, choose Databases to display a list of your DB instances. Choose the name of the MySQL DB instance to display its details. On the Connectivity & security tab, copy the endpoint.


1 Answers

A couple ways to do it

Using the AWS Console

Go to the RDS console and select the region your database is in. Click on the Show Monitoring button and pick your database instance. There will be a graph (like below image) that shows Free Storage Space.

This is documented over at AWS RDS documentation.

Example AWS RDS console

Using the API via AWS CLI

Alternatively, you can use the AWS API to get the information from cloudwatch.

I will show how to do this with the AWS CLI.

This assumes you have set up the AWS CLI credentials. I export AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY in my environment variables, but there are multiple ways to configure the CLI (or SDKS).

REGION="eu-west-1"
START="$(date -u -d '5 minutes ago' '+%Y-%m-%dT%T')"
END="$(date -u '+%Y-%m-%dT%T')"
INSTANCE_NAME="tstirldbopgs001"

AWS_DEFAULT_REGION="$REGION" aws cloudwatch get-metric-statistics \
  --namespace AWS/RDS --metric-name FreeStorageSpace \
  --start-time $START --end-time $END --period 300 \
  --statistics Average \
  --dimensions "Name=DBInstanceIdentifier, Value=${INSTANCE_NAME}"

{
    "Label": "FreeStorageSpace",
    "Datapoints": [
        {
            "Timestamp": "2017-11-16T14:01:00Z",
            "Average": 95406264320.0,
            "Unit": "Bytes"
        }
    ]
}

Using the API via Java SDK

Here's a rudimentary example of how to get the same data via the Java AWS SDK, using the Cloudwatch API.

build.gradle contents

apply plugin: 'java'
apply plugin: 'application'

sourceCompatibility = 1.8

repositories {
    jcenter()
}

dependencies {
    compile 'com.amazonaws:aws-java-sdk-cloudwatch:1.11.232'
}

mainClassName = 'GetRDSInfo'

Java class

Again, I rely on the credential chain to get AWS API credentials (I set them in my environment). You can change the call to the builder to change this behavior (see Working with AWS Credentials documentation).

import java.util.Calendar;
import java.util.Date;

import com.amazonaws.regions.Regions;
import com.amazonaws.services.cloudwatch.AmazonCloudWatch;
import com.amazonaws.services.cloudwatch.AmazonCloudWatchClientBuilder;
import com.amazonaws.services.cloudwatch.model.GetMetricStatisticsRequest;
import com.amazonaws.services.cloudwatch.model.GetMetricStatisticsResult;
import com.amazonaws.services.cloudwatch.model.StandardUnit;
import com.amazonaws.services.cloudwatch.model.Dimension;
import com.amazonaws.services.cloudwatch.model.Datapoint;

public class GetRDSInfo {
    public static void main(String[] args) {
        final long GIGABYTE = 1024L * 1024L * 1024L;

        // calculate our endTime as now and startTime as 5 minutes ago.
        Calendar cal = Calendar.getInstance();
        Date endTime = cal.getTime();
        cal.add(Calendar.MINUTE, -5);
        Date startTime = cal.getTime();

        String dbIdentifier = "tstirldbopgs001";
        Regions region = Regions.EU_WEST_1;

        Dimension dim = new Dimension()
            .withName("DBInstanceIdentifier")
            .withValue(dbIdentifier);

        final AmazonCloudWatch cw = AmazonCloudWatchClientBuilder.standard()
                                        .withRegion(region)
                                        .build();

        GetMetricStatisticsRequest req = new GetMetricStatisticsRequest()
            .withNamespace("AWS/RDS")
            .withMetricName("FreeStorageSpace")
            .withStatistics("Average")
            .withStartTime(startTime)
            .withEndTime(endTime)
            .withDimensions(dim)
            .withPeriod(300);

        GetMetricStatisticsResult res = cw.getMetricStatistics(req);

        for (Datapoint dp : res.getDatapoints()) {
            // We requested only the average free space over the last 5 minutes
            // so we only have one datapoint
            double freespaceGigs = dp.getAverage() / GIGABYTE;
            System.out.println(String.format("Free Space: %.2f GB", freespaceGigs));
        }
    }
}

Example Java Code Execution

> gradle run

> Task :run
Free Space: 88.85 GB


BUILD SUCCESSFUL in 7s
like image 187
逆さま Avatar answered Nov 16 '22 01:11

逆さま