hpmistry19
hpmistry19

Reputation: 33

Running AWS cli command using Subprocess

I am just trying out to see if it's valid to use AWS cli commands using subprocess module. Running the below command gives the successful output

vpc = subprocess.run("aws ec2 describe-vpcs | jq -r '.Vpcs[].VpcId'", stdout=subprocess.PIPE, shell=True)

While running the with select statement in jq shown as below gives an error.

subnets = subprocess.run("aws ec2 describe-subnets | jq -r '.Subnets[] | select(.Tags[].Value == "az1") .SubnetId'", stdout=subprocess.PIPE, shell=True)

error as

File "awsinit.py", line 42
    subnets = subprocess.run("aws ec2 describe-subnets | jq -r '.Subnets[] | select(.Tags[].Value == "az1") .SubnetId'", stdout=subprocess.PIPE, shell=True)
                                                                                                      ^
SyntaxError: invalid syntax

Any guide on fixing this or any other suggestion on this?

Upvotes: 0

Views: 980

Answers (1)

ultimate_muscle
ultimate_muscle

Reputation: 11

Fixing the quotation marks will help you solve the problem

subnets = subprocess.run('''aws ec2 describe-subnets | jq -r '.Subnets[] | select(.Tags[].Value == "az1") .SubnetId' ''', stdout=subprocess.PIPE, shell=True)

Upvotes: 1

Related Questions