0

I'm trying to migrate a working lambda running from python2.7 code python3.8. However I noticed urllib2 seems to be no longer supported on latest python3? May I ask if you guys also encountered this and able to resolved it?

This is the error I'm getting:

[ERROR] Runtime.ImportModuleError: Unable to import module 'lambda_function': No module named 'urllib2'
Traceback (most recent call last):

The Python code:

from __future__ import print_function

import json, boto3, urllib2

new_ips = [ '172.0.0.0/8', '192.0.0.0/24']
group_id = 'sg-1235484513245482'
ec2 = boto3.resource('ec2')

def lambda_handler(event, context):

    allowed_ips = new_ips
    print(allowed_ips)

    security_group = ec2.SecurityGroup(group_id)
    current_ip_ranges = [ x['CidrIp'] for x in security_group.ip_permissions[0]['IpRanges'] ]
    print(current_ip_ranges)


    params_dict = {
        u'PrefixListIds':[],
        u'FromPort': 443,
        u'IpRanges': [],
        u'ToPort': 443,
        u'IpProtocol': 'tcp',
        u'UserIdGroupPairs': []
    }
    
    revoke_dict = params_dict.copy()
    for ip in allowed_ips:
        if ip in current_ip_ranges:
            revoke_dict['IpRanges'].append({u'CidrIp': ip})
           

    print("the following Spotfire ip addresses will be removed:")
    print(revoke_dict['IpRanges'])
    
    security_group.revoke_ingress(IpPermissions=[revoke_dict])
    
    return {'revoked': revoke_dict}
    
Lagot
  • 639
  • 1
  • 9
  • 26
  • Does this answer your question? [Import error: No module name urllib2](https://stackoverflow.com/questions/2792650/import-error-no-module-name-urllib2) – Jan Wilamowski Mar 17 '22 at 03:28

1 Answers1

0

seems like urllib2 is no longer used for python3, removing it from the code fixed the issue

Lagot
  • 639
  • 1
  • 9
  • 26