Python 使用boto3创建自动缩放组amazon ec2时出现关键错误
我有一个AmazonEC2实例,我正在尝试创建一个自动缩放组,并在其上使用cloudwatch度量等Python 使用boto3创建自动缩放组amazon ec2时出现关键错误,python,amazon-web-services,amazon-ec2,boto3,autoscaling,Python,Amazon Web Services,Amazon Ec2,Boto3,Autoscaling,我有一个AmazonEC2实例,我正在尝试创建一个自动缩放组,并在其上使用cloudwatch度量等 import boto3 from boto3 import Session session = Session() credentials = session.get_credentials() current_credentials = credentials.get_frozen_credentials() if current_credentials.access_key is N
import boto3
from boto3 import Session
session = Session()
credentials = session.get_credentials()
current_credentials = credentials.get_frozen_credentials()
if current_credentials.access_key is None:
print("Access Key missing, use `aws configure` to setup")
exit()
if current_credentials.secret_key is None:
print("Secret Key missing, use `aws configure` to setup")
exit()
# VPC design for multi az deployments
globalVars = {}
globalVars['REGION_NAME'] = "ap-south-1"
globalVars['AZ1'] = "ap-south-1a"
globalVars['AZ2'] = "ap-south-1b"
globalVars['CIDRange'] = "10.240.0.0/23"
globalVars['az1_pvtsubnet_CIDRange'] = "10.240.0.0/25"
globalVars['az1_pubsubnet_CIDRange'] = "10.240.0.128/26"
globalVars['az1_sparesubnet_CIDRange'] = "10.240.0.192/26"
globalVars['az2_pvtsubnet_CIDRange'] = "10.240.1.0/25"
globalVars['az2_pubsubnet_CIDRange'] = "10.240.1.128/26"
globalVars['az2_sparesubnet_CIDRange'] = "10.240.1.192/26"
globalVars['Project'] = { 'Key': 'Name', 'Value': 'test1'}
globalVars['tags'] = [{'Key': 'Owner', 'Value': 'test1'},
{'Key': 'Environment', 'Value': 'Test'},
{'Key': 'Department', 'Value': 'TestD'}]
# EC2 Parameters
globalVars['EC2-Amazon-AMI-ID'] = "ami-00b6a8a2bd28daf19"
globalVars['EC2-InstanceType'] = "t2.micro"
globalVars['EC2-KeyName'] = "datastructutrekey.pem"
# AutoScaling Parameters
globalVars['ASG-LaunchConfigName'] = "ASG-Demo-LaunchConfig"
globalVars['ASG-AutoScalingGroupName'] = "ASG-Demo-AutoScalingGrp"
# Creating a VPC, Subnet, and Gateway
ec2 = boto3.resource('ec2', region_name=globalVars['REGION_NAME'])
ec2Client = boto3.client('ec2', region_name=globalVars['REGION_NAME'])
vpc = ec2.create_vpc(CidrBlock=globalVars['CIDRange'])
asgClient = boto3.client('autoscaling', region_name=globalVars['REGION_NAME'])
rds = boto3.client('rds', region_name=globalVars['REGION_NAME'])
# AZ1 Subnets
az1_pvtsubnet = vpc.create_subnet(CidrBlock=globalVars['az1_pvtsubnet_CIDRange'], AvailabilityZone=globalVars['AZ1'])
az1_pubsubnet = vpc.create_subnet(CidrBlock=globalVars['az1_pubsubnet_CIDRange'], AvailabilityZone=globalVars['AZ1'])
az1_sparesubnet = vpc.create_subnet(CidrBlock=globalVars['az1_sparesubnet_CIDRange'], AvailabilityZone=globalVars['AZ1'])
# AZ2 Subnet
az2_pvtsubnet = vpc.create_subnet(CidrBlock=globalVars['az2_pvtsubnet_CIDRange'], AvailabilityZone=globalVars['AZ2'])
az2_pubsubnet = vpc.create_subnet(CidrBlock=globalVars['az2_pubsubnet_CIDRange'], AvailabilityZone=globalVars['AZ2'])
az2_sparesubnet = vpc.create_subnet(CidrBlock=globalVars['az2_sparesubnet_CIDRange'], AvailabilityZone=globalVars['AZ2'])
# Enable DNS Hostnames in the VPC
vpc.modify_attribute(EnableDnsSupport={'Value': True})
vpc.modify_attribute(EnableDnsHostnames={'Value': True})
# Create the Internet Gatway & Attach to the VPC
intGateway = ec2.create_internet_gateway()
intGateway.attach_to_vpc(VpcId=vpc.id)
# Create another route table for Public & Private traffic
routeTable = ec2.create_route_table(VpcId=vpc.id)
rtbAssn=[]
rtbAssn.append(routeTable.associate_with_subnet(SubnetId=az1_pubsubnet.id))
rtbAssn.append(routeTable.associate_with_subnet(SubnetId=az1_pvtsubnet.id))
rtbAssn.append(routeTable.associate_with_subnet(SubnetId=az2_pubsubnet.id))
rtbAssn.append(routeTable.associate_with_subnet(SubnetId=az2_pvtsubnet.id))
# Create a route for internet traffic to flow out
intRoute = ec2Client.create_route(RouteTableId=routeTable.id, DestinationCidrBlock='0.0.0.0/0', GatewayId=intGateway.id)
# Tag the resources
vpc.create_tags (Tags=globalVars['tags'])
az1_pvtsubnet.create_tags (Tags=globalVars['tags'])
az1_pubsubnet.create_tags (Tags=globalVars['tags'])
az1_sparesubnet.create_tags(Tags=globalVars['tags'])
az2_pvtsubnet.create_tags (Tags=globalVars['tags'])
az2_pubsubnet.create_tags (Tags=globalVars['tags'])
az2_sparesubnet.create_tags(Tags=globalVars['tags'])
intGateway.create_tags (Tags=globalVars['tags'])
routeTable.create_tags (Tags=globalVars['tags'])
vpc.create_tags (Tags=[{'Key': 'Name', 'Value': globalVars['Project']['Value'] + '-vpc'}])
az1_pvtsubnet.create_tags (Tags=[{'Key': 'Name', 'Value': globalVars['Project']['Value'] + '-az1-private-subnet'}])
az1_pubsubnet.create_tags (Tags=[{'Key': 'Name', 'Value': globalVars['Project']['Value'] + '-az1-public-subnet'}])
az1_sparesubnet.create_tags(Tags=[{'Key': 'Name', 'Value': globalVars['Project']['Value'] + '-az1-spare-subnet'}])
az2_pvtsubnet.create_tags (Tags=[{'Key': 'Name', 'Value': globalVars['Project']['Value'] + '-az2-private-subnet'}])
az2_pubsubnet.create_tags (Tags=[{'Key': 'Name', 'Value': globalVars['Project']['Value'] + '-az2-public-subnet'}])
az2_sparesubnet.create_tags(Tags=[{'Key': 'Name', 'Value': globalVars['Project']['Value'] + '-az2-spare-subnet'}])
intGateway.create_tags (Tags=[{'Key': 'Name', 'Value': globalVars['Project']['Value'] + '-igw'}])
routeTable.create_tags (Tags=[{'Key': 'Name', 'Value': globalVars['Project']['Value'] + '-rtb'}])
# Let create the Public & Private Security Groups
elbSecGrp = ec2.create_security_group(DryRun=False,
GroupName='elbSecGrp',
Description='ElasticLoadBalancer_Security_Group',
VpcId=vpc.id
)
pubSecGrp = ec2.create_security_group(DryRun=False,
GroupName='pubSecGrp',
Description='Public_Security_Group',
VpcId=vpc.id
)
pvtSecGrp = ec2.create_security_group(DryRun=False,
GroupName='pvtSecGrp',
Description='Private_Security_Group',
VpcId=vpc.id
)
elbSecGrp.create_tags(Tags=globalVars['tags'])
pubSecGrp.create_tags(Tags=globalVars['tags'])
pvtSecGrp.create_tags(Tags=globalVars['tags'])
elbSecGrp.create_tags(Tags=[{'Key': 'Name', 'Value': globalVars['Project']['Value'] + '-elb-security-group'}])
pubSecGrp.create_tags(Tags=[{'Key': 'Name', 'Value': globalVars['Project']['Value'] + '-public-security-group'}])
pvtSecGrp.create_tags(Tags=[{'Key': 'Name', 'Value': globalVars['Project']['Value'] + '-private-security-group'}])
# Add a rule that allows inbound SSH, HTTP, HTTPS traffic ( from any source )
ec2Client.authorize_security_group_ingress(GroupId=elbSecGrp.id,
IpProtocol='tcp',
FromPort=80,
ToPort=80,
CidrIp='0.0.0.0/0'
)
# Allow Public Security Group to receive traffic from ELB Security group
ec2Client.authorize_security_group_ingress(GroupId=pubSecGrp.id,
IpPermissions=[{'IpProtocol': 'tcp',
'FromPort': 80,
'ToPort': 80,
'UserIdGroupPairs': [{'GroupId': elbSecGrp.id}]
}]
)
# Allow Private Security Group to receive traffic from Application Security group
ec2Client.authorize_security_group_ingress(GroupId=pvtSecGrp.id,
IpPermissions=[{'IpProtocol': 'tcp',
'FromPort': 3306,
'ToPort': 3306,
'UserIdGroupPairs': [{'GroupId': pubSecGrp.id}]
}]
)
ec2Client.authorize_security_group_ingress(GroupId=pubSecGrp.id,
IpProtocol='tcp',
FromPort=80,
ToPort=80,
CidrIp='0.0.0.0/0'
)
ec2Client.authorize_security_group_ingress(GroupId=pubSecGrp.id,
IpProtocol='tcp',
FromPort=443,
ToPort=443,
CidrIp='0.0.0.0/0'
)
ec2Client.authorize_security_group_ingress(GroupId=pubSecGrp.id,
IpProtocol='tcp',
FromPort=22,
ToPort=22,
CidrIp='0.0.0.0/0'
)
# The user defined code to install WebServer & Configure them
userDataCode = """
#!/bin/bash
set -e -x
# Setting up the HTTP server
yum install -y httpd
service httpd start
chkconfig httpd on
groupadd www
usermod -a -G www ec2-user
cd /var/www/
# Set the permissions
chown -R root:www /var/www
chmod 2775 /var/www
find /var/www -type d -exec chmod 2775 {} +
find /var/www -type f -exec chmod 0664 {} +
# SE Linux permissive
# setsebool -P httpd_can_network_connect=1
service httpd restart
echo "<?php phpinfo(); ?>" > /var/www/html/phptestinfo.php
"""
# Create the Public Instance
##### **DeviceIndex**:The network interface's position in the attachment order. For example, the first attached network interface has a DeviceIndex of 0
instanceLst = ec2.create_instances(ImageId=globalVars['EC2-Amazon-AMI-ID'],
MinCount=1,
MaxCount=2,
KeyName="datastructutrekey.pem",
UserData=userDataCode,
InstanceType=globalVars['EC2-InstanceType'],
NetworkInterfaces=[
{
'SubnetId': az1_pubsubnet.id,
'Groups': [pubSecGrp.id],
'DeviceIndex': 0,
'DeleteOnTermination': True,
'AssociatePublicIpAddress': True,
}
]
)
# Create the Launch Configuration
# InstanceId = 'string'
asgLaunchConfig = asgClient.create_launch_configuration(
LaunchConfigurationName=globalVars['ASG-LaunchConfigName'],
ImageId=globalVars['EC2-Amazon-AMI-ID'],
KeyName=globalVars['EC2-KeyName'],
SecurityGroups=[pubSecGrp.id],
UserData=userDataCode,
InstanceType=globalVars['EC2-InstanceType'],
InstanceMonitoring={'Enabled': False },
EbsOptimized=False,
AssociatePublicIpAddress=False
)
# create Auto-Scaling Group
ASGSubnets = az1_pubsubnet.id + "," +az2_pubsubnet.id
asGroup=asgClient.create_auto_scaling_group(
AutoScalingGroupName=globalVars['ASG-AutoScalingGroupName'],
LaunchConfigurationName=globalVars['ASG-LaunchConfigName'],
MinSize=1,
MaxSize=3,
DesiredCapacity=2,
DefaultCooldown=120,
HealthCheckType='EC2',
HealthCheckGracePeriod=60,
Tags=globalVars['tags'],
VPCZoneIdentifier=ASGSubnets
)
asgClient.create_or_update_tags(
Tags=[
{
'ResourceId': globalVars['ASG-AutoScalingGroupName'],
'ResourceType': 'auto-scaling-group',
'Key': 'Name',
'Value': globalVars['Project']['Value'] + '-ASG-Group',
'PropagateAtLaunch': True
},
]
)
###### Print to Screen ########
print("VPC ID : {0}".format(vpc.id))
print("AZ1 Public Subnet ID : {0}".format(az1_pubsubnet.id))
print("AZ1 Private Subnet ID : {0}".format(az1_pvtsubnet.id))
print("AZ1 Spare Subnet ID : {0}".format(az1_sparesubnet.id))
print("Internet Gateway ID : {0}".format(intGateway.id))
print("Route Table ID : {0}".format(routeTable.id))
print("Public Security Group ID : {0}".format(pubSecGrp.id))
print("Private Security Group ID : {0}".format(pvtSecGrp.id))
print("EC2 Key Pair : {0}".format(globalVars['EC2-KeyName']))
print("EC2 PublicIP : {0}".format(globalVars['EC2-KeyName']))
print("RDS Endpoint : {0}".format(globalVars['Endpoint']))
###### Print to Screen ########
"""
Function to clean up all the resources
"""
def cleanAll(resourcesDict=None):
# Delete the instances
ids = []
for i in instanceLst:
ids.append(i.id)
ec2.instances.filter(InstanceIds=ids).terminate()
# Wait for the instance to be terminated
waiter = ec2Client.get_waiter('instance_terminated')
waiter.wait(InstanceIds=[ids])
ec2Client.delete_key_pair(KeyName=globalVars['EC2-KeyName'])
# Delete Routes & Routing Table
for assn in rtbAssn:
ec2Client.disassociate_route_table(AssociationId=assn.id)
routeTable.delete()
# Delete Subnets
az1_pvtsubnet.delete()
az1_pubsubnet.delete()
az1_sparesubnet.delete()
# Detach & Delete internet Gateway
ec2Client.detach_internet_gateway(InternetGatewayId=intGateway.id, VpcId=vpc.id)
intGateway.delete()
# Delete Security Groups
pubSecGrp.delete()
pvtSecGrp.delete()
vpc.delete()
导入boto3
从boto3导入会话
会话=会话()
凭据=会话。获取\u凭据()
当前\u凭据=凭据。获取\u冻结的\u凭据()
如果当前\u credentials.access\u密钥为“无”:
打印(“缺少访问密钥,使用`aws configure`进行设置”)
退出()
如果当前\u credentials.secret\u密钥为无:
打印(“缺少密钥,请使用`aws configure`进行设置”)
退出()
#用于多az部署的VPC设计
globalVars={}
globalVars[“地区名称”]=“ap-south-1”
全球卫星['AZ1']=“ap-south-1a”
全球卫星['AZ2']=“ap-south-1b”
globalVars['CIDRange']=“10.240.0.0/23”
globalVars['az1_pvtsubnet_CIDRange']=“10.240.0.0/25”
globalVars['az1_pubsubnet_CIDRange']=“10.240.0.128/26”
globalVars['az1_sparesubnet_CIDRange']=“10.240.0.192/26”
globalVars['az2_pvtsubnet_CIDRange']=“10.240.1.0/25”
globalVars['az2_pubsubnet_CIDRange']=“10.240.1.128/26”
globalVars['az2_sparesubnet_CIDRange']=“10.240.1.192/26”
globalVars['Project']={'Key':'Name','Value':'test1'}
globalVars['tags']=[{'Key':'Owner','Value':'test1'},
{'Key':'Environment','Value':'Test'},
{'Key':'Department','Value':'TestD'}]
#EC2参数
globalVars['EC2-Amazon-AMI-ID']=“AMI-00b6a8a2bd28daf19”
globalVars['EC2-InstanceType']=“t2.micro”
globalVars['EC2-KeyName']=“datastructuretrekey.pem”
#自动缩放参数
globalVars['ASG-LaunchConfigName']=“ASG演示启动配置”
globalVars['ASG-AutoScalingGroupName']=“ASG演示自动校准组”
#创建专有网络、子网和网关
ec2=boto3.resource('ec2',region\u name=globalVars['region\u name']))
ec2Client=boto3.client('ec2',region\u name=globalVars['region\u name']))
vpc=ec2.创建vpc(CidrBlock=globalVars['CIDRange'])
asgClient=boto3.client('autoscaling',region\u name=globalVars['region\u name']))
rds=boto3.client('rds',region\u name=globalVars['region\u name']))
#AZ1子网
az1\u pvtsubnet=vpc.创建子网(CidrBlock=globalVars['az1\u pvtsubnet\u CIDRange'],AvailabilityZone=globalVars['az1'])
az1\u pubsubnet=vpc.创建\u子网(CidrBlock=globalVars['az1\u pubsubnet\u CIDRange'],AvailabilityZone=globalVars['az1'])
az1\u sparesubnet=vpc.创建\u子网(CidrBlock=globalVars['az1\u sparesubnet\u CIDRange'],AvailabilityZone=globalVars['az1']))
#AZ2子网
az2\u pvtsubnet=vpc.创建子网(CidrBlock=globalVars['az2\u pvtsubnet\u CIDRange'],AvailabilityZone=globalVars['az2'])
az2\u pubsubnet=vpc.创建\u子网(CidrBlock=globalVars['az2\u pubsubnet\u CIDRange'],AvailabilityZone=globalVars['az2'])
az2\u sparesubnet=vpc.创建\u子网(CidrBlock=globalVars['az2\u sparesubnet\u CIDRange'],AvailabilityZone=globalVars['az2']))
#在VPC中启用DNS主机名
modify_属性(enablednsupport={'Value':True})
modify_属性(EnableDnsHostnames={'Value':True})
#创建Internet网关并连接到VPC
intGateway=ec2.创建互联网网关()
intGateway.attach_to_vpc(VpcId=vpc.id)
#为公共和私人交通创建另一个路由表
routeTable=ec2.创建路由表(VpcId=vpc.id)
rtbAssn=[]
rtbAssn.append(routeTable.associate_与_子网(SubnetId=az1_pubsubnet.id))
rtbAssn.append(routeTable.associate_与_子网(SubnetId=az1_pvtsubnet.id))
rtbAssn.append(routeTable.associate_与_子网(SubnetId=az2_pubsubnet.id))
rtbAssn.append(routeTable.associate_与_子网(SubnetId=az2_pvtsubnet.id))
#创建internet流量流出的路由
intRoute=ec2Client.create_路由(RouteTableId=routeTable.id,DestinationCidBlock='0.0.0/0',GatewayId=intGateway.id)
#标记资源
vpc.create_标签(标签=globalVars['tags'])
az1_pvtsubnet.create_标记(标记=globalVars['tags'])
az1_pubsubnet.create_标记(标记=globalVars['tags'])
az1_sparesubnet.create_标记(标记=globalVars['tags'])
az2_pvtsubnet.create_标记(标记=globalVars['tags'])
az2_pubsubnet.create_标记(标记=globalVars['tags'])
az2_sparesubnet.创建_标记(标记=globalVars['tags'])
intGateway.create_标记(标记=globalVars['tags'])
routeTable.create_标记(标记=globalVars['tags'])
创建_标记(标记=[{'Key':'Name','Value':globalVars['Project']['Value']+'-vpc'}])
az1_pvtsubnet.create_标记(标记=[{'Key':'Name','Value':globalVars['Project']['Value']+'-az1私有子网'}])
az1_pubsubnet.create_标记(标记=[{'Key':'Name','Value':globalVars['Project']['Value']+'-az1公共子网'}])
az1_备用子网。创建_标记(标记=[{'Key':'Name','Value':globalVars['Project']['Value']+'-az1备用子网'}])
az2_pvtsubnet.create_标记(标记=[{'Key':'Name','Value':globalVars['Project']['Value']+'-az2私有子网'}])
az2_pubsubnet.create_标记(标记=[{'Key':'Name','Value':globalVars['Project']['Value']+'-az2 public subnet'}])
az2_备用子网。创建_标记(标记=[{'Key':'Name','Value':globalVars['Project']['Value']+'-az2备用子网'}])
intGateway.create_标记(标记=[{'Key':'Name','Value':globalVars['Project']['Value']+'-igw'}])
routeTable.create_标记(标记=[{'Key':'Name','Value':globalVars['Project']['Value']+'-rtb'}])
#让我们创建公共和私人安全组
elbSecGrp=ec2.创建安全组(DryRun=False,
GroupName='elbSecGrp',
Description='ElasticLoadBalancer\u Security\u Group',
VpcId=vpc.id
)
pubSecGrp=ec2.创建安全组(DryRun=False,
GroupName='pubSecGrp',
Description='Public\u Security\u Group',
VpcId=vpc。
VPC ID : vpc-047839873c2b62b51
AZ1 Public Subnet ID : subnet-0c8db93f160c952b3
AZ1 Private Subnet ID : subnet-00e2068c36695cf47
AZ1 Spare Subnet ID : subnet-0521b503114f92f97
Internet Gateway ID : igw-020b3e284d591e977
Route Table ID : rtb-0e83e29fed23f6773
Public Security Group ID : sg-05bbc53b9cad2a6db
Private Security Group ID : sg-028d432258e825562
EC2 Key Pair : datastructutrekey.pem
EC2 PublicIP : datastructutrekey.pem
Traceback (most recent call last):
File "D:\programming \autoscaling.py", line 281, in <module>
print("RDS Endpoint : {0}".format(globalVars['Endpoint']))
KeyError: 'Endpoint'
globalVars = {
'REGION_NAME': 'ap-south-1',
'AZ1': 'ap-south-1a',
'AZ2': 'ap-south-1b',
# others here
}