Skip to content

Instantly share code, notes, and snippets.

@undeadops
Created February 6, 2018 21:45
Show Gist options
  • Save undeadops/c7e7e6a8d044bbbc985791514027dbe6 to your computer and use it in GitHub Desktop.
Save undeadops/c7e7e6a8d044bbbc985791514027dbe6 to your computer and use it in GitHub Desktop.
aws-sdk-go-v2 - Route53 Create Invalid
package main
import (
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/awserr"
"github.com/aws/aws-sdk-go-v2/aws/external"
"github.com/aws/aws-sdk-go-v2/service/route53"
)
func main() {
cfg, err := external.LoadDefaultAWSConfig()
if err != nil {
panic("failed to load config, " + err.Error())
}
var c []route53.Change
change := route53.Change{ // Required
Action: route53.ChangeAction("CREATE"), // Required
ResourceRecordSet: &route53.ResourceRecordSet{ // Required
Name: aws.String("sweethost.sub.example.com."), // Required
Type: route53.RRType("CNAME"), // Required
ResourceRecords: []route53.ResourceRecord{
{ // Required
Value: aws.String("sweetalias.ec2.internal"), // Required
},
},
},
}
changeErr := change.Validate()
if changeErr != nil {
fmt.Printf("There was an error in your Change Record: %s\n\n", change)
}
c = append(c, change)
params := &route53.ChangeResourceRecordSetsInput{
ChangeBatch: &route53.ChangeBatch{
Comment: aws.String("CNAME upserts for zone Run by DNS-SYNC"),
Changes: c,
},
HostedZoneId: aws.String("-------ZONEID-HERE--------"),
}
svc := route53.New(cfg)
parmsErr := params.Validate()
if parmsErr != nil {
fmt.Printf("There was an error with Validation of route53.ChangeResourceRecordSetsInput")
panic(parmsErr)
}
fmt.Printf("INPUT ---------------------------------------------------\n\n")
fmt.Printf("%s\n\n", params.GoString())
fmt.Printf("------------------------------------------------------\n\n")
req := svc.ChangeResourceRecordSetsRequest(params)
res, err := req.Send()
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case route53.ErrCodeNoSuchHostedZone:
fmt.Println(route53.ErrCodeNoSuchHostedZone, aerr.Error())
case route53.ErrCodeNoSuchHealthCheck:
fmt.Println(route53.ErrCodeNoSuchHealthCheck, aerr.Error())
case route53.ErrCodeInvalidChangeBatch:
fmt.Println(route53.ErrCodeInvalidChangeBatch, aerr.Error())
case route53.ErrCodeInvalidInput:
fmt.Println(route53.ErrCodeInvalidInput, aerr.Error())
case route53.ErrCodePriorRequestNotComplete:
fmt.Println(route53.ErrCodePriorRequestNotComplete, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
}
fmt.Printf("Res: %s\n\n", res)
}
@undeadops
Copy link
Author

Output of above code:

go run main.go
INPUT ---------------------------------------------------

{
  ChangeBatch: {
    Changes: [{
        Action: CREATE,
        ResourceRecordSet: {
          Failover: ,
          Name: "sweethost.sub.example.com.",
          Region: ,
          ResourceRecords: [{
              Value: "sweetalias.ec2.internal"
            }],
          Type: CNAME
        }
      }],
    Comment: "CNAME upserts for zone Run by DNS-SYNC"
  },
  HostedZoneId: "-------ZONEID-HERE--------"
}

------------------------------------------------------

InvalidInput InvalidInput: Invalid request
	status code: 400, request id: c96c28ae-0b83-11e8-8731-091632d7a4a7
Res: <nil>

@undeadops
Copy link
Author

Note: Additional Region: , and Failover: , which I did not specify in the code.

@undeadops
Copy link
Author

TTL is required, Added TTL to code and it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment