【问题标题】:Boto3 Adding an Inbound Rule to a Security Group in a Non Default VPCBoto3 向非默认 VPC 中的安全组添加入站规则
【发布时间】:2021-03-31 14:18:48
【问题描述】:

我正在尝试向非默认 VPC 中的安全组添加入站规则。我正在使用此代码:

import boto3

ec2 = boto3.client('ec2')

def modify_sg_add_rules():
        response = ec2.authorize_security_group_ingress(
        IpPermissions=
        [
            { 
                'FromPort': 3306,
                'IpProtocol': 'tcp',
                'IpRanges': 
                    [
                        {
                        'CidrIp': '64.192.85.294/32',
                        'Description': 'My home IP',
                        },
                    ],
                'ToPort': 3306,
                'UserIdGroupPairs': 
                    [
                        {
                            'Description': 'My home IP',
                            'GroupId': 'sg-0123',
                            # 'GroupName': 'mysql-sg-0123',
                            'VpcId': 'vpc-0f93q3',
                        },
                    ]
            },
        ],
    )

但是,我收到以下错误:

botocore.exceptions.ClientError:调用AuthorizeSecurityGroupIngress操作时发生错误(MissingParameter):请求必须包含参数groupName或groupId

我试过了,包括组名,但还是不行。

【问题讨论】:

    标签: amazon-web-services boto3 aws-security-group


    【解决方案1】:

    使用安全组 ID 即可:

    data = client_ec2.authorize_security_group_ingress(
            GroupId='sg-01b8f7d6ae1022a20',
            IpPermissions=[
                {'IpProtocol': 'tcp',
                 'FromPort': 80,
                 'ToPort': 80,
                 'IpRanges': [{'CidrIp': '0.0.0.0/0'}]},
                {'IpProtocol': 'tcp',
                 'FromPort': 22,
                 'ToPort': 22,
                 'IpRanges': [{'CidrIp': '0.0.0.0/0'}]}
            ])
    print('Ingress Successfully Set %s' % data)
    

    输出:

    Ingress Successfully Set {'ResponseMetadata': {'RequestId': xxxx, 'HTTPStatusCode': 200, 'HTTPHeaders': ......}}
    

    应该可以通过编程方式获取安全组 ID - 按安全组名称过滤。

    【讨论】:

    • 你好恩乔:是的!有用!听从您的建议后,我不得不稍微调整一下代码。
    猜你喜欢
    • 2015-04-28
    • 1970-01-01
    • 2017-08-10
    • 1970-01-01
    • 2020-02-06
    • 2022-01-22
    • 1970-01-01
    • 2015-11-13
    • 2015-01-23
    相关资源
    最近更新 更多