Amazon web services Boto3向非默认VPC中的安全组添加入站规则
我正在尝试向非默认VPC中的安全组添加入站规则。我正在使用以下代码:Amazon web services Boto3向非默认VPC中的安全组添加入站规则,amazon-web-services,boto3,aws-security-group,Amazon Web Services,Boto3,Aws Security Group,我正在尝试向非默认VPC中的安全组添加入站规则。我正在使用以下代码: import boto3 ec2 = boto3.client('ec2') def modify_sg_add_rules(): response = ec2.authorize_security_group_ingress( IpPermissions= [ { 'FromPort': 3306,
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操作时出错(缺少参数):请求必须包含参数groupName或groupId
我尝试过将组名包括在内,但仍然不起作用。使用安全组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—按安全组名称进行过滤。你好,Encho:是!它起作用了!在遵循你的建议之后,我不得不稍微调整一下我的代码。