|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX - License - Identifier: Apache - 2.0 |
| 3 | +// snippet-start:[route53.go-v2.route53.hello] |
| 4 | +package main |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "errors" |
| 9 | + "flag" |
| 10 | + "fmt" |
| 11 | + "log" |
| 12 | + |
| 13 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 14 | + "github.com/aws/aws-sdk-go-v2/config" |
| 15 | + "github.com/aws/aws-sdk-go-v2/service/route53" |
| 16 | + "github.com/aws/aws-sdk-go-v2/service/route53/types" |
| 17 | +) |
| 18 | + |
| 19 | +const CloudFrontHostedZoneID = "Z2FDTNDATAQYW2" |
| 20 | +const region = "us-east-1" |
| 21 | + |
| 22 | +var ( |
| 23 | + // Define the flags |
| 24 | + // hostedZoneID is the ID of the hosted zone. |
| 25 | + hostedZoneID = "" |
| 26 | + // domain is the domain to point to the CloudFront domain. |
| 27 | + domain = "" |
| 28 | + // cloudfrontDomain is the CloudFront domain. |
| 29 | + cloudfrontDomain = "" |
| 30 | +) |
| 31 | + |
| 32 | +// main uses the AWS SDK for Go (v2) to operate an Amazon route53 client and |
| 33 | +// change resource record sets to point to a domain. |
| 34 | +// This example uses the default settings specified in your shared credentials |
| 35 | +// and config files. |
| 36 | +func main() { |
| 37 | + |
| 38 | + // Get the hosted zone ID, domain, and CloudFront domain from the user. |
| 39 | + flag.StringVar(&hostedZoneID, "bucket", "", "<HOSTED ZONE ID>") |
| 40 | + flag.StringVar(&cloudfrontDomain, "cert", "", "<CLOUDFRONT DOMAIN>") |
| 41 | + flag.StringVar(&domain, "domain", "", "<YOUR DOMAIN>") |
| 42 | + flag.Parse() |
| 43 | + if hostedZoneID == "" { |
| 44 | + log.Println(errors.New("please setup hosted zone ID")) |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + if cloudfrontDomain == "" { |
| 49 | + log.Println(errors.New("please setup CloudFront domain")) |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + if domain == "" { |
| 54 | + log.Println(errors.New("please setup your domain")) |
| 55 | + return |
| 56 | + } |
| 57 | + |
| 58 | + ctx := context.TODO() |
| 59 | + sdkConfig, err := config.LoadDefaultConfig(ctx, config.WithRegion(region)) |
| 60 | + if err != nil { |
| 61 | + fmt.Println("Couldn't load default configuration. Have you set up your AWS account?") |
| 62 | + fmt.Println(err) |
| 63 | + return |
| 64 | + } |
| 65 | + route53Client := route53.NewFromConfig(sdkConfig) |
| 66 | + output, err := route53Client.ChangeResourceRecordSets(ctx, &route53.ChangeResourceRecordSetsInput{ |
| 67 | + HostedZoneId: aws.String(hostedZoneID), |
| 68 | + ChangeBatch: &types.ChangeBatch{ |
| 69 | + Changes: []types.Change{ |
| 70 | + { |
| 71 | + Action: types.ChangeActionUpsert, |
| 72 | + ResourceRecordSet: &types.ResourceRecordSet{ |
| 73 | + Type: types.RRTypeA, |
| 74 | + Name: aws.String(domain), |
| 75 | + AliasTarget: &types.AliasTarget{ |
| 76 | + DNSName: aws.String(cloudfrontDomain), |
| 77 | + HostedZoneId: aws.String(CloudFrontHostedZoneID), |
| 78 | + EvaluateTargetHealth: false, |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }) |
| 85 | + if err != nil { |
| 86 | + fmt.Printf("Couldn't change resource record sets. Here's why: %v\n", err) |
| 87 | + return |
| 88 | + } |
| 89 | + fmt.Println(output) |
| 90 | +} |
| 91 | + |
| 92 | +// snippet-end:[route53.go-v2.route53.hello] |
0 commit comments