1

I have a hosted zone and record set that route to multiple addresses. I'd like to update the record set with adding or removing one IP address in the list. Unfortunately, AWS CLI doesn't provide the option of deleting/adding the value of resource record in route53

{
      "Comment": "Update the A record set",
      "Changes": [
        {
          "Action": "UPSERT",
          "ResourceRecordSet": {
            "Name": "mydomain.com",
            "Type": "A",
            "TTL": 300,
            "ResourceRecords": [
              {
                "Value": "XX.XX.XX.XX"
              }
            ]
          }
        }
      ]
    }

I can add multiple IP addresses into your json like this manually. But I want to add multiple IPs to the json file using bash automatically.

{
    "Comment": "Update the A record set",
    "Changes": [{
        "Action": "UPSERT",
        "ResourceRecordSet": {
            "Name": "mydomain.com",
            "Type": "A",
            "TTL": 300,
            "ResourceRecords": [{
                    "Value": "XX.XX.XX.XX"
                },
                {
                    "Value": "XX.XX.XX.XX"
                }
            ]
        }
    }]
}
  • 1
    bash is just a shell, so I replaced that with command-line; I wasn't sure why clipboard was involved. Perhaps you can specify the interaction you'd like to take place for this replacement? What's the input and resulting command? – Jeff Schaller Feb 13 at 19:42
  • If you definitely want to do this on the command line as a one-time workaround jq is the definite tool for it. If you want to produce a production-level tool to do this automatically and reliably you should try a language like Python or Ruby - they are much better suited for dealing with nested data structures. – l0b0 Feb 14 at 2:54
3

Adding, using

$ jq '.Changes[0].ResourceRecordSet.ResourceRecords += [{"Value": "foobar"}]' file.json
{
  "Comment": "Update the A record set",
  "Changes": [
    {
      "Action": "UPSERT",
      "ResourceRecordSet": {
        "Name": "mydomain.com",
        "Type": "A",
        "TTL": 300,
        "ResourceRecords": [
          {
            "Value": "XX.XX.XX.XX"
          },
          {
            "Value": "foobar"
          }
        ]
      }
    }
  ]
}
  • Thanks! It prints the output on the terminal, but how do I save the file with the changes? And what is the command to add multiple values? – Nani Feb 13 at 20:20
  • I don't see that jq has a "save in place" option (might be wrong here), so you pipe the output into > tmpfile && mv tmpfile file.json – glenn jackman Feb 13 at 20:26
  • To add multiple items, just add more objects into the array: += [{"Value":"1"}, {"Value":"2"}, ...] – glenn jackman Feb 13 at 20:27
0

you may want to give it a try to another unix utility: jtc, it's capable of applying modifications in-file (with -f option):

bash $ jtc -w'<ResourceRecords>l' -i'{ "Value": "YY.YY.YY.YY" }' -f file.json
bash $ jtc file.json 
{
   "Changes": [
      {
         "Action": "UPSERT",
         "ResourceRecordSet": {
            "Name": "mydomain.com",
            "ResourceRecords": [
               {
                  "Value": "XX.XX.XX.XX"
               },
               {
                  "Value": "YY.YY.YY.YY"
               }
            ],
            "TTL": 300,
            "Type": "A"
         }
      }
   ],
   "Comment": "Update the A record set"
}
bash $ 

and also deleting entry(ies):

bash $ jtc -w'<ResourceRecords>l [0]' -p -f file.json
bash $ cat  file.json 
{
   "Changes": [
      {
         "Action": "UPSERT",
         "ResourceRecordSet": {
            "Name": "mydomain.com",
            "ResourceRecords": [
               {
                  "Value": "YY.YY.YY.YY"
               }
            ],
            "TTL": 300,
            "Type": "A"
         }
      }
   ],
   "Comment": "Update the A record set"
}
bash $ 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.