|
5 | 5 | "fmt" |
6 | 6 | "log" |
7 | 7 | "os" |
| 8 | + "sync" |
8 | 9 |
|
9 | 10 | awsprovider "terraform-provider-iterative/iterative/aws" |
10 | 11 |
|
@@ -51,50 +52,62 @@ func main() { |
51 | 52 | } |
52 | 53 |
|
53 | 54 | ami := imagesRes.Images[0] |
54 | | - amiID := *ami.ImageId |
55 | | - amiDesc := *ami.Description |
56 | 55 |
|
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 | +} |
59 | 71 |
|
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 | + ) |
63 | 76 |
|
64 | | - svc := ec2.New(sess) |
| 77 | + svc := ec2.New(sess) |
65 | 78 |
|
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 | + } |
75 | 88 |
|
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")}, |
83 | 95 | }, |
84 | | - }) |
| 96 | + }, |
| 97 | + }) |
85 | 98 |
|
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"), |
93 | 105 | }, |
94 | 106 | }, |
95 | | - }) |
96 | | - if modifyErr != nil { |
97 | | - fmt.Println(modifyErr) |
98 | | - } |
| 107 | + }, |
| 108 | + }) |
| 109 | + if modifyErr != nil { |
| 110 | + return err |
99 | 111 | } |
| 112 | + return nil |
100 | 113 | } |
0 commit comments