Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to nuke all AWS resources in an AWS account?

I have an AWS account where multiple EC2 instances, load balancers, target groups, security groups etc are setup by multiple owners. We use terraform to set this up but sometimes due to corruption, the state becomes inconsistent. Current mechanism to recover is to manually destroy all resources in that account owned by a particular owner. Is there an easy way to nuke all resources in an AWS account belonging to a particular owner?

like image 952
Phelodas Avatar asked Mar 30 '17 01:03

Phelodas


5 Answers

There is no way to delete all resources in an account owned by a particular user but there is a way to delete all resources in an account.

You can use aws-nuke which was created somewhat out of the same use case you described.

  1. At first, you need to set an account alias for your account.
  2. You must create a config file.
  3. Then you can list down all resources that will be deleted using the following command:

    aws-nuke -c config/nuke-config.yml --profile aws-nuke-example

  4. Add --no-dry-run option to permanently delete all resources in the same command.

  5. There are also multiple filter options available such as target, resource type, exclude, etc. that you can leverage to suit your needs.

like image 51
Shwetabh Shekhar Avatar answered Oct 06 '22 08:10

Shwetabh Shekhar


Agree with the other answer that there is no easy way delete orphan resources.

But I see the original issue is that the terraform state is corrupted. You can checkout the terraform import feature which lets you generate state file from aws resources. In that way you can connect your config to resources again.

like image 36
Nithin Satheesan Avatar answered Oct 06 '22 08:10

Nithin Satheesan


Short answer: no.

Longer answer: actually, that's also no. There's no built-in capabillity for this.

The case you're describing is not within the bounds of typical AWS usage... destroying everything in an account -- usually -- should not be easy.

Of course, you could script it, fairly trivially, by wrapping calls to aws-cli to custom code to iterate through the resources and generate additional requests to destroy them... but if you do, lock that code away, since such capability is inherently dangerous.

like image 28
Michael - sqlbot Avatar answered Oct 06 '22 09:10

Michael - sqlbot


You can delete all your resources you created, you'll need to automate, see a sample here:

Creation

https://github.com/jouellnyc/AWS/tree/master/create_aws_vpc2

Deletion

https://github.com/jouellnyc/AWS/blob/master/create_aws_vpc2/delete_lb_and_vpc.sh

Other

I've had some success with cloud nuke (played around for a few min; not in depth):

https://github.com/gruntwork-io/cloud-nuke

like image 41
jouell Avatar answered Oct 06 '22 08:10

jouell


I dont think there is any state forward way to do it but to check if you have any active resources in your account, do the following:

  1. Open the Billing and Cost Management console.
  2. Choose Bills in the navigation pane.
  3. You can see the charges incurred by different services in the Bill details by service section.
  4. You can see the charges incurred in different AWS Regions in the Bill details by account section.

For each service, identify the Regions where the services have incurred charges. To terminate the identified active resources under different services, do the following:

  1. Open the AWS Management Console.
  2. For Find services, enter the service name.
  3. After opening the service console, terminate all your active resources. Be sure to check each Region where you have allocated resources.
like image 21
Sai prateek Avatar answered Oct 06 '22 09:10

Sai prateek