How to query AWS Stack Outputs? - amazon-web-services

My Stack Outputs:
"Outputs": [
{
"OutputKey": "InstanceId",
"OutputValue": "i-0ed2834d95ae5bb98",
"Description": "Instance Id"
},
{
"OutputKey": "PrivateIp",
"OutputValue": "10.176.66.46",
"Description": "Private IP address"
},
{
"OutputKey": "EbsVolumeId",
"OutputValue": "vol-03837489a20032881",
"Description": "EbsVolume"
}
I tried to query the PrivateIp of stack by using the command below but the command doesn't return anything.
aws cloudformation describe-stacks --stack-name my-stack-01 --query "Stacks[0].Outputs[?OutputKey=="PrivateIp"].OutputValue" --output text
What did I do wrong?
Thx in advance!

You must use single-quotes in the filter part ?OutputKey=="PrivateIp" of your query. So, change your whole query to:
aws cloudformation describe-stacks --stack-name my-stack-01 --query "Stacks[0].Outputs[?OutputKey=='PrivateIp'].OutputValue" --output text
This will work.

Related

How to parse aws cli output using jq

aws elbv2 describe-target-group-attributes \
--target-group-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-targets/73e2d6bc24d8a067
provides
{
"Attributes": [
{
"Value": "false",
"Key": "stickiness.enabled"
},
{
"Value": "300",
"Key": "deregistration_delay.timeout_seconds"
},
{
"Value": "lb_cookie",
"Key": "stickiness.type"
},
{
"Value": "86400",
"Key": "stickiness.lb_cookie.duration_seconds"
},
{
"Value": "0",
"Key": "slow_start.duration_seconds"
}
]
}
I would like to fetch deregistration_delay.timeout_seconds from the output
I tried which works for this case when deregistration_delay.timeout_seconds appears on the second position.
aws elbv2 describe-target-group-attributes \
--target-group-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-targets/73e2d6bc24d8a067
| jq -r '.Attributes[1].Value'
but for some target groups the deregistration_delay.timeout_seconds is placed at a different number.
How can I use jq to fetch deregistration_delay.timeout_seconds
You can actually use JMESPATH in the AWS CLI without needing to use jq:
aws elbv2 describe-target-group-attributes \
--target-group-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-targets/73e2d6bc24d8a067 \
--query "Attributes[?Key=='deregistration_delay.timeout_seconds']|[0].Value" \
--output text
JMESPATH was created by James Saryerwinnie, one of the authors of the AWS CLI. The tutorial is well worth reading.

Retrieve only one parameter value from aws ssm get-parameter command?

How can I print only the value of Value attribute from the below output of the following command
aws ssm get-parameter --name "/test/ip/cidr" --profile test
{
"Parameter": {
"Name": "/test/ip/cidr",
"Type": "String",
"Value": "172.18.0.0/20",
"Version": 1,
"LastModifiedDate": 1585251360.78,
"ARN": "arn:aws:ssm:us-east-1:123233:parameter/test/ip/cidr",
"DataType": "text"
}
}
Tried running the below command but prints like [{"Value": "172.18.0.0/20"}] but just want to see only 172.18.0.0/20
aws ssm get-parameters --names "/test/ip/cidr" --query "Parameters[*].{Value:Value}" --profile test
[
{
"Value": "172.18.0.0/20"
}
]
You can add --output text and modify your --query:
aws ssm get-parameter --name "/test/ip/cidr" --profile test \
--query "Parameter.Value" --output text

pipe output from aws cli as input to another aws cli command

Hi I would like to pipe an instance output to start/stop ec2 instances. Here is the beginning of the code:
aws ec2 describe-instances \
--query "Reservations[*].Instances[*].{Instance:InstanceId,Subnet:SubnetId}" \
--filters "Name=tag-value,Values=<INSTANCE NAME TAG>" \
--output text | \
How do I pipe this output to AWS ec2 start-instances command in Windows?
Output format can be JSON,YAML, TEXT or Table.It depends on your requirements.
Sample command for JSON output:
$ aws iam list-users --output json
Sample output:
{
"Users": [
{
"Path": "/",
"UserName": "Admin",
"UserId": "AIDA1111111111EXAMPLE",
"Arn": "arn:aws:iam::123456789012:user/Admin",
"CreateDate": "2014-10-16T16:03:09+00:00",
"PasswordLastUsed": "2016-06-03T18:37:29+00:00"
},
{
"Path": "/backup/",
"UserName": "backup-user",
"UserId": "AIDA2222222222EXAMPLE",
"Arn": "arn:aws:iam::123456789012:user/backup/backup-user",
"CreateDate": "2019-09-17T19:30:40+00:00"
},
{
"Path": "/",
"UserName": "cli-user",
"UserId": "AIDA3333333333EXAMPLE",
"Arn": "arn:aws:iam::123456789012:user/cli-user",
"CreateDate": "2019-09-17T19:11:39+00:00"
}
]
}
Now if you want to use this output for input of another command, one easy way is to read the json file, extract the value and use that as input to other command.
Please read https://www.business.com/articles/using-powershell-with-json-data/ for some details.
I found a PowerShell solution which suits my needs better:
$InstanceId = aws ec2 describe-instances --query "Reservations[*].Instances[*].{Instance:InstanceId}" --filters "Name=tag-value,Values=<INSTANCE NAME TAG>" --output text aws ec2 start-instances --instance-ids $InstanceId

aws cloudformation describe-stack-resources query by LogicalResourceId

I am attempt to retrieve a stack PhysicalResourceId using the aws command line.
$ aws cloudformation describe-stack-resources \
--stack-name test-app-prometheus \
--query 'StackResources[?LogicalResourceId=="PrometheusAutoScalingGroup"]'
I was expecting this to return:
[
{
"ResourceStatus": "...",
"LogicalResourceId": "...",
"StackName": "test-app-prometheus",
"StackId": "...",
"PhysicalResourceId": "test-app-prometheus-PrometheusAutoScalingGroup-...",
"ResourceType": "AWS::AutoScaling::AutoScalingGroup",
"Timestamp": "2016-11-08T15:17:23.567Z"
}
]
However instead it is returning an empty array.
[]
Running the command without the query and I can see the resource. Running the command:
$ aws cloudformation describe-stack-resources \
--stack-name test-app-prometheus \
--query 'StackResources[*].LogicalResourceId' \
| grep PrometheusAutoScalingGroup
"PrometheusAutoScalingGroup",
suggests that the resource exists.
How about:
$ aws cloudformation describe-stack-resources \
--stack-name test-app-prometheus \
--logical-resource-id PrometheusAutoScalingGroup
CloudFormation and the New AWS CLI

AWS get all private ip address in given vpc

I want to run an ansible role, for all the ip address in the vpc which are running.
How to get all the ip address of running instance in given vpc
Things: I have tired:
aws ec2 describe-instances --filters "Name=vpc-id,
Values="vpc-******"" --query
"Reservations[].Instances[].PrivateIpAddresses[*]" --output text
This is returning null
The name of the parameter is PrivateIpAddress not PrivateIpAddresses as you can see from Json object
[
[
{
"Monitoring": {
"State": "disabled"
},
"PublicDnsName": "xxxx",
"RootDeviceType": "ebs",
"State": {
"Code": 16,
"Name": "running"
},
"EbsOptimized": false,
"LaunchTime": "xxx",
"PublicIpAddress": "xxx",
"PrivateIpAddress": "xxxxx",
"ProductCodes": [
....
so if you run your command as
aws ec2 describe-instances --filters "Name=vpc-id, Values="vpc-cda7c6a8"" --query "Reservations[*].Instances[*].PrivateIpAddress" --output text
you will have your expected result
it's PrivateIPAddress, not Addresses
aws ec2 describe-instances --instance-ids --query Reservations[].Instances[].PrivateIpAddress
Hope this helps