Boto3->修改EC2的实例以具有多个安全组 [英] Boto3 --> Modifying EC2's instance to have multiple Security Groups

查看:59
本文介绍了Boto3->修改EC2的实例以具有多个安全组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个要附加到EC2实例的安全组.我尝试了以下操作,但失败了:

  sg_1 ='sg-something'sg_2 ='sg-else'响应= instance.modify_attribute(Groups = sg_1,sg_2) 

是这样的:

 响应= instance.modify_attribute(Groups = [sg_1,sg_2]) 

是这样的:

sg_1,sg_2中sg的

 :响应= instance.modify_attribute(Groups = [sg_1,sg_2]) 

似乎一次只能接受一个sg,但是当我通过第二个sg时,它将覆盖上一个.

有什么想法吗?谢谢

解决方案

这对我来说很好:

  import boto3client = boto3('ec2')响应= client.modify_instance_attribute(InstanceId ='i-1234',Groups = ['sg-1111','sg-2222']) 

或使用资源版本:

  import boto3ec2 = boto3.resource('ec2')实例= ec2.Instance('i-1234')instance.modify_attribute(Groups = ['sg-1111','sg-2222']) 

I have a couple of Security Groups I'd like to attach to an EC2 instance. I tried the following but failed:

sg_1 = 'sg-something'
sg_2 = 'sg-else'
response = instance.modify_attribute(Groups=sg_1, sg_2)

And something like this:

response = instance.modify_attribute(Groups=[sg_1, sg_2])

And something like this:

for sg in sg_1, sg_2:
    response = instance.modify_attribute(Groups=[sg_1, sg_2])

It seems like it can only accept one sg at a time but when I pass the second one it overwrites the previous one.

Any ideas? Thanks

解决方案

This worked fine for me:

import boto3

client=boto3('ec2')

response = client.modify_instance_attribute(InstanceId='i-1234',Groups=['sg-1111','sg-2222'])

Or using the resource version:

import boto3

ec2 = boto3.resource('ec2')

instance = ec2.Instance('i-1234')
instance.modify_attribute(Groups=['sg-1111','sg-2222'])

这篇关于Boto3->修改EC2的实例以具有多个安全组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