0

Could I get help on getting the values of Alias record in the cli table output?

The test.emea.example.com. is an alias.

BTW, if the DNS record is multi-value, how to flat and concatenate the values

Below is the example:

 $ aws route53 list-resource-record-sets --hosted-zone-id Z34XXXXXXXX4EF --query "ResourceRecordSets[?Type=='A']"
[
    {
        "ResourceRecords": [
            {
                "Value": "10.189.134.12"
            }
        ], 
        "Type": "A", 
        "Name": "dnsforwarder0.emea.example.com.", 
        "TTL": 300
    }, 
    {
        "ResourceRecords": [
            {
                "Value": "10.189.134.47"
            }
        ], 
        "Type": "A", 
        "Name": "dnsforwarder1.emea.example.com.", 
        "TTL": 300
    }, 
    {
        "ResourceRecords": [
            {
                "Value": "10.189.134.78"
            }
        ], 
        "Type": "A", 
        "Name": "dnsforwarder2.emea.example.com.", 
        "TTL": 300
    }, 
    {
        "AliasTarget": {
            "HostedZoneId": "Z32O12XQLNTSW2", 
            "EvaluateTargetHealth": false, 
            "DNSName": "dualstack.kubernetes-elb-k8fca-prod-emea-1745420721.eu-west-1.elb.amazonaws.com."
        }, 
        "Type": "A", 
        "Name": "test.emea.example.com."
    }
]
[Tiger-Pengs-MacBook-Pro:~/aws/aws_fed]
tpeng $ aws route53 list-resource-record-sets --hosted-zone-id Z34XXXXXXXX4EF --query "ResourceRecordSets[?Type=='A'].[Name,Type,ResourceRecords[0].Value]" --output table --color off 
-----------------------------------------------------------
|                 ListResourceRecordSets                  |
+----------------------------------+----+-----------------+
|  dnsforwarder0.emea.example.com. |  A |  10.189.134.12  |
|  dnsforwarder1.emea.example.com. |  A |  10.189.134.47  |
|  dnsforwarder2.emea.example.com. |  A |  10.189.134.78  |
|  test.emea.example.com.          |  A |  None           |
+----------------------------------+----+-----------------+
Tiger peng
  • 647
  • 6
  • 10
  • Can you show a sample output of what you are seeking? – John Rotenstein May 31 '19 at 23:27
  • Before going down this rabbit-hole, would you consider running a separate command just to get the `AliasTarget` records, rather than having to get them in one command? Alternatively, have you considered doing it in Python, where you would have much more control over the results? – John Rotenstein May 31 '19 at 23:27
  • "BTW, if the DNS record is multi-value, how to flat and concatenate the values". I am not certain I understand what you mean by flattening and concatenating values. In any case it should be asked in a separate question, with some example inputs and what you expect to see in the output. – Alex Harvey Jun 01 '19 at 07:33
  • John Rotenstein: I'd like the DNSName of the Alias shown in the output, not the 'None'; and I like all in one cmd - bad old Perlish habit ;-) – Tiger peng Jun 10 '19 at 14:13

2 Answers2

1

Based on John and Alex's idea, I use the union, as well as join function, to get what I want. However, it is too verbose.

$ aws route53 list-resource-record-sets --hosted-zone-id Z34XXXXXXXX4EF --query "[ResourceRecordSets[?Type=='A' && contains(keys(@), 'AliasTarget')].[Name,Type,AliasTarget.DNSName],ResourceRecordSets[?Type=='A' && contains(keys(@), 'ResourceRecords')].[Name,Type,join(', ', ResourceRecords[].Value)]]" --output table
-------------------------------------------------------------------------------------------------------------------------
|                                              ListResourceRecordSets                                                   |
+-----------------------------+----+------------------------------------------------------------------------------------+
|  test.example.com.          |  A |  dualstack.kubernetes-elb-k8fca-prod-emea-1745420721.eu-west-1.elb.amazonaws.com.  |
|  dnsforwarder0.example.com. |  A |  10.189.134.12                                                                     |
|  dnsforwarder1.example.com. |  A |  10.189.134.47                                                                     |
|  dnsforwarder2.example.com. |  A |  10.189.134.78                                                                     |
|  privatezone.example.com.   |  A |  10.20.30.40, 10.10.30.40, 10.30.30.40                                             |
+-----------------------------+----+------------------------------------------------------------------------------------+
Tiger peng
  • 647
  • 6
  • 10
0

You can do this by combining a JMESpath And expression with the keys and contains functions. So this would work:

aws route53 list-resource-record-sets --hosted-zone-id Z34XXXXXXXX4EF \
  --query 'ResourceRecordSets[?Type==`A` && contains(keys(@), `AliasTarget`)].Name'
Alex Harvey
  • 14,494
  • 5
  • 61
  • 97
  • Alex, your query searches the A type Alias only (normal A records are excluded). I am looking for the query retrieves IP if it is normal A record and the DNSName if it is an Alias record. – Tiger peng Jun 10 '19 at 14:17
  • I guess you can just delete `Type==\`A\` &&` from the query @Tigerpeng ? Your example suggested that's what you wanted. – Alex Harvey Jun 10 '19 at 14:19