This post is the starting point of a new blog series called “What I’ve learned last week”. I came up with this idea cause I felt that I need to motivate myself systematically learn new things, so without further ado, let’s get to the point.
#1 Chocolatey is super-easy to use and a must-have when using Windows
I’ve used package managers on Linux for some time now, but never on Windows. There’s always a firs time for everything. Last Monday I had to set up my new laptop, for that I’ve installed Chocolatey – this is a blessing. The installation took maybe 2 minutes, installing all of the software that I needed took maybe another 10, I can’t recommend this software enough.
choco install all the things!!!
If you’re interested, please go to https://chocolatey.org/docs/installation or just have a look at the video below.
#2 Parsing a JSON file is a piece of cake when using the “jq” command on Linux (AWS)
I had to get some very important information from a JSON file returned by the aws sts get-session-token
command which returns data that looks like the snippet below:
{
"Credentials": {
"SecretAccessKey": "Sb5pXXXXXQY3/qPoL/jUjMyXXXXXuev/0j3vXs",
"SessionToken": "FQoGZXIvYXdzENv//////////wEaDEeLj11xTbnBk6azRCKsAacCiJK85DSXExJ81GtHrkEyev8eedQcFY4EoM00PsD1FAEMOfzQclVEFsoh9Y4ZHUjgGmCPRUnYCLNc2NnHkadvmI+MT7jRRa1Mo2HA/HYStOOwEyfXFYG91MNj9rVXXXXXXXXA6+7FF6jcxj3cr1U9CDB9hjW+jGq4UgjxXXXXXXXCoIidDjl4Z911zIzP/Q+FYA1R2CUz5Y32EUaMzR+bQvmheUolLOq3wU=",
"Expiration": "2018-11-14T21:29:56Z",
"AccessKeyId": "ASIXXXXXXXXZ27X"
}
}
Thanks to brilliant AWS Mentor with years of experience getting the desired data was no problem, check this out:
[miko@localhost ~]$ cat aws.json | jq '.Credentials'
{
"SecretAccessKey": "Sb5pXXXXXQY3/qPoL/jUjMyXXXXXuev/0j3vXs",
"SessionToken": "FQoGZXIvYXdzENv//////////wEaDEeLj11xTbnBk6azRCKsAacCiJK85DSXExJ81GtHrkEyev8eedQcFY4EoM00PsD1FAEMOfzQclVEFsoh9Y4ZHUjgGmCPRUnYCLNc2NnHkadvmI+MT7jRRa1Mo2HA/HYStOOwEyfXFYG91MNj9rVXXXXXXXXA6+7FF6jcxj3cr1U9CDB9hjW+jGq4UgjxXXXXXXXCoIidDjl4Z911zIzP/Q+FYA1R2CUz5Y32EUaMzR+bQvmheUolLOq3wU=",
"Expiration": "2018-11-14T21:29:56Z",
"AccessKeyId": "ASIXXXXXXXXZ27X"
}
And if we want to take only specific values we can do it like so:
[miko@localhost ~]$ cat aws.json | jq '.Credentials.AccessKeyId'
"ASIXXXXXXXXZ27X"
[miko@localhost ~]$ cat aws.json | jq '.Credentials.SecretAccessKey'
"Sb5pXXXXXQY3/qPoL/jUjMyXXXXXuev/0j3vXs"
Attention!
There is one thing that you have to know about when using the jq
command. By default the output is formatted as a JSON string with quotes:
"Sb5pXXXXXQY3/qPoL/jUjMyXXXXXuev/0j3vXs"
If you want to assign this value to a variable or forward it to a non-JSON-based systems. use the --raw-output / -r
parameter. It will work like this:
[miko@localhost ~]$ cat aws.json | jq -r '.Credentials.SecretAccessKey'
Sb5pXXXXXQY3/qPoL/jUjMyXXXXXuev/0j3vXs
Hooray! No quotes!
Believe me, this may look stupid, but I wasted too much time on this “minor” detail.
Quick jq demo
More on this topic
AWS Docs: https://docs.aws.amazon.com/cli/latest/reference/sts/get-session-token.html
jq manual: https://stedolan.github.io/jq/manual/
jq examples: https://linuxhint.com/bash_jq_command/
Hope some of this was helpful,
See you next week!
Mike
Be First to Comment