Ableman
Ableman

Reputation: 49

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?

Upvotes: 0

Views: 1688

Answers (2)

Ableman
Ableman

Reputation: 49

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

Upvotes: 0

subhadeep dutta gupta
subhadeep dutta gupta

Reputation: 213

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.

Upvotes: 1

Related Questions