Pull an EC2 console log using the AWS CLI api and jq
AWS provides a REST Api to everything they offer, and returns the results in Json format. If you have a server in a private network in a VPC, you might want to get the logs to see what happened at boot time, and the aws cli gives you a simple way of doing that:
CODE:
aws ec2 get-console-output --instance-id {i-xxxxxxxx}
This is great for many things, but the data from a server console log isn't one of them. You get the log as a giant string in the 'Output' key:
This is where the great json processor jq can be used, both to extract just the output, and to convert the encoded newline characters back into actual newlines using the -r flag.
And we get the logs, just as if we were sitting at the console:
As it turns out, there is a far easier solution to this problem, pointed out in a comment to this post. You can just use an --output text option:
CODE:
{
"InstanceId": "i-005a7e6e8ae....",
"Output": "[ 0.000000] Initializing cgroup subsys cpuset\r\n[ 0.000000] Initializing cgroup subsys cpu\r\n[ 0.000000] Initializing cgroup subsys cpuacct\r\n[ 0.000000] Linux version 4.1.13-19.30.amzn1.x86_64 (mockbuild@gobi-build-64011) (gcc version 4.8.3 20140911 (Red Hat 4.8.3-9) (GCC) ) #1 SMP Fri Dec 11 03:42:10 UTC 2015\r\n[ 0.000000] Command line: root=LABEL=/ console=ttyS0 \r\n[ 0.000000] e820: BIOS-provided physical RAM map:\r\n[ 0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009dfff] usable\r\n[ 0.000000] BIOS-e820: [mem 0x000000000009e000-0x000000000009ffff] reserved\r\n[ 0.000000] BIOS-e820: [mem 0x00000000000e0000-0x00000000000fffff] reserved\r\n[ 0.000000] BIOS-e820: [mem 0x0000000000100000-0x000000003fffffff] usable\r\n[ 0.000000] BIOS-e820: [mem 0x00000000fc000000-0x00000000ffffffff] reserved\r\n[ 0.000000] NX (Execute Disable) protection: active\r\n[ 0.000000] SMBIOS 2.4 present.\r\n[ 0.000000] Hypervisor detected:
This is where the great json processor jq can be used, both to extract just the output, and to convert the encoded newline characters back into actual newlines using the -r flag.
CODE:
aws ec2 get-console-output --instance-id i-005a7e6e8aeae06 | jq -r '.Output'
And we get the logs, just as if we were sitting at the console:
CODE:
[ 0.000000] Initializing cgroup subsys cpuset
[ 0.000000] Initializing cgroup subsys cpu
[ 0.000000] Initializing cgroup subsys cpuacct
[ 0.000000] Linux version 4.1.13-19.30.amzn1.x86_64 (mockbuild@gobi-build-64011) (gcc version 4.8.3 20140911 (Red Hat 4.8.3-9) (GCC) ) #1 SMP Fri Dec 11 03:42:10 UTC 2015
[ 0.000000] Command line: root=LABEL=/ console=ttyS0
[ 0.000000] e820: BIOS-provided physical RAM map:
[ 0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009dfff] usable
[ 0.000000] BIOS-e820: [mem 0x000000000009e000-0x000000000009ffff] reserved
[ 0.000000] BIOS-e820: [mem 0x00000000000e0000-0x00000000000fffff] reserved
[ 0.000000] BIOS-e820: [mem 0x0000000000100000-0x000000003fffffff] usable
[ 0.000000] BIOS-e820: [mem 0x00000000fc000000-0x00000000ffffffff] reserved
[ 0.000000] NX (Execute Disable) protection: active
[ 0.000000] SMBIOS 2.4 present.
[ 0.000000] Hypervisor detected: Xen
[ 0.000000] Xen version 4.2.
[ 0.000000] Netfront and the Xen platform PCI driver have been compiled for this kernel: unplug emulated NICs.
[ 0.000000] Blkfront and the Xen platform PCI driver have been compiled for this kernel: unplug emulated disks.
[ 0.000000] You might have to change the root device
[ 0.000000] from /dev/hd[a-d] to /dev/xvd[a-d]
As it turns out, there is a far easier solution to this problem, pointed out in a comment to this post. You can just use an --output text option:
CODE:
aws ec2 get-console-output --output text --instance-id i-005a7e6e8aeae06
Comments
Display comments as Linear | Threaded
margaret on :