如何在 python 或 node.js 编程语言中使用 aws cli?

How to use aws cli in python or node.js programming language?

  1. 如何在 python 或 node.js 编程中使用 awscli 命令?
  2. 我的最终目标是将安全组附加到 AWS 中的 运行ning 实例。

    aws --region us-east-1 ec2 modify-instance-attribute --instance-id i-782cc9fb --groups sg-47fb243f
    

我想通过 python 编程代码或 nodejs 运行 这个命令。有什么办法吗?

为什么 shell 从 python 中进入命令行? AWS 命令​​行只是 python boto 库的前端。你可以直接做。

博托:http://boto.cloudhackers.com/en/latest/

无论如何...子流程模块,如下所示:

import subprocess
subprocess.call(["aws", "--region", "us-east-1", "ec2", "modify-instance-attribute", "--instance-id", "i-782cc9fb", "--groups", "sg-47fb243f"])

https://docs.python.org/2/library/subprocess.html

使用 Boto2 库非常容易做到。确保您的凭据在 ~/.aws/credentials 中设置正确或在 boto.connect_ec2() 中传递它们。在 Boto3 中简单得多。以下是使用 Boto2.

import boto

conn = boto.connect_ec2()
reservations = conn.get_all_instances(instance_ids=['i-782cc9fb'])
instance = reservations[0].instances[0]
instance.modify_attribute('groupSet', ['sg-47fb243f'])