|
| 1 | +//usr/bin/env go run $0 "$@"; exit |
| 2 | +package main |
| 3 | + |
| 4 | +import ( |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "os" |
| 8 | + |
| 9 | + "github.com/aws/aws-sdk-go/aws" |
| 10 | + "github.com/aws/aws-sdk-go/aws/session" |
| 11 | + "github.com/aws/aws-sdk-go/service/ec2" |
| 12 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 13 | +) |
| 14 | + |
| 15 | +func main() { |
| 16 | + region := "us-west-1" |
| 17 | + amiName := "iterative-cml" |
| 18 | + regions := []string{"us-east-1", "us-east-2", "us-west-2", "eu-central-1", "eu-west-1"} |
| 19 | + |
| 20 | + sess, sessError := session.NewSession(&aws.Config{ |
| 21 | + Region: aws.String(region)}, |
| 22 | + ) |
| 23 | + if sessError != nil { |
| 24 | + log.Printf("[ERROR] %s", sessError) |
| 25 | + os.Exit(1) |
| 26 | + } |
| 27 | + |
| 28 | + svc := ec2.New(sess) |
| 29 | + |
| 30 | + amiParams := &ec2.DescribeImagesInput{ |
| 31 | + Filters: []*ec2.Filter{ |
| 32 | + { |
| 33 | + Name: aws.String("name"), |
| 34 | + Values: []*string{aws.String(amiName)}, |
| 35 | + }, |
| 36 | + { |
| 37 | + Name: aws.String("architecture"), |
| 38 | + Values: []*string{aws.String("x86_64")}, |
| 39 | + }, |
| 40 | + }, |
| 41 | + } |
| 42 | + imagesRes, imagesErr := svc.DescribeImages(amiParams) |
| 43 | + if imagesErr != nil { |
| 44 | + diag.FromErr(imagesErr) |
| 45 | + } |
| 46 | + if len(imagesRes.Images) == 0 { |
| 47 | + log.Printf("[ERROR] ami %s not found", amiName) |
| 48 | + os.Exit(1) |
| 49 | + } |
| 50 | + |
| 51 | + ami := imagesRes.Images[0] |
| 52 | + amiID := *ami.ImageId |
| 53 | + amiDesc := *ami.Description |
| 54 | + |
| 55 | + for _, value := range regions { |
| 56 | + fmt.Println("Cloning", value) |
| 57 | + |
| 58 | + sess, _ := session.NewSession(&aws.Config{ |
| 59 | + Region: aws.String(value)}, |
| 60 | + ) |
| 61 | + |
| 62 | + svc := ec2.New(sess) |
| 63 | + |
| 64 | + copyResult, err := svc.CopyImage(&ec2.CopyImageInput{ |
| 65 | + SourceImageId: aws.String(amiID), |
| 66 | + SourceRegion: aws.String(region), |
| 67 | + Name: aws.String(amiName), |
| 68 | + Description: aws.String(amiDesc), |
| 69 | + }) |
| 70 | + if err != nil { |
| 71 | + fmt.Println(err) |
| 72 | + } |
| 73 | + |
| 74 | + svc.WaitUntilImageExists(&ec2.DescribeImagesInput{ |
| 75 | + ImageIds: []*string{aws.String(*copyResult.ImageId)}, |
| 76 | + Filters: []*ec2.Filter{ |
| 77 | + { |
| 78 | + Name: aws.String("state"), |
| 79 | + Values: []*string{aws.String("available")}, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }) |
| 83 | + |
| 84 | + _, modifyErr := svc.ModifyImageAttribute(&ec2.ModifyImageAttributeInput{ |
| 85 | + ImageId: aws.String(*copyResult.ImageId), |
| 86 | + LaunchPermission: &ec2.LaunchPermissionModifications{ |
| 87 | + Add: []*ec2.LaunchPermission{ |
| 88 | + { |
| 89 | + Group: aws.String("all"), |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | + }) |
| 94 | + if modifyErr != nil { |
| 95 | + fmt.Println(modifyErr) |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments