forked from nozaq/terraform-aws-secure-baseline
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Config retention & frequency configuration (#3)
- Loading branch information
1 parent
c2216c6
commit f109ddb
Showing
4 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#!/usr/bin/python3 | ||
|
||
import boto3 | ||
import os | ||
|
||
frequency = os.environ["CONFIG_RECORDER_FREQUENCY"] | ||
retention = int(os.getenv("CONFIG_RECORDER_RETENTION", "0")) | ||
role_arn = os.environ["TF_AWS_ROLE"] | ||
target_regions = os.environ["CONFIG_REGIONS"].split(",") | ||
|
||
# assume terraform role | ||
sts_client = boto3.client("sts") | ||
print(f"Assuming AWS role {role_arn}") | ||
assumed = sts_client.assume_role( | ||
RoleArn=role_arn, | ||
RoleSessionName="TerragruntConfigurationRecorderProvisioner", | ||
)["Credentials"] | ||
|
||
for region in target_regions: | ||
# setup AWS Config connection | ||
config = boto3.client( | ||
"config", | ||
aws_access_key_id=assumed["AccessKeyId"], | ||
aws_secret_access_key=assumed["SecretAccessKey"], | ||
aws_session_token=assumed["SessionToken"], | ||
region_name=region, | ||
) | ||
|
||
recorder = config.describe_configuration_recorders()["ConfigurationRecorders"][0] | ||
recordingMode = recorder.get("recordingMode", {}) | ||
if recordingMode.get("recordingFrequency") != frequency: | ||
print(f"Setting {region} Config recorder frequency to {frequency}") | ||
recordingMode["recordingFrequency"] = frequency | ||
recorder["recordingMode"] = recordingMode | ||
config.put_configuration_recorder(ConfigurationRecorder=recorder) | ||
|
||
if retention: | ||
current_retention = config.describe_retention_configurations()[ | ||
"RetentionConfigurations" | ||
] | ||
if current_retention != [ | ||
{"Name": recorder["name"], "RetentionPeriodInDays": retention} | ||
]: | ||
print(f"Setting {region} Config retention to {retention} days") | ||
config.put_retention_configuration(RetentionPeriodInDays=retention) |
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