【问题标题】:How to use aws cli in python or node.js programming language?如何在 python 或 node.js 编程语言中使用 aws cli?
【发布时间】:2016-06-08 08:32:05
【问题描述】:
  1. 如何在 python 或 node.js 编程中使用 awscli 命令?
  2. 我的最终目标是将安全组附加到 AWS 中正​​在运行的实例。

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

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

【问题讨论】:

  • 有一个非常流行的库可以在 python 中实现你想要的,叫做 Boto:pypi.python.org/pypi/boto
  • 使用适用于 Python(也称为 Boto3)和 NodeJS 的 AWS 开发工具包,而不是调用 CLI。

标签: python node.js amazon-web-services aws-sdk aws-cli


【解决方案1】:

使用 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'])

【讨论】:

    【解决方案2】:

    为什么要从 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

    【讨论】:

      猜你喜欢
      • 2012-04-21
      • 2015-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-17
      • 2022-11-09
      • 1970-01-01
      • 2019-07-27
      相关资源
      最近更新 更多