阅读背景:

aws 批量创建instance并绑定security groups

来源:互联网 
#coding:utf-8

import boto.ec2
import re
import json
import time

# 读取变量

## boto2
conn = boto.ec2.connect_to_region("us-west-2",aws_access_key_id='123',aws_secret_access_key='123/123')

def create_instance(info=[]):
# 创建seuciryt
security_name = "security-%s"%info[0]
security_ports = info[7].split("__")
web = conn.create_security_group(security_name, security_name)
for security_port in security_ports:
web.authorize('tcp', security_port, security_port, '0.0.0.0/0')
# 创建instance
instance_name = info[0]
instance_type = info[1]
instance_disksize = info[2]
instance_imageid = info[4]
#instance_keyname = info[5]
instance_keyname = "zzlyzq"
instance_count = int(info[6])
instance_vpcid = "vpc-6755d002"
instance_placements = [ info[3]+'b',info[3]+'c']
#instance_placements = [ 'subnet-c2a33ea7', 'subnet-d242948b']
instance_subnetids = [ 'subnet-6e3baa09','subnet-55e3ed0d' ]

## 准备磁盘
dev_sda1 = boto.ec2.blockdevicemapping.EBSBlockDeviceType()
dev_sda1.size = instance_disksize # size in Gigabytes
dev_sda1.volume_type = "gp2"
bdm = boto.ec2.blockdevicemapping.BlockDeviceMapping()
bdm['/dev/sda1'] = dev_sda1
# 绑定security到instance
for count in range(instance_count):
# 下面的两行比较有意思哦,最后面的报错就是instance_subnetid没有初始化造成的
instance_placement = instance_placements[count%2]
instance_subnetid = instance_subnetids[count%2]
reservation = conn.run_instances(image_id=instance_imageid,key_name=instance_keyname,instance_type=instance_type,placement=instance_placement,subnet_id=instance_subnetid,security_groups=[security_name],block_device_map = bdm )
# 添加tag为主机名
instance = reservation.instances[0]
status = instance.update()
while status == 'pending':
time.sleep(10)
status = instance.update()
if status == 'running':
instance.add_tag("Name", instance_name)
else:
print('Instance status: ' + status)
return None

def main():
with open("./instances.csv") as f:
for line in f:
#print line
info = []
info = line.replace("\r","").replace("\n","").split(",")
print info
create_instance(info)
#break


main()
#create_security()
a,c4.xlarge,150,us-west-2,ami-0dde2e6d,abc,4,22
b,t2.medium,50,us-west-2,ami-0dde2e6d,abc,1,22__873__23000__8090__22122
c,t2.xlarge,200,us-west-2,ami-0dde2e6d,abc,1,22
d,t2.medium,50,us-west-2,ami-0dde2e6d,abc,1,22__6379__26379

操作视频

https://ojv12llai.bkt.clouddn.com/uploads/20170315144756.mp4

遇到的问题

  • 下面的问题,稍后也解决了, 问题处在变量没有值的低级错误,哈哈。
Traceback (most recent call last):
File "/home/ops/opsfab/3rd/mylab/mm/runinstances.py", line 90, in <module>
main()
File "/home/ops/opsfab/3rd/mylab/mm/runinstances.py", line 86, in main
create_instance(info)
File "/home/ops/opsfab/3rd/mylab/mm/runinstances.py", line 66, in create_instance
reservation = conn.run_instances(image_id=instance_imageid,key_name=instance_keyname,instance_type=instance_type,security_groups=[security_name],placement=instance_placement,subnet_id=instance_subnetid,block_device_map = bdm )
File "/usr/local/lib/python2.7/dist-packages/boto/ec2/connection.py", line 977, in run_instances
verb='POST')
File "/usr/local/lib/python2.7/dist-packages/boto/connection.py", line 1208, in get_object
raise self.ResponseError(response.status, response.reason, body)
boto.exception.EC2ResponseError: EC2ResponseError: 400 Bad Request
<?xml version="1.0" encoding="UTF-8"?>
<Response><Errors><Error><Code>InvalidParameterCombination</Code><Message>The parameter groupName cannot be used with the parameter subnet</Message></Error></Errors><RequestID>50fc53d8-e227-4f5b-98df-034e346a92cf</RequestID></Response>

分享到: