【问题标题】:authorize aws security group using python boto3 with description for each ingress使用带有每个入口描述的 python boto3 授权 aws 安全组
【发布时间】:2018-11-04 23:37:45
【问题描述】:

当我使用不同的网络连接运行时,我正在准备一个脚本来使用 myip 和静态字符串更新安全组。

sg.authorize_ingress(DryRun=False,IpPermissions=[{'IpProtocol': 'tcp','FromPort': 22,'ToPort': 22,'IpRanges': [{'CidrIp': 192.168.2.3/32}]}])

以上授权工作正常,但如果我添加

sg.authorize_ingress(DryRun=False,IpPermissions=[{'IpProtocol': 'tcp','FromPort': 22,'ToPort': 22,'IpRanges': [{'CidrIp': 192.168.2.3/32,'Description': 'string'}]}])

根据语法,如果我应用描述部分,它会引发错误消息。是否可以用描述更新每个条目。

语法:

'IpRanges': [
                {
                    'CidrIp': 'string',
                    'Description': 'string'
                }

【问题讨论】:

    标签: python boto3 aws-security-group


    【解决方案1】:

    我使用AWS Command-Line Interface (CLI)

    IP=`curl -s http://whatismyip.akamai.com/`
    aws ec2 authorize-security-group-ingress --group-name "Foo-SG" --protocol tcp --port 22   --cidr $IP/32 --output text
    aws ec2 authorize-security-group-ingress --group-name "Foo-SG" --protocol tcp --port 3389 --cidr $IP/32 --output text
    

    但是,我还没有尝试使用Description 参数。

    见:authorize-security-group-ingress — AWS CLI Command Reference

    【讨论】:

      【解决方案2】:
      import boto3
      
      aws_access_key_id = 'XXXXXXXXXXXXXXXXXXXXXXXXXX'
      
      aws_secret_access_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
      
      ec2 = boto3.resource('ec2', region_name='region_name', aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key)
      
      sg = "security group id"
      
      security_group = ec2.SecurityGroup(sg)
      
      add_ip = security_group.authorize_ingress(GroupId=sg,IpPermissions=[ {'IpProtocol': 'tcp','FromPort': 80,'ToPort': 80,'IpRanges':[{'CidrIp': '127.5.5.5/32', 'Description' : 'description'}]}])
      

      请尝试使用上述代码。 因为它对我来说很好用。希望它也对你有用。

      【讨论】:

      【解决方案3】:

      这是我用于 boto3 创建 ec2s 和 ssh 的通用方法。

      # Configure so you can SSH
      ec2Client.modify_vpc_attribute( VpcId = vpc.id , EnableDnsSupport = { 'Value': DNS_SUPPORT })
      ec2Client.modify_vpc_attribute( VpcId = vpc.id , EnableDnsHostnames = { 'Value': DNS_HOSTNAMES })
      
      # Create a security group and allow SSH inbound rule through the VPC
      securitygroup = ec2.create_security_group(
          GroupName=SG_GROUP_NAME,
          Description=SG_DESC,
          VpcId=vpc.id
      )
      
      securitygroup.authorize_ingress(
          IpPermissions=[
              {'IpProtocol': SG_IP_PROTOCOL,
               'FromPort': SG_FROM_PORT,
               'ToPort': SG_TO_PORT,
               'IpRanges': [{'CidrIp': SG_IP}]}
          ]
      )
      

      这是用于 ssh 的,所以:

      SG_IP_PROTOCOL = 'tcp'
         SG_FROM_PORT = 22
         SG_TO_PORT = 22
         SG_IP = 'XXX.XXX.XXX.XXX/32' # your specified IP address
      

      在哪里

          ec2Client = boto3.client('ec2', region_name=aws_region)
      

      AWS Boto3 Documentation

      【讨论】:

        猜你喜欢
        • 2020-02-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-08
        • 2017-08-10
        • 2016-04-11
        • 2016-12-15
        • 1970-01-01
        相关资源
        最近更新 更多