1

I'm trying to execute aws cli command using Pyton's subprocess

windows cmd:

aws --profile some_profile --region some_region ec2 describe-instances --filters Name=tag:some_tag,Values=some_value --query "Reservations[*].Instances[*].{AvailabilityZone:Placement.AvailabilityZone,Status:State.Name,Name:Tags[?Key=='Name']|[0].Value}" --output=table

and that's how I try to do it:

profile = "some_profile"
region = "some_region"
ec2_filters = "Name=tag:some_tag,Values=some_value"
ec2_query = "Reservations[*].Instances[*].{AvailabilityZone:Placement.AvailabilityZone,Status:State.Name,Name:Tags[?Key=='Name']|[0].Value}"
ec2_output_type = "table"

proc = subprocess.Popen(["aws", "--profile", profile, "--region", region, "ec2", "describe-instances", "--filters", ec2_filters, "--query", ec2_query, "--output", ec2_output_type], stdout=subprocess.PIPE, shell=True)

This is the error message:

'[0].Value}' is not recognized as an internal or external command, operable program or batch file.

7
  • 1
    Try putting an extra set of quotes on your query, '"Reservations[*].Instances[*].{AvailabilityZone:Placement.AvailabilityZone,Status:State.Name,Name:Tags[?Key=='Name']|[0].Value}'" Commented Apr 2, 2021 at 22:27
  • I've tried that but it says: SyntaxError: invalid syntax. I also tried the other way around (single quotes inside), like "'query_here'" but then I have similar error: '[0].Value}'' is not recognized as an internal or external command, operable program or batch file. Commented Apr 2, 2021 at 22:44
  • Ohh, I didn't notice the single quotes in your command. Try triple quotes then. ec2_query = '''"Reservations[*].Instances[*].{AvailabilityZone:Placement.AvailabilityZone,Status:State.Name,Name:Tags[?Key=='Name']|[0].Value}"''' Commented Apr 2, 2021 at 22:51
  • This triple-quoted string will comment the value of my variable :) Commented Apr 2, 2021 at 23:12
  • I don't understand. Triple quoted strings aren't comments, they're just strings. You can use them as much as you want. Commented Apr 2, 2021 at 23:41

1 Answer 1

1

I don't have aws installed, so I created a mock batch file to spit back what it received. I did try my initial guesses and you're right, it often makes it difficult, but I figured it out. Sorry for not testing what I asked you to try.

Anyway, aws.bat contains a single line, echo %*, which prints back whatever the batch file receives as arguments, so we know it's working.

Then, I tried to use your command. I got the same error you got, so I modified it to:

.\aws.bat --profile some_profile --region some_region ec2 describe-instances --filters Name=tag:some_tag,Values=some_value --query '"Reservations[*].Instances[*].{AvailabilityZone:Placement.AvailabilityZone,Status:State.Name,Name:Tags[?Key=='Name']|[0].Value}"' --output=table

This outputted the command back, meaning it got executed correctly.

Then, I modified your code to make sure there's quotes over all the query. I used simple string concatenation to do that.

import subprocess
profile = "some_profile"
region = "some_region"
ec2_filters = "Name=tag:some_tag,Values=some_value"
ec2_query = (
    '"Reservations[*].Instances[*].{AvailabilityZone:Placement.AvailabilityZone,Status:State.Name,Name:Tags[?Key=='
    "'Name'" 
    ']|[0].Value}"'
)
ec2_output_type = "table"

proc = subprocess.Popen(["aws.bat", "--profile", profile, "--region", region, "ec2", "describe-instances", "--filters", ec2_filters, "--query", ec2_query, "--output", ec2_output_type])

This worked. Funnily, if I used triple quotes in an unorthodox manner, it worked as well.

ec2_query = ' '''"Reservations[*].Instances[*].{AvailabilityZone:Placement.AvailabilityZone,Status:State.Name,Name:Tags[?Key=='Name']|[0].Value}"' '''

Note the start, ' '''". I don't really know what's going on.

Anyway, the easier solution is to break up your string so the quotes don't get confusing.

Sign up to request clarification or add additional context in comments.

1 Comment

Great, it works! You are right, breaking that string is propably the best solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.