2016-05-03 25 views
5

我嘗試使用boto3更新安全組規則,將規則添加到安全組a(sg_a)以允許安全組b(sg_b )來訪問端口8443如何使用boto3 authorize_security_group_ingress在非默認VPC中的兩個安全組之間添加規則

我想使用EC2的客戶有以下

ec2.authorize_security_group_ingress(
     GroupId=sg_a, 
     SourceSecurityGroupName=sg_b, 
     IpProtocol='tcp', 
     FromPort=service_port, 
     ToPort=service_port 
    ) 

來實現這一點,但我得到這個錯誤:

botocore.exceptions.ClientError: An error occurred (VPCIdNotSpecified) when calling the AuthorizeSecurityGroupIngress operation: No default VPC for this user. 

如何使用authori ze_security_group_igress爲非默認VPC?

+0

[該文檔可能有助於](http://boto3.readthedocs.io/en/latest/reference/services/ec2.html#EC2.Client.authorize_security_group_ingress) – hangtwenty

+1

正確的語法是: ' ec2.authorize_security_group_ingress( 的GroupId = sg_a, IpPermissions = [{ 'IpProtocol': 'TCP', 'FromPort':from_port, 'ToPort':to_port, 'UserIdGroupPairs' :[{GroupId':sg_b }] }], ) ' – blindstack

+0

請編寫您的解決方案並將其標記爲已解決。 – mootmoot

回答

3

正確的語法是:

ec2.authorize_security_group_ingress( 
    GroupId=sg_a, 
    IpPermissions=[ 
     {'IpProtocol': 'tcp', 
     'FromPort': from_port, 
     'ToPort': to_port, 
     'UserIdGroupPairs': [{ 'GroupId': sg_b }] } 
    ], 
) 
相關問題