Skip to content

Commit 0574091

Browse files
authored
skip: Merge pull request #34 from epam/feature/ecc-aws-571-stopped_rds_instances_removed
new: added policy ecc-aws-571-stopped_rds_instances_removed
2 parents 35fc023 + 5254033 commit 0574091

File tree

14 files changed

+528
-0
lines changed

14 files changed

+528
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright (c) 2023 EPAM Systems, Inc.
2+
#
3+
# This Source Code Form is subject to the terms of the Mozilla Public
4+
# License, v. 2.0. If a copy of the MPL was not distributed with this
5+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
7+
8+
policies:
9+
- name: ecc-aws-571-stopped_rds_instances_removed
10+
comment: '010002062000'
11+
description: |
12+
RDS DB instances
13+
resource: aws.rds
14+
filters:
15+
- type: value
16+
key: DBInstanceStatus
17+
value: stopped
18+
- type: value
19+
key: AutomaticRestartTime
20+
value: 4
21+
value_type: expiration
22+
op: lte
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
terraform {
2+
required_providers {
3+
aws = {
4+
source = "hashicorp/aws"
5+
version = "~> 4"
6+
}
7+
}
8+
}
9+
10+
provider "aws" {
11+
profile = var.profile
12+
region = var.default-region
13+
14+
default_tags {
15+
tags = {
16+
CustodianRule = "ecc-aws-571-stopped_rds_instances_removed"
17+
ComplianceStatus = "Green"
18+
}
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
resource "random_password" "this" {
2+
length = 12
3+
special = true
4+
numeric = true
5+
override_special = "!#$%*()-_=+[]{}:?"
6+
}
7+
8+
resource "aws_db_instance" "this" {
9+
engine = "mysql"
10+
engine_version = "5.7"
11+
instance_class = "db.t2.micro"
12+
allocated_storage = 10
13+
storage_type = "gp2"
14+
db_name = "database517green"
15+
username = "root"
16+
password = random_password.this.result
17+
multi_az = false
18+
skip_final_snapshot = true
19+
identifier = "db-instance-571-green"
20+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
profile = "c7n"
2+
default-region = "us-east-1"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
variable "default-region" {
2+
type = string
3+
description = "Default region for resources will be created"
4+
}
5+
6+
variable "profile" {
7+
type = string
8+
description = "Profile name configured before running apply"
9+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"Version": "2012-10-17",
3+
"Statement": [
4+
{
5+
"Effect": "Allow",
6+
"Action": [
7+
"rds:DescribeDBInstances"
8+
],
9+
"Resource": "*"
10+
}
11+
]
12+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
terraform {
2+
required_providers {
3+
aws = {
4+
source = "hashicorp/aws"
5+
version = "~> 4"
6+
}
7+
}
8+
}
9+
10+
provider "aws" {
11+
profile = var.profile
12+
region = var.default-region
13+
14+
default_tags {
15+
tags = {
16+
CustodianRule = "ecc-aws-571-stopped_rds_instances_removed"
17+
ComplianceStatus = "Red"
18+
}
19+
}
20+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
resource "random_password" "this" {
2+
length = 12
3+
special = true
4+
numeric = true
5+
override_special = "!#$%*()-_=+[]{}:?"
6+
}
7+
8+
resource "aws_db_instance" "this" {
9+
engine = "mysql"
10+
engine_version = "5.7"
11+
instance_class = "db.t2.micro"
12+
allocated_storage = 10
13+
storage_type = "gp2"
14+
db_name = "database517red"
15+
username = "root"
16+
password = random_password.this.result
17+
multi_az = false
18+
skip_final_snapshot = true
19+
identifier = "db-instance-571-red"
20+
}
21+
22+
resource "null_resource" "cleanup_rds" {
23+
depends_on = [
24+
aws_db_instance.this
25+
]
26+
triggers = {
27+
profile = var.profile
28+
region = var.default-region
29+
identifier = aws_db_instance.this.identifier
30+
}
31+
provisioner "local-exec" {
32+
interpreter = ["/bin/bash", "-c"]
33+
command = <<EOF
34+
aws sts get-caller-identity
35+
export AWS_PROFILE=${self.triggers.profile}
36+
export AWS_REGION=${self.triggers.region}
37+
aws sts get-caller-identity
38+
39+
while true; do
40+
status="$(aws rds describe-db-instances --db-instance-identifier ${self.triggers.identifier} --query DBInstances[0].DBInstanceStatus --output text)"
41+
if [ "$status" = "available" ]; then
42+
aws rds stop-db-instance --db-instance-identifier ${self.triggers.identifier}
43+
break
44+
else
45+
echo "Waiting for database: $rds to be available"
46+
sleep 60
47+
fi
48+
done
49+
while true; do
50+
status="$(aws rds describe-db-instances --db-instance-identifier ${self.triggers.identifier} --query DBInstances[0].DBInstanceStatus --output text)"
51+
if [ "$status" = "stopped" ]; then
52+
break
53+
else
54+
echo "Waiting for database: $rds to be stopped"
55+
sleep 60
56+
fi
57+
done
58+
59+
echo "RDS instance stopped."
60+
EOF
61+
}
62+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
profile = "c7n"
2+
default-region = "us-east-1"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
variable "default-region" {
2+
type = string
3+
description = "Default region for resources will be created"
4+
}
5+
6+
variable "profile" {
7+
type = string
8+
description = "Profile name configured before running apply"
9+
}

0 commit comments

Comments
 (0)