Skip to content

Commit 08440a2

Browse files
authored
Parallelize clone-amis.go (#97)
Each clone-amis.go run took eons to process, but now it takes a single eon no matter how many regions we want to support.
1 parent b8197f4 commit 08440a2

File tree

1 file changed

+49
-36
lines changed

1 file changed

+49
-36
lines changed

cml/clone-amis.go

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"log"
77
"os"
8+
"sync"
89

910
awsprovider "terraform-provider-iterative/iterative/aws"
1011

@@ -51,50 +52,62 @@ func main() {
5152
}
5253

5354
ami := imagesRes.Images[0]
54-
amiID := *ami.ImageId
55-
amiDesc := *ami.Description
5655

57-
for _, value := range regions {
58-
fmt.Println("Cloning", value)
56+
var wg sync.WaitGroup
57+
for _, destination := range regions {
58+
wg.Add(1)
59+
go func(destinationRegion string) {
60+
fmt.Printf("Cloning image to region %s...\n", destinationRegion)
61+
if err := cloneImage(region, destinationRegion, *ami.ImageId, *ami.Name, *ami.Description); err != nil {
62+
fmt.Printf("Error cloning image to region %s\n", destinationRegion)
63+
fmt.Println(err.Error())
64+
os.Exit(1)
65+
}
66+
fmt.Printf("Image successfully cloned to region %s\n", destinationRegion)
67+
}(destination)
68+
}
69+
wg.Wait()
70+
}
5971

60-
sess, _ := session.NewSession(&aws.Config{
61-
Region: aws.String(value)},
62-
)
72+
func cloneImage(sourceRegion string, destinationRegion string, imageIdentifier string, imageName string, imageDescription string) error {
73+
sess, _ := session.NewSession(&aws.Config{
74+
Region: aws.String(destinationRegion)},
75+
)
6376

64-
svc := ec2.New(sess)
77+
svc := ec2.New(sess)
6578

66-
copyResult, err := svc.CopyImage(&ec2.CopyImageInput{
67-
SourceImageId: aws.String(amiID),
68-
SourceRegion: aws.String(region),
69-
Name: aws.String(amiName),
70-
Description: aws.String(amiDesc),
71-
})
72-
if err != nil {
73-
fmt.Println(err)
74-
}
79+
copyResult, err := svc.CopyImage(&ec2.CopyImageInput{
80+
SourceImageId: aws.String(imageIdentifier),
81+
SourceRegion: aws.String(sourceRegion),
82+
Name: aws.String(imageName),
83+
Description: aws.String(imageDescription),
84+
})
85+
if err != nil {
86+
return err
87+
}
7588

76-
svc.WaitUntilImageExists(&ec2.DescribeImagesInput{
77-
ImageIds: []*string{aws.String(*copyResult.ImageId)},
78-
Filters: []*ec2.Filter{
79-
{
80-
Name: aws.String("state"),
81-
Values: []*string{aws.String("available")},
82-
},
89+
svc.WaitUntilImageExists(&ec2.DescribeImagesInput{
90+
ImageIds: []*string{aws.String(*copyResult.ImageId)},
91+
Filters: []*ec2.Filter{
92+
{
93+
Name: aws.String("state"),
94+
Values: []*string{aws.String("available")},
8395
},
84-
})
96+
},
97+
})
8598

86-
_, modifyErr := svc.ModifyImageAttribute(&ec2.ModifyImageAttributeInput{
87-
ImageId: aws.String(*copyResult.ImageId),
88-
LaunchPermission: &ec2.LaunchPermissionModifications{
89-
Add: []*ec2.LaunchPermission{
90-
{
91-
Group: aws.String("all"),
92-
},
99+
_, modifyErr := svc.ModifyImageAttribute(&ec2.ModifyImageAttributeInput{
100+
ImageId: aws.String(*copyResult.ImageId),
101+
LaunchPermission: &ec2.LaunchPermissionModifications{
102+
Add: []*ec2.LaunchPermission{
103+
{
104+
Group: aws.String("all"),
93105
},
94106
},
95-
})
96-
if modifyErr != nil {
97-
fmt.Println(modifyErr)
98-
}
107+
},
108+
})
109+
if modifyErr != nil {
110+
return err
99111
}
112+
return nil
100113
}

0 commit comments

Comments
 (0)