AWS SSM get-parameter-by-path Manipulate JSON - amazon-web-services

I am trying to retrieve all the parameters under a specific path from the AWS Parameter store using the command below:
aws ssm get-parameters-by-path --path some-path --no-paginate
This returns me a JSON with a lot of fields I do not need. How can I use the --query to just retrieve the name and the value?
Any documentation on how can I use the --query parameter? I have tried passing jq query strings, but that doesn't work.

You need to extract the fields from Parameters(Array) and later select the fields you want to get using {key:value} syntax:
aws ssm get-parameters-by-path --path %PATH% --no-paginate --region %REGION% --query "Parameters[].{Key:Name,Val:Value}" --output json
Output Json:
[
{
"Key": "/test/amit",
"Val": "test1"
},
{
"Key": "/test/amit1",
"Val": "test2"
}
]
Or in case you want the output in text, change --output to text.
Output Text:
/test/amit test1
/test/amit1 test2
More info about Controlling Command Output from the AWS CLI.

Related

AWS CLI to get only Volumeid and tagvalue

From AWS using CLI to get volume details via describe-volumes
Sample volume Details
VolumeId = Vol-********
Attachments.InstanceId = i-*********
Tags : Key = Name Value = sometext
Expected Output via AWS CLI:
Vol-******** i-********* sometext
Codes I tried:
aws ec2 describe-volumes --query "Volumes[*].{ID:VolumeId,InstanceId:Attachments[0].InstanceId,Tag:Tags}" --output text
Output what I am getting:
Vol-******** i-*********
TAG Name sometext
Looking for help to get output as below.
Expected Output via AWS CLI:
Vol-******** i-********* sometext
If you are looking for a specific tag value of a volume:
aws ec2 describe-volumes --query "Volumes[*].{ID:VolumeId,InstanceId:Attachments[0].InstanceId,tag:Tags[0].Value}" --output text
vol-******* i-****** second
If you are looking for all the tags value, it will come as below: format= text
aws ec2 describe-volumes --query "Volumes[*].{ID:VolumeId,InstanceId:Attachments[0].InstanceId,tag:Tags[].Value}" --output text
vol-0fa1417c2369e7440 i-0533e831213cf0cc4
TAG second
TAG test
If you are looking for all the tags value, it will come as below. format= json
aws ec2 describe-volumes --query "Volumes[*].{ID:VolumeId,InstanceId:Attachments[0].InstanceId,tag:Tags[].Value}" --output json
[
{
"InstanceId": "i-******",
"tag": [
"second",
"test"
],
"ID": "vol-******"
}
]

AWS CLI Loading Output Onto Multiple Lines

I am using the aws cli to give me some output which I require in .csv format.
The below shows the commands I've put together:
aws ec2 describe-instances --output text --query 'Reservations[*].Instances[*].[VpcId, SubnetId, InstanceId, InstanceType, ImageId, State.Name, LaunchTime, Placement.AvailabilityZone, Placement.Tenancy, PrivateIpAddress, PrivateDnsName, PublicDnsName,[Tags[?Key==`Name`].Value],[Tags[?Key==`PowerData`].Value] ]' --output text | sed -E 's/\s+/,/g'
For some reason, the final two pieces of info, "Name" and "PowerData" end up on separate lines. This screws up my formatting when I open it up in a spreadsheet.
Anyone have any ideas on what might be causing that or how I may resolve it please?
The reason is TAGs return array so you need to print the only value on 0 index to display the TAGs on a single line.
You can verify this
aws ec2 describe-instances --output json --query 'Reservations[*].Instances[*].Tags[]'
output
[
{
"Key": "Name",
"Value": "demo"
},
{
"Key": "PowerData",
"Value": "demo"
}
]
so you need pipe expression
Tags[?Key==`Name`].Value| [0]
With the addition of filters, we could pass the result of one expression to another, operating on the result of a projection (or any expression).
Expression:
foo.*.bar | [0]
jmespath-pipe-expressions
So try this
aws ec2 describe-instances --output text --query 'Reservations[*].Instances[*].[VpcId, SubnetId, InstanceId, InstanceType, ImageId, State.Name, LaunchTime, Placement.AvailabilityZone, Placement.Tenancy, PrivateIpAddress, PrivateDnsName, PublicDnsName,Tags[?Key==`Name`].Value| [0],Tags[?Key==`PowerData`].Value | [0] ]' --output text | sed -E 's/\s+/,/g'

How do I get the value of a tag in the aws cli describe-instances output?

