使用带有安全组的 boto3 启动 ec2 实例

使用带有安全组的 boto3 启动 ec2 实例
ecx = boto3.resource('ec2' , region_name='eu-west-1')
mysg = ecx.create_security_group(GroupName='testgroup11',Description='testme')
mysg.authorize_ingress(IpProtocol="tcp",CidrIp="0.0.0.0/0",FromPort=22,ToPort=22)


# create a new EC2 instance
instances = ecx.create_instances(
     ImageId='ami-07c7a309b70bc57f8',
     MinCount=1,
     MaxCount=1,
     InstanceType='p3.2xlarge',
     SecurityGroups=mysg,
     UserData=open('script.sh').read(),
     KeyName='ec2-keypair'
 )
print(instance[0].id)
Invalid type for parameter SecurityGroups, value: ec2.SecurityGroup(id='sg-089aaxxx'), type: <class 'boto3.resources.factory.ec2.SecurityGroup'>, valid types: <type 'list'>, <type 'tuple'>

相关内容