【问题标题】:Python Boto3: set session, create an EC2 instance and run command using csdshellPython Boto3:设置会话、创建 EC2 实例并使用 csdshell 运行命令
【发布时间】:2016-08-28 11:17:14
【问题描述】:

我需要使用 boto3 编写一个 python 脚本,它执行以下操作,

  • 为我的会话设置 aws 访问权限和密钥
  • 然后创建一个 ec2 实例(使用 ami 映像)
  • 在新创建的 ec2 实例中执行命令

【问题讨论】:

标签: python amazon-web-services amazon-ec2 ec2-ami boto3


【解决方案1】:

这并不难,您所问的大部分内容都包含在 boto3 文档中。

用于在运行 ubuntu 14.04 的 us-east-1a 上创建新的 t2.micro。你应该可以这样做:

# latest ubuntu ami
ami_id = 'ami-5189a661'

# define userdata to be run at instance launch
userdata = """#cloud-config

runcmd:
 - touch /home/ubuntu/heythere.txt
"""

conn_args = {
    'aws_access_key_id': 'YOURKEY',
    'aws_secret_access_key': 'YOUSECACCESSKEY',
    'region_name': 'us-east-1'
}

ec2_res = boto3.resource('ec2', **conn_args)

new_instance = ec2_res.create_instances(
    ImageId=ami_id,
    MinCount=1,
    MaxCount=1,
    UserData=userdata,
    InstanceType='t2.micro'
    )

print new_instance.id

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-09
    • 2015-12-28
    • 2019-11-21
    • 2020-07-20
    • 1970-01-01
    • 1970-01-01
    • 2017-07-27
    • 1970-01-01
    相关资源
    最近更新 更多