I want to get the instance id and the value of the name tag, this doesn't work:
aws ec2 describe-instances --filters 'Name=tag:Name,Values=InstaneNameTagPrefix*' --query 'Reservations[*].Instances[*].{InstanceId:InstanceId,Tags:Tags.Key=Name}'
After going this through JMESPath example, I was able to make it working. Try this and let me know how it goes at your end.
aws ec2 describe-instances --filters 'Name=tag:Name,Values=myDevEC2*' --query 'Reservations[*].Instances[*].{InstanceId:InstanceId,Tags:Tags[?Key == `Name`] | [0].Value}'
Output -
[
[
{
"InstanceId": "i-xxxxxxxxxxxxxx",
"Tags": "myDevEC2-123"
}
]
]
Since Tags is an array, you will need to filter to just Name pair. After that pipe the Tags result and select Value.
PS - You might want to rename JSON output second element to InstanceName instead Tags :)

AWS SSM Parameters Store

Is there anyway to just nuke / remove all items in AWS Parameters Store?
All the command line I found are to remove it either one by one or remove it given a list of names.
I also tried using
aws ssm delete-parameters --cli-input-json test.json
with test.json file looks like this
{
"Names": [
"test1",
"test2"
]
}
still does not work..
Ideally if I can use --query and use it as is, that'd be great.
I'm using --query like so
aws ssm get-parameters-by-path --path / --max-items 2 --query 'Parameters[*].[Name]'
When you need to delete all parameters by path in AWS Systems Manager Parameter Store and there are more than 10 parameters you have to deal with pagination.
Otherwise, an the command will fail with the error:
An error occurred (ValidationException) when calling the DeleteParameters operation: 1 validation error detected: Value '[/config/application/prop1, ...]' at 'names' failed to satisfy constraint: Member must have length less than or equal to 10
The following Bash script using AWS CLI pagination options deletes any number of parameters from AWS SSM Parameter Store by path:
#!/bin/bash
path=/config/application_dev/
while : ; do
aws ssm delete-parameters --names $(aws ssm get-parameters-by-path --path "$path" --query "Parameters[*].Name" --output text --max-items 10 $starting_token | grep -v None)
next_token=$(aws ssm get-parameters-by-path --path "$path" --query NextToken --output text --max-items 10 | grep -v None)
if [ -z "$next_token" ]; then
starting_token=""
break
else
starting_token="--starting-token $next_token"
fi
done
You can combine get-parameters-by-path with delete-parameters:
aws ssm delete-parameters --names `aws ssm get-parameters-by-path --path / --query Parameters[].Name --output text`
I tested it by creating two parameters, then running the above command. It successfully deleted by parameters.
try this and execute multiple times
aws ssm delete-parameters --names `aws ssm get-parameters-by-path --path / --recursive --query Parameters[].Name --output text --max-items 9`
Adding to the above. I had to delete around 400 params from the parameter store. Ran the below in command line and it did it! (Change 45 in for loop to whatever number you like);
for ((n=0;n<**45**;n++)); do
aws ssm delete-parameters --names `aws ssm get-parameters-by-path --path / --recursive --query Parameters[].Name --output text --max-items 9`
done
This is my one line solution for this:
$ for key in $(aws ssm get-parameters-by-path --path "/" --recursive | jq -r '.Parameters[] | .Name' | tr '\r\n' ' '); do aws ssm delete-parameter --name ${key}; done
NOTE: Be careful if you copy & paste this as it will remove everything under "/"

How to remove extra parentheses and angular brackets while using aws cli?

I am trying to get the info using of NetworkInterface using following command.
[root#ip-172-29-45-82 ~]# aws ec2 describe-instances --instance-ids i-dd6f6f53 --query Reservations[*].{VpcId:Instances[*].NetworkInterfaces[*].VpcId}
[
{
"VpcId": [
[
"vpc-38fb075d"
]
]
}
]
I don't want extra parentheses and angular brackets.
It should be something like as follows:
["VpcId":"vpc-38fb075d"] or [{"VpcId":"vpc-38fb075d"}] or {"VpcId":"vpc-38fb075d"}
Is there any way to achieve above output from the above command.
Thanks in advance.
Result can be further refined by adding this to the end of your command
--output text
And the output will be unstructured text:
"VpcId": "vpc-38fb075d"
I solved it.
I used following command.
aws ec2 describe-instances --instance-ids i-dd6f6f53 --query Reservations[0].{VpcId:Instances[0].NetworkInterfaces[0].VpcId}
I just changed * to 0 and its working.
Now the output is
{
"VpcId": "vpc-38fb075d"
}
You could run - given that you can't have ENIs in different VPCs:
aws ec2 describe-instances --query 'Reservations[].Instances[].VpcId' --output text --instance-ids i-dd6f6f53
You can also try nesting your JSON objects - for example, trying to get the name tag from the parent bracket Tags:
aws ec2 describe-instances --query 'Reservations[].Instances[].[InstanceId,PrivateIpAddress, [Tags[?Key==`Name`].Value][0][0]]' --output text
It is easier to develop the query using the JSON output, then cleaning things up with --output text.