Created
September 25, 2019 09:44
-
-
Save kasunbg/115f331a0ac0c773eb90cfb2a1f13295 to your computer and use it in GitHub Desktop.
This class finds CREATE_COMPLETE stacks that are more than five days old, and output a aws cloudformation delete command to stdout. This simply outputs the result, no actual stacks will be deleted.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.sample; | |
import com.google.gson.JsonElement; | |
import com.google.gson.JsonParser; | |
import java.io.FileNotFoundException; | |
import java.io.FileReader; | |
import java.io.Reader; | |
import java.time.Instant; | |
import java.time.format.DateTimeFormatter; | |
import java.time.temporal.TemporalAccessor; | |
import java.util.ArrayList; | |
import java.util.Date; | |
import java.util.List; | |
import java.util.concurrent.TimeUnit; | |
/** | |
* This class finds CREATE_COMPLETE stacks that are more than five days old, | |
* and output a aws cloudformation delete command to stdout. | |
* This simply outputs the result, no actual stacks will be deleted. | |
* <p> | |
* First run this command: | |
* export AWS_DEFAULT_REGION=us-east-1 | |
* aws cloudformation describe-stacks --query 'Stacks[?StackStatus==`CREATE_COMPLETE`]' --output json > /tmp/cfn-output-json | |
* This shows all create_complete stacks in a given region | |
* <p> | |
* Then, run this class. | |
* Finally, run the output in bash. | |
* <p> | |
* Requirements: | |
* gson library should be in classpath | |
*/ | |
public class CloudformationStackDeleter { | |
private static final int OLDER_THAN_DAYS = 4; | |
private static final String CFN_DESCRIBE_STACK_JSON_OUTPUT = "/tmp/cfn-output-json"; | |
public static void main(String[] args) throws FileNotFoundException { | |
Reader r = new FileReader(CFN_DESCRIBE_STACK_JSON_OUTPUT); | |
JsonParser parser = new JsonParser(); | |
final JsonElement parsed = parser.parse(r); | |
// final JsonElement parsed = parser.parse("{id: 123}"); | |
List<String> stacksToDel = new ArrayList<>(); | |
for (JsonElement element : parsed.getAsJsonArray()) { | |
final JsonElement creationTime = element.getAsJsonObject().get("CreationTime"); | |
String creationTimeAsString = creationTime.getAsString(); | |
DateTimeFormatter timeFormatter = DateTimeFormatter.ISO_INSTANT; | |
TemporalAccessor accessor = timeFormatter.parse(creationTimeAsString); | |
final Instant ctI = Instant.from(accessor); | |
Date date = Date.from(ctI); | |
Date now = new Date(); | |
final long diff = Math.abs(now.getTime() - date.getTime()); | |
final long days = TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS); | |
if (days > OLDER_THAN_DAYS) { | |
final JsonElement stackName = element.getAsJsonObject().get("StackName"); | |
stacksToDel.add(stackName.getAsString()); | |
} | |
} | |
System.out.println("Stacks to delete: " + stacksToDel.size()); | |
for (String name : stacksToDel) { | |
System.out.println("aws cloudformation delete-stack --stack-name " + name); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment