Home > Back-end >  How to ftech values from nested josn via aws cli from DynamoDB
How to ftech values from nested josn via aws cli from DynamoDB

Time:12-11

I am trying to list the Items and other values from the AWS Dynamodb table(DDNS), However, if only look for InstnaceId then I am able to get the entire list of my instances, however i want to get few more selective values from the table which is a nested json, I am not able to figure about how to do that.

Below is Successful while only looking for InstanceId:

(awscliv2) $ aws dynamodb scan --table-name DDNS --query "Items[*].{InstanceId:InstanceId}" --region us-west-2
[
    {
        "InstanceId": {
            "S": "i-03dd05693e4y43b9b"
        }
    },
    {
        "InstanceId": {
            "S": "i-07b4a8yr38d89faf1"
        }
    },
    {
        "InstanceId": {
            "S": "i-029340cb56eeae8a1"
        }
    },

View From AWS DynamoDB console:

Below I could see, there are two columns

  • one is InstanceId
  • another is InstanceAttributes and all these values nested in this only

enter image description here

Below is just what the nested output looks like:

  • precisely from the below I want to list down values like InstanceId, RootDeviceName, ImageId and InstanceType etc.
  • I am failing to get there to list them.

enter image description here

apologies, i am adding pics here Just to address my issues. any help will be very much appreciated.

CodePudding user response:

You Should Probably try below which is working for me on my test.

#!/bin/bash
# List the DynamoDB contents from table
aws dynamodb scan --table-name DDNS --region us-west-2 \
  --query 'Items[*].{ "Instance IP" :    InstanceAttributes.M.Reservations.L[*].M.Instances.L[*].M.PrivateIpAddress.S[]|[0],
                      "Instance TYPE":   InstanceAttributes.M.Reservations.L[*].M.Instances.L[*].M.InstanceType.S[]|[0],
                      "Instance ID":     InstanceAttributes.M.Reservations.L[*].M.Instances.L[*].M.InstanceId.S[]|[0],
                      "Image ID":        InstanceAttributes.M.Reservations.L[*].M.Instances.L[*].M.ImageId.S[]|[0],
                      "Root Device":     InstanceAttributes.M.Reservations.L[*].M.Instances.L[*].M.RootDeviceName.S[]|[0] }' \
  --output table \
  --no-cli-pager

Result:

----------------------------------------------------------------------------------------------------
|                                               Scan                                               |
 ------------------------ ---------------------- --------------- ------------------ --------------- 
|        Image ID        |     Instance ID      |  Instance IP  |  Instance TYPE   |  Root Device  |
 ------------------------ ---------------------- --------------- ------------------ --------------- 
|  ami-0660c6f78888h2dcad |  i-03dd05693e2fe3b9b |  10.0.10.200 |  r6i.large       |  /dev/sda1    |
|  ami-06811f95599jjh8126 |  i-07b4a16b44d89faf1 |  10.0.10.100 |  r6i.large       |  /dev/sda1    |
|  ami-0660c6f7c9dhhg6cad |  i-025830cb11llae8a1 |  10.0.10.110 |  r6i.large       |  /dev/sda1    |
|  ami-087945320da3908tf3 |  i-0c3e05430176nm5db |  10.0.10.111 |  r6i.large       |  /dev/sda1    |
----------------------------------------------------------------------------------------------------
  • Related