Random things I found out
 

Format aws-cli output as CSV

One of my recurring annoyances is that the AWS cli utility doesn’t support a CSV output option. This command fixes this (at least for snapshot data).

aws ec2 describe-snapshots --owner-ids 12345 --output json |jq -r '.Snapshots[] | [.Description, .Encrypted, .OwnerId, .Progress, .SnapshotId, .StartTime, .State, .VolumeId, .VolumeSize, .StorageTier] | @csv' >snapshots.csv

One thing to note here is that jq is case sensitive. If you specified “OwnerID” rather than “OwnerId” you would get a blank column.

The secret is that you have to tell jq how to format the input. the aws ec2 describe-snapshots output looks like this:

{
    "Snapshots": [
        {
            "Description": SourceSnapshot snap-0324302948024a",
            "Encrypted": false,
            "OwnerId": "1234567890",
            "Progress": "100%",
            "SnapshotId": "snap-01abc2342df",
            "StartTime": "2018-09-25T19:19:03+00:00",
            "State": "completed",
            "VolumeId": "vol-ffffffff",
            "VolumeSize": 3,
            "StorageTier": "standard"
        },
        { ... 
        }
}

So in this case, you tell jq that the first level is “Snapshots”, then you enumerate the data within that, so the @csv part will properly wrap the fields without inadvertently causing an issue when a comma is thrown into the description field.

Leave a Reply

Your email address will not be published. Required fields are marked *