Hi folks,
First of all, i’m pretty new on serverless staff.
I was trying to call an external API (Google URL Shortner to be precise) from my lambda function already published on AWS, but i get an error 500, and can’t see anything at logs.
So i embedded code with a try block to get error, but not happens on logs, the function simple stops to run on request.post
line. I have no clue what can be. I’ll appreciate who can help me.
My code below:
import sys
import requests
import json
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def get_shortened_url(long_url):
api_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
google_url = "https://www.googleapis.com/urlshortener/v1/url?key=" + api_key
data = json.dumps({'longUrl': long_url})
logging.info('google_url - Starts - %s' % long_url)
try:
result = requests.post(google_url, headers={'content-type': 'application/json'}, data=data)
result.raise_for_status()
short_url = result.json()['id']
logging.info('google_url - OK')
except requests.exceptions.RequestException as e:
logger.error("ERROR: Request Error: %s" % e)
sys.exit(1)
# except requests.exceptions.HTTPError as err:
# logger.error("ERROR: HTTP Error: %s" % err)
return short_url
Here the logs from serverless logs -f my_function -t
:
START RequestId: bc9b63e6-38e0-11e8-bf29-fff819ee1d14 Version: $LATEST
2018-04-05 11:50:53.620 (-03:00) bc9b63e6-38e0-11e8-bf29-fff819ee1d14 [INFO] Category - OK
2018-04-05 11:50:53.680 (-03:00) bc9b63e6-38e0-11e8-bf29-fff819ee1d14 [INFO] Supplier - OK
2018-04-05 11:50:53.750 (-03:00) bc9b63e6-38e0-11e8-bf29-fff819ee1d14 [INFO] Customer - OK
2018-04-05 11:50:53.750 (-03:00) bc9b63e6-38e0-11e8-bf29-fff819ee1d14 [INFO] google_url - Starts - https://my_url/1960c700-3747-11e8-86fb-81ce33840396/29fb889b-cf1f-431f-a7d0-9ff1ffa84b8a
END RequestId: bc9b63e6-38e0-11e8-bf29-fff819ee1d14
REPORT RequestId: bc9b63e6-38e0-11e8-bf29-fff819ee1d14 Duration: 6006.27 ms Billed Duration: 6000 ms Memory Size: 1024 MB Max Memory Used: 42 MB
2018-04-05T14:50:59.043Z bc9b63e6-38e0-11e8-bf29-fff819ee1d14 Task timed out after 6.01 seconds
Regards,
Danilo.