RDS instances script automation - amazon-web-services

I am trying to create a bash script that will find my rds instance with a particular tag and stop the instance.
For example, if my rds instance is running with the tag Name=application_name and value=abc, it should provide me the list of the instance and then stop those.

You should be able to use something like:
ID=$(aws resourcegroupstaggingapi get-resources --resource-type-filters rds:instance --tag-filters Key=application_name,Values=abc --output text)
aws rds stop-db-instance --db-instance-identifier $ID
I didn't test it, but that should get you going!
See: get-resources — AWS CLI Command Reference

Related

How to list AWS RDS instances that are stopped

I'm trying to use the AWS CLI to list all the AWS RDS instances that I have that are in a Stopped status.
It's possible with EC2 with aws ec2 describe-instances and adding a filter --filters "Name=instance-state-name,Values=stopped".
However, with aws rds describe-db-instances, I do not find an equivalent. There is a --filter option, but only had the following options as filters: db-cluster-id, db-instance-id, dbi-resource-id, domain, engine.
So what AWS CLI command can I use to list all the RDS instances that are currently Stopped (Status=stopped)?
Use --query instead of --filters:
something like:
aws rds describe-db-instances --query '...'
https://docs.aws.amazon.com/cli/latest/userguide/cli-usage-filter.html
You can do something like this:
aws rds describe-db-instances --query 'DBInstances[].DBInstanceStatus[]'
output:
[
"available"
]

Stop multiple RDS instances using AWS CLI command

I have been trying to stop multiple instances of RDS using a single command line but it does not seem to work.
Currently I can only make it work with one instance at a time with a command like this:
aws rds stop-db-instance --db-instance-identifier test-instance1 --region ap-southeast-1 --profile dev
However I would like to stop multiple RDS and this does not seem to work:
aws rds stop-db-instance --db-instance-identifier test-instance1 test-instance2 testinstance3 --region ap-southeast-1 --profile dev
Any idea or suggestion on how I can make this work?
If it is not possible I will probably create a CRON job instead using Lambda.
Sadly you can't do this. But you can write a simple bash for loop:
ids=(test-instance1 test-instance2 test-instance3)
for id in ${ids[#]};
do
echo "Stopping: ${id}"
aws rds stop-db-instance --db-instance-identifier ${id} --region ap-southeast-1 --profile dev
done
If writing a shell script that calls aws rds stop-db-instance multiple times, once per RDS instance, is problematic for you somehow, then consider doing this via a scheduled Lambda (think of it like crontab).
See Schedule Amazon RDS stop and start using AWS Lambda, which:
presents a solution using AWS Lambda and Amazon EventBridge that allows you to schedule a Lambda function to stop and start the idle databases with specific tags to save on compute costs.

How to terminate multiple EC2 instances in AWS via CLI?

I'm looking for terminating multiple EC2 instances via AWS CLI. Yes, can able to terminate an EC2 instance by executing the below command.
Syntax:
aws ec2 terminate-instances --instance-ids <intance id> --profile <profile name>
Example:
aws ec2 terminate-instances --instance-ids <i-...> --profile xxx
But I have a big list of instances that I need to terminate so I'm searching for a solution to terminating a batch of EC2 instances by providing the list of instance ids. I tried with multiple instance ids as below but those not working.
aws ec2 terminate-instances --instance-ids ("instance-id1", "intance-id2") --profile xxx
aws ec2 terminate-instances --instance-ids ("instance-id1intance-id2") --profile xxx
aws ec2 terminate-instances --instance-ids (instance-id1,intance-id2) --profile xxx
Kindly let me know if there is any possibility to terminate a batch of instances.
I can able to achieve this by following the below command as recommended by luk2302
aws ec2 terminate-instances --instance-ids instance-id1 instance-id2 --profile xxx
Also as recommended by Alex Bailey, we can try with the shell script (.sh) or batch (.bat) which will make our job easier.
Instead of running all the instance ID's through at once I would create a loop in a shell script to do this.
Assuming you have each instance ID on a separate line in a text file you could do something like:
#!/usr/bin/env bash
while read ins_id; do
aws ec2 terminate-instances --instance-ids $ins_id --profile <profile name> || echo "error terminating ${ins_id}"
done < instance_ids.txt
That's not tested and I'm not great with shell scripting so if you try using it just try with one or two instances first and see what happens.

How to delete an EC2 instance with AWS CLI by using the EC2 tag or name?

I need to delete an Amazon EC2 instance using name or tag using the AWS CLI.
How is it possible?
To terminate an instance using the AWS Command-Line Interface (CLI):
aws ec2 terminate-instances --instance-ids i-abcd1234
See: terminate-instances — AWS CLI Command Reference
This, however, requires the **Instance ID*.
If, instead, you want to select the instance via a Tag, then you will first need to search for the instance(s).
For example, if you are searching by the instance Name (which is actually just a Tag), use:
aws ec2 describe-instances --filters Name=tag:Name,Values=Foo --query Reservations[].Instances[].InstanceId
Finally, you can combine the two queries together with:
aws ec2 terminate-instances --instance-ids `aws ec2 describe-instances --filters Name=tag:Name,Values=Foo --query Reservations[].Instances[].InstanceId --output text`

EC2 instance from AWS command line

I am very new to AWS. I've got a Windows instance running and have my aws command line configured. I've read through the AWS docs but can't seem to find exactly what I'm looking for.
How do I view my current instances from the command line?
If by view your current instances, you mean list all running instances from the command line, you can call the describe-instances command:
aws ec2 describe-instances
This will list all of your current instances.
See describe-instances
As noted in the answer by Rodrigo M, you should use describe-instances to view your EC2 instances. In general, the help command is the best way to explore the CLI. Start with aws ec2 help and try the various options. You can get more details on subcommands with aws ec2 describe-instances help as well.
The output is a bit verbose and by default in JSON. This can be a bit overwhelming and hard to read without additional processing. I'd recommend getting familiar with the --query aws CLI parameter if you intend to use the CLI interactively.
In particular, I use this for a quick overview of my EC2 instances:
aws ec2 describe-instances --query 'Reservations[].Instances[].[InstanceId, Tags[?Key==`Name`] | [0].Value, State.Name, PublicDnsName]' --output table
To check one particular attribute on an instance:
aws ec2 describe-instances --query Reservations[0].Instances[0].InstanceType --output text --instance-ids <my-instance-id>
The CLI is very powerful once you get comfortable with learning the commands and managing the output. It's also helpful for learning the porgramming APIs as well, since aws CLI commands generally map one-to-one with an API call.