My python function stopped working after amazon aws dropped lambda support python 3.6, I updated python to 3.7.
import boto3
import os
import random
import string
import botocore
from botocore.client import Config
AWS_REGION = os.environ['AWS_REGION']
DEBUG = false
# generate a random string of n characters, lowercase and numbers
def generate_random(n):
return ''.join(random.SystemRandom().choice(string.ascii_lowercase + string.digits) for _ in range(n))
# checks whether an object already exists in the Amazon S3 bucket
# we do a head_object, if it throws a 404 error then the object does not exist
def exists_s3_key(s3_client, bucket, key):
resp = s3_client.head_object(Bucket=bucket, Key=key)
return True
def handler(event, context):
print(event)
BUCKET_NAME = os.environ['S3_BUCKET'] # from env variable
native_url = event.get("url_long")
cdn_prefix = event.get("cdn_prefix")
### Generate a short id for the redirect
# check if short_key object already exists - collision could occur
s3 = boto3.client('s3', config=Config(signature_version='s3v4'))
while (True):
short_id = generate_random(7)
short_key = "u/" + short_id
if not(exists_s3_key(s3, BUCKET_NAME, short_key)):
break
else:
print("We got a short_key collision: " + short_key + ". Retrying.")
print("We got a valid short_key: " + short_key)
### Third step: create the redirection object in the S3 bucket
resp = s3.put_object(Bucket=BUCKET_NAME,
Key=short_key,
Body=b"",
WebsiteRedirectLocation=native_url,
ContentType="text/plain")
public_short_url = "https://" + cdn_prefix + "/" + short_id;
return { "url_short": public_short_url, "url_long": native_url }
But now when I invoke the script from AWS Lambda getting the below error, not sure if I am missing anything on the script?
What may have changed from python 3.6 to 3.7?
{
"errorMessage": "name 'false' is not defined",
"errorType": "NameError",
"stackTrace": [
" File \"/var/lang/lib/python3.7/imp.py\", line 234, in load_module\n return load_source(name, filename, file)\n",
" File \"/var/lang/lib/python3.7/imp.py\", line 171, in load_source\n module = _load(spec)\n",
" File \"<frozen importlib._bootstrap>\", line 696, in _load\n",
" File \"<frozen importlib._bootstrap>\", line 677, in _load_unlocked\n",
" File \"<frozen importlib._bootstrap_external>\", line 728, in exec_module\n",
" File \"<frozen importlib._bootstrap>\", line 219, in _call_with_frames_removed\n",
" File \"/var/task/index.py\", line 10, in <module>\n DEBUG = false\n"
]
}
>Solution :
F should be capital in false
DEBUG = False