diff --git a/provider/aws/cmd/regions/main.go b/provider/aws/cmd/regions/main.go deleted file mode 100644 index 36dc6e89a3..0000000000 --- a/provider/aws/cmd/regions/main.go +++ /dev/null @@ -1,336 +0,0 @@ -package main - -import ( - "encoding/json" - "fmt" - "os" - "os/exec" - "sort" - "strings" - - "github.com/PuerkitoBio/goquery" - "github.com/headzoo/surf" - "github.com/pkg/errors" -) - -type Region struct { - Ami string - ArmAmi string - AvailabilityZones []string - EFS bool - ELBAccountId string - Fargate bool -} - -type Regions map[string]Region - -func main() { - if err := run(); err != nil { - fmt.Fprintf(os.Stderr, "ERROR: %+v\n", err) - } -} - -func run() error { - regions, err := fetchRegions() - if err != nil { - return errors.WithStack(err) - } - - if err := fetchAmis(regions); err != nil { - return errors.WithStack(err) - } - - if err := fetchArmAmis(regions); err != nil { - return errors.WithStack(err) - } - - if err := fetchAvailabilityZones(regions); err != nil { - return errors.WithStack(err) - } - - if err := fetchEFS(regions); err != nil { - return errors.WithStack(err) - } - - if err := fetchFargate(regions); err != nil { - return errors.WithStack(err) - } - - if err := fetchELBAccountIds(regions); err != nil { - return errors.WithStack(err) - } - - names := []string{} - - for name := range regions { - names = append(names, name) - } - - sort.Strings(names) - - rns := make([]string, len(names)) - amis := make([]string, len(names)) - aamis := make([]string, len(names)) - efss := make([]string, len(names)) - tazs := make([]string, len(names)) - elbs := make([]string, len(names)) - fargates := make([]string, len(names)) - - for i, name := range names { - region := regions[name] - - rns[i] = fmt.Sprintf("%q:", name) - amis[i] = fmt.Sprintf(`"Ami": %q,`, region.Ami) - aamis[i] = fmt.Sprintf(`"ArmAmi": %q,`, region.ArmAmi) - efss[i] = fmt.Sprintf(`"EFS": %q,`, yn(region.EFS)) - tazs[i] = fmt.Sprintf(`"ThirdAvailabilityZone": %q,`, yn(len(region.AvailabilityZones) > 2)) - elbs[i] = fmt.Sprintf(`"ELBAccountId": %q,`, region.ELBAccountId) - fargates[i] = fmt.Sprintf(`"Fargate": %q`, yn(region.Fargate)) - } - - rnMax := max(rns, 0) - amiMax := max(amis, 0) - aamiMax := max(aamis, 0) - efsMax := max(efss, 0) - tazMax := max(tazs, 0) - elbMax := max(elbs, 0) - fargateMax := max(fargates, 0) - - for i := range names { - if regions[names[i]].Ami == "" { - continue - } - - f := fmt.Sprintf(` %%-%ds { %%-%ds %%-%ds %%-%ds %%-%ds %%-%ds %%-%ds },`, rnMax, amiMax, aamiMax, efsMax, tazMax, elbMax, fargateMax) - fmt.Printf(f, rns[i], amis[i], aamis[i], efss[i], tazs[i], elbs[i], fargates[i]) - fmt.Println() - } - - return nil -} - -func fetchRegions() (Regions, error) { - rs := Regions{} - - data, err := exec.Command("aws", "ec2", "describe-regions", "--query", "Regions[].RegionName").CombinedOutput() - if err != nil { - fmt.Printf("string(data): %+v\n", string(data)) - return nil, errors.WithStack(err) - } - - var regions []string - - if err := json.Unmarshal(data, ®ions); err != nil { - return nil, errors.WithStack(err) - } - - for _, region := range regions { - rs[region] = Region{} - } - - return rs, nil -} - -func fetchAmis(regions Regions) error { - var ami string - - for name, region := range regions { - data, err := exec.Command("aws", "ssm", "get-parameter", "--name", "/aws/service/ecs/optimized-ami/amazon-linux-2/recommended/image_id", "--query", "Parameter.Value", "--region", name).CombinedOutput() - if err != nil { - fmt.Printf("error fetching Amd AMI. region=%s error=%s\n", name, err) - delete(regions, name) - continue - } - - if err := json.Unmarshal(data, &ami); err != nil { - return errors.WithStack(err) - } - - region.Ami = ami - - regions[name] = region - } - - return nil -} - -func fetchArmAmis(regions Regions) error { - var ami string - - for name, region := range regions { - data, err := exec.Command("aws", "ssm", "get-parameter", "--name", "/aws/service/ecs/optimized-ami/amazon-linux-2/arm64/recommended/image_id", "--query", "Parameter.Value", "--region", name).CombinedOutput() - if err != nil { - fmt.Printf("error fetching Arm AMI. region=%s error=%s\n", name, err) - continue - } - - if err := json.Unmarshal(data, &ami); err != nil { - return errors.WithStack(err) - } - - region.ArmAmi = ami - - regions[name] = region - } - - return nil -} - -func fetchAvailabilityZones(regions Regions) error { - var azs struct { - AvailabilityZones []struct { - ZoneName string - } - } - - for name, region := range regions { - data, err := exec.Command("bash", "-c", fmt.Sprintf("aws ec2 describe-availability-zones --region %s", name)).CombinedOutput() - if err != nil { - return errors.WithStack(fmt.Errorf(string(data))) - } - - if err := json.Unmarshal(data, &azs); err != nil { - return errors.WithStack(err) - } - - for _, az := range azs.AvailabilityZones { - region.AvailabilityZones = append(region.AvailabilityZones, az.ZoneName) - } - - regions[name] = region - } - - return nil -} - -func fetchEFS(regions Regions) error { - b := surf.NewBrowser() - - if err := b.Open("https://docs.aws.amazon.com/general/latest/gr/elasticfilesystem.html"); err != nil { - return errors.WithStack(err) - } - - rows := b.Find("h2#elasticfilesystem-region+.table-container table tr") - - if rows.Length() < 1 { - return errors.WithStack(fmt.Errorf("no efs entries found")) - } - - rows.Each(func(i int, s *goquery.Selection) { - if i == 0 { - return - } - - name := strings.TrimSpace(s.Find("td:nth-child(2)").Text()) - - region := regions[name] - region.EFS = true - regions[name] = region - }) - - return nil -} - -func fetchELBAccountIds(regions Regions) error { - b := surf.NewBrowser() - - if err := b.Open("https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-access-logs.html"); err != nil { - return errors.WithStack(err) - } - - rows := b.Find("h2#access-logging-bucket-permissions~div.procedure table tr") - - if rows.Length() < 1 { - return errors.WithStack(fmt.Errorf("no elb account ids found")) - } - - rows.Each(func(i int, s *goquery.Selection) { - if i == 0 { - return - } - - name := strings.TrimSuffix(strings.TrimSpace(s.Find("td:nth-child(1)").Text()), "*") - id := strings.TrimSpace(s.Find("td:nth-child(3)").Text()) - - region := regions[name] - region.ELBAccountId = id - regions[name] = region - }) - - return nil -} - -func fetchFargate(regions Regions) error { - b := surf.NewBrowser() - - if err := b.Open("https://docs.aws.amazon.com/AmazonECS/latest/developerguide/AWS_Fargate-Regions.html"); err != nil { - return errors.WithStack(err) - } - - rows := b.Find("table:nth-of-type(1) tr") - - if rows.Length() < 1 { - return errors.WithStack(fmt.Errorf("no fargate regions found")) - } - - rows.Each(func(i int, s *goquery.Selection) { - if i == 0 { - return - } - - name := strings.TrimSpace(s.Find("td:nth-child(2)").Text()) - - if !strings.HasSuffix(name, "*") { - if region, ok := regions[name]; ok { - region.Fargate = true - regions[name] = region - } - } - }) - - return nil -} - -func printRegions(regions Regions) { - rns := []string{} - amis := []string{} - elbs := []string{} - - for name := range regions { - rns = append(rns, name) - amis = append(amis, regions[name].Ami) - elbs = append(elbs, regions[name].ELBAccountId) - } - - sort.Strings(rns) - - rnmax := max(rns, 6) - amimax := max(amis, 3) - elmax := max(elbs, 12) - - fmt.Printf(fmt.Sprintf("\n%%-%ds %%-%ds %%s %%-5s %%-%ds %%s\n", rnmax, amimax, elmax), "region", "ami", "azs", "efs", "elbaccountid", "fargate") - - for _, name := range rns { - r := regions[name] - fmt.Printf(fmt.Sprintf("%%-%ds %%-%ds %%3d %%-5t %%-%ds %%t\n", rnmax, amimax, elmax), name, r.Ami, len(r.AvailabilityZones), r.EFS, r.ELBAccountId, r.Fargate) - } -} - -func max(ss []string, min int) int { - m := min - - for _, s := range ss { - if len(s) > m { - m = len(s) - } - } - - return m -} - -func yn(v bool) string { - if v { - return "Yes" - } - return "No" -} diff --git a/provider/aws/formation/rack.json b/provider/aws/formation/rack.json index 3653bf878d..b0f7caa118 100644 --- a/provider/aws/formation/rack.json +++ b/provider/aws/formation/rack.json @@ -152,29 +152,29 @@ }, "Mappings": { "RegionConfig": { - "af-south-1": { "Ami": "ami-0974da6f95845d1fb", "ArmAmi": "", "EFS": "Yes", "ThirdAvailabilityZone": "Yes", "ELBAccountId": "098369216593", "Fargate": "Yes" }, - "ap-east-1": { "Ami": "ami-0e1e9a40bf74ef303", "ArmAmi": "ami-062cb839b04eaf653", "EFS": "Yes", "ThirdAvailabilityZone": "Yes", "ELBAccountId": "754344448648", "Fargate": "Yes" }, - "ap-northeast-1": { "Ami": "ami-07beb9196f84744a6", "ArmAmi": "ami-03ebda6a9a9466fa3", "EFS": "Yes", "ThirdAvailabilityZone": "Yes", "ELBAccountId": "582318560864", "Fargate": "No" }, - "ap-northeast-2": { "Ami": "ami-0ef7a2936ae1e00cc", "ArmAmi": "ami-0fb2fc55bd7ad6674", "EFS": "Yes", "ThirdAvailabilityZone": "Yes", "ELBAccountId": "600734575887", "Fargate": "Yes" }, - "ap-northeast-3": { "Ami": "ami-076672d8d1177c3c7", "ArmAmi": "", "EFS": "Yes", "ThirdAvailabilityZone": "Yes", "ELBAccountId": "383597477331", "Fargate": "Yes" }, - "ap-south-1": { "Ami": "ami-05df77ec905ed3dcf", "ArmAmi": "ami-06959b9dc7ed71bd9", "EFS": "Yes", "ThirdAvailabilityZone": "Yes", "ELBAccountId": "718504428378", "Fargate": "Yes" }, - "ap-southeast-1": { "Ami": "ami-095997bc097212f6f", "ArmAmi": "ami-05b646a0daeeafeb6", "EFS": "Yes", "ThirdAvailabilityZone": "Yes", "ELBAccountId": "114774131450", "Fargate": "Yes" }, - "ap-southeast-2": { "Ami": "ami-0e14619ff8da67b1f", "ArmAmi": "ami-07e8ddb585d34cc0c", "EFS": "Yes", "ThirdAvailabilityZone": "Yes", "ELBAccountId": "783225319266", "Fargate": "Yes" }, - "ca-central-1": { "Ami": "ami-0c133a38764018681", "ArmAmi": "ami-0add02ed36ecb6526", "EFS": "Yes", "ThirdAvailabilityZone": "Yes", "ELBAccountId": "985666609251", "Fargate": "No" }, - "eu-central-1": { "Ami": "ami-0262c9b663f67241e", "ArmAmi": "ami-00d2d80024cf3906e", "EFS": "Yes", "ThirdAvailabilityZone": "Yes", "ELBAccountId": "054676820928", "Fargate": "Yes" }, - "eu-north-1": { "Ami": "ami-08056d04e24f84e34", "ArmAmi": "ami-0af1ddfda62ed797b", "EFS": "Yes", "ThirdAvailabilityZone": "Yes", "ELBAccountId": "897822967062", "Fargate": "Yes" }, - "eu-south-1": { "Ami": "ami-0f03ef04f39d8e461", "ArmAmi": "ami-02754c8637700380e", "EFS": "Yes", "ThirdAvailabilityZone": "Yes", "ELBAccountId": "635631232127", "Fargate": "Yes" }, - "eu-west-1": { "Ami": "ami-0c21ebd9e0dbd6249", "ArmAmi": "ami-080bc27e1b95be2f7", "EFS": "Yes", "ThirdAvailabilityZone": "Yes", "ELBAccountId": "156460612806", "Fargate": "Yes" }, - "eu-west-2": { "Ami": "ami-0d87e866e25e832fe", "ArmAmi": "ami-03278dc7e12d6ae4e", "EFS": "Yes", "ThirdAvailabilityZone": "Yes", "ELBAccountId": "652711504416", "Fargate": "Yes" }, - "eu-west-3": { "Ami": "ami-0f41f5b130d9f1ef4", "ArmAmi": "ami-062bfd35b053eff18", "EFS": "Yes", "ThirdAvailabilityZone": "Yes", "ELBAccountId": "009996457667", "Fargate": "Yes" }, - "me-south-1": { "Ami": "ami-0430a09cffe66d975", "ArmAmi": "ami-038352082e2259652", "EFS": "Yes", "ThirdAvailabilityZone": "Yes", "ELBAccountId": "076674570225", "Fargate": "Yes" }, - "sa-east-1": { "Ami": "ami-0b098a101dfecaeaf", "ArmAmi": "ami-0ac68f6babfa7ee58", "EFS": "Yes", "ThirdAvailabilityZone": "Yes", "ELBAccountId": "507241528517", "Fargate": "Yes" }, - "us-east-1": { "Ami": "ami-0f260fe26c2826a3d", "ArmAmi": "ami-0bed0a25ad1eafbac", "EFS": "Yes", "ThirdAvailabilityZone": "Yes", "ELBAccountId": "127311923021", "Fargate": "Yes" }, - "us-east-2": { "Ami": "ami-09ca0a8af1c65c842", "ArmAmi": "ami-05e1c2a7e346ae4fc", "EFS": "Yes", "ThirdAvailabilityZone": "Yes", "ELBAccountId": "033677994240", "Fargate": "Yes" }, - "us-west-1": { "Ami": "ami-0c1f28679e0c1a694", "ArmAmi": "ami-06e958ab6268ad642", "EFS": "Yes", "ThirdAvailabilityZone": "No", "ELBAccountId": "027434742980", "Fargate": "No" }, - "us-west-2": { "Ami": "ami-0e0e34cdb5fd714fc", "ArmAmi": "ami-04581de0304b6c3c1", "EFS": "Yes", "ThirdAvailabilityZone": "Yes", "ELBAccountId": "797873946194", "Fargate": "Yes" }, - "us-gov-east-1": { "Ami": "ami-09c78c4bf3ddfc2c1", "ArmAmi": "ami-00947675fd7f2f13f", "EFS": "Yes", "ThirdAvailabilityZone": "Yes", "ELBAccountId": "190560391635", "Fargate": "Yes" }, - "us-gov-west-1": { "Ami": "ami-0430cdf9d765c8341", "ArmAmi": "ami-028e37934f000fdb7", "EFS": "Yes", "ThirdAvailabilityZone": "Yes", "ELBAccountId": "048591011584", "Fargate": "Yes" } + "af-south-1": { "EFS": "Yes", "ThirdAvailabilityZone": "Yes", "ELBAccountId": "098369216593", "Fargate": "Yes" }, + "ap-east-1": { "EFS": "Yes", "ThirdAvailabilityZone": "Yes", "ELBAccountId": "754344448648", "Fargate": "Yes" }, + "ap-northeast-1": { "EFS": "Yes", "ThirdAvailabilityZone": "Yes", "ELBAccountId": "582318560864", "Fargate": "No" }, + "ap-northeast-2": { "EFS": "Yes", "ThirdAvailabilityZone": "Yes", "ELBAccountId": "600734575887", "Fargate": "Yes" }, + "ap-northeast-3": { "EFS": "Yes", "ThirdAvailabilityZone": "Yes", "ELBAccountId": "383597477331", "Fargate": "Yes" }, + "ap-south-1": { "EFS": "Yes", "ThirdAvailabilityZone": "Yes", "ELBAccountId": "718504428378", "Fargate": "Yes" }, + "ap-southeast-1": { "EFS": "Yes", "ThirdAvailabilityZone": "Yes", "ELBAccountId": "114774131450", "Fargate": "Yes" }, + "ap-southeast-2": { "EFS": "Yes", "ThirdAvailabilityZone": "Yes", "ELBAccountId": "783225319266", "Fargate": "Yes" }, + "ca-central-1": { "EFS": "Yes", "ThirdAvailabilityZone": "Yes", "ELBAccountId": "985666609251", "Fargate": "No" }, + "eu-central-1": { "EFS": "Yes", "ThirdAvailabilityZone": "Yes", "ELBAccountId": "054676820928", "Fargate": "Yes" }, + "eu-north-1": { "EFS": "Yes", "ThirdAvailabilityZone": "Yes", "ELBAccountId": "897822967062", "Fargate": "Yes" }, + "eu-south-1": { "EFS": "Yes", "ThirdAvailabilityZone": "Yes", "ELBAccountId": "635631232127", "Fargate": "Yes" }, + "eu-west-1": { "EFS": "Yes", "ThirdAvailabilityZone": "Yes", "ELBAccountId": "156460612806", "Fargate": "Yes" }, + "eu-west-2": { "EFS": "Yes", "ThirdAvailabilityZone": "Yes", "ELBAccountId": "652711504416", "Fargate": "Yes" }, + "eu-west-3": { "EFS": "Yes", "ThirdAvailabilityZone": "Yes", "ELBAccountId": "009996457667", "Fargate": "Yes" }, + "me-south-1": { "EFS": "Yes", "ThirdAvailabilityZone": "Yes", "ELBAccountId": "076674570225", "Fargate": "Yes" }, + "sa-east-1": { "EFS": "Yes", "ThirdAvailabilityZone": "Yes", "ELBAccountId": "507241528517", "Fargate": "Yes" }, + "us-east-1": { "EFS": "Yes", "ThirdAvailabilityZone": "Yes", "ELBAccountId": "127311923021", "Fargate": "Yes" }, + "us-east-2": { "EFS": "Yes", "ThirdAvailabilityZone": "Yes", "ELBAccountId": "033677994240", "Fargate": "Yes" }, + "us-west-1": { "EFS": "Yes", "ThirdAvailabilityZone": "No", "ELBAccountId": "027434742980", "Fargate": "No" }, + "us-west-2": { "EFS": "Yes", "ThirdAvailabilityZone": "Yes", "ELBAccountId": "797873946194", "Fargate": "Yes" }, + "us-gov-east-1": { "EFS": "Yes", "ThirdAvailabilityZone": "Yes", "ELBAccountId": "190560391635", "Fargate": "Yes" }, + "us-gov-west-1": { "EFS": "Yes", "ThirdAvailabilityZone": "Yes", "ELBAccountId": "048591011584", "Fargate": "Yes" } } }, "Outputs": { @@ -581,6 +581,14 @@ "Description": "Anonymous identifier", "Default": "" }, + "DefaultAmi": { + "Default": "/aws/service/ecs/optimized-ami/amazon-linux-2/recommended/image_id", + "Type": "AWS::SSM::Parameter::Value" + }, + "DefaultAmiArm": { + "Default": "/aws/service/ecs/optimized-ami/amazon-linux-2/arm64/recommended/image_id", + "Type": "AWS::SSM::Parameter::Value" + }, "Development": { "Type": "String", "Description": "Development mode", @@ -1474,18 +1482,10 @@ "Fn::If": [ "InstanceARM", { - "Fn::FindInMap": [ - "RegionConfig", - { "Ref": "AWS::Region" }, - "ArmAmi" - ] + "Ref": "DefaultAmiArm" }, { - "Fn::FindInMap": [ - "RegionConfig", - { "Ref": "AWS::Region" }, - "Ami" - ] + "Ref": "DefaultAmi" } ] }, @@ -1672,18 +1672,10 @@ "Fn::If": [ "InstanceARM", { - "Fn::FindInMap": [ - "RegionConfig", - { "Ref": "AWS::Region" }, - "ArmAmi" - ] + "Ref": "DefaultAmiArm" }, { - "Fn::FindInMap": [ - "RegionConfig", - { "Ref": "AWS::Region" }, - "Ami" - ] + "Ref": "DefaultAmi" } ] }, @@ -2172,18 +2164,10 @@ "Fn::If": [ "InstanceARM", { - "Fn::FindInMap": [ - "RegionConfig", - { "Ref": "AWS::Region" }, - "ArmAmi" - ] + "Ref": "DefaultAmiArm" }, { - "Fn::FindInMap": [ - "RegionConfig", - { "Ref": "AWS::Region" }, - "Ami" - ] + "Ref": "DefaultAmi" } ] },