Last active
March 5, 2020 03:32
-
-
Save guitarrapc/7a84ce76b054a8f9bb8f31f65867f293 to your computer and use it in GitHub Desktop.
generate terraform guardduty for all region via C# template. (member account is not scoped)
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
void Main() | |
{ | |
var awsProviderVersion = "2.51.0"; | |
GenProviders().Dump(); | |
GenModules().Dump(); | |
static string[] GenModules() | |
{ | |
return Amazon.RegionEndpoint.EnumerableAllRegions | |
.Where(x => !IsSpetialRegion(x.SystemName)) | |
.Where(x => !IsNotSupportedRegion(x.SystemName)) | |
.Select(x => GetRegionName(x.DisplayName)) | |
.Select(x => $@"module ""{x}"" {{ | |
source = ""./modules/guardduty"" | |
providers = {{ aws = aws.{x} }} | |
}}") | |
.ToArray(); | |
} | |
string[] GenProviders() | |
{ | |
return Amazon.RegionEndpoint.EnumerableAllRegions | |
.Where(x => !IsSpetialRegion(x.SystemName)) | |
.Where(x => !IsNotSupportedRegion(x.SystemName)) | |
.Select(x => (alias: GetRegionName(x.DisplayName), region: x.SystemName)) | |
.Select(x => $@"provider ""aws"" {{ | |
version = ""{awsProviderVersion}"" | |
region = ""{x.region}"" | |
alias = ""{x.alias}"" | |
access_key = var.ACCESS_KEY | |
secret_key = var.SECRET_KEY | |
}}") | |
.ToArray(); | |
} | |
static bool IsSpetialRegion(string region) | |
=> region.Contains("gov-") || region.Contains("cn-") || region.Contains("iso-") || region.Contains("isob-"); | |
static bool IsNotSupportedRegion(string region) | |
// HongKong | |
// Bahrain | |
=> region == "ap-east-1" || region == "me-south-1"; | |
static string GetRegionName(string fullName) | |
{ | |
var begin = fullName.IndexOf('('); | |
var end = fullName.IndexOf(')'); | |
if (begin == -1 || end == -1) | |
return fullName.Replace(" ", ""); | |
return fullName.Substring(begin + 1, end - begin - 1).Replace(" ", "").Replace(".", "_"); | |
} | |
} |
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
resource "aws_guardduty_detector" "main" { | |
enable = true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
output