[SOLVED][AWS][Python] Call to external APIs from my Lambda function

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.

@danilolutz Can you please show me how you call the function? Maybe try a sls invoke -f <function_name> -d '{"longUrl": "http://www.google.com/"}' and post the results. I tried it and it works for me.

Thanks,
Rupak Ganguly

Hi @rupakg, thank you for you answer, but i figure it out, you need more information.

A have an endpoint like below:

  read_json:
      handler: reader/read.input_json
      events:
        - http:
            path: api/reader/json
            method: post
            cors: true

And my package structure as a follow:
wgproject

So i read json, do some database inputs, selects etc. and i call the function from one to another package.

And i call the shortener function like this:

s = 'https://www.mysite.com/nps/%s/%s' % (supplier_id, customer_id)
link = url_shortner.get_shortened_url(s)

Also the log that you ask for:

 sls invoke -f read_json -d '{"name":"Danilo Lutz","email":"danilolutz@gmail.com","cellphone":"16981040000","supplierId":"1960c700-3747-11e8-86fb-81ce33840396","plate":"AAA-1234","productCategoryId":"3f97a780-3295-11e8-ad56-b5128c9774b9","previousDateChange":"2018-01-07T08:15:43.511Z","previousKm":80000,"actualKm":90000}'
{
    "errorMessage": "'body'",
    "errorType": "KeyError",
    "stackTrace": [
        [
            "/var/task/reader/read.py",
            149,
            "input_json",
            "data = json.loads(event['body'])"
        ]
    ]
}

But in this case i guess i’m passing json wrong…

Thank you again for your time!
Regard’s,
Danilo

So, @rupakg, i have increased timeout to 60 seconds a have a decent log for you:

sls invoke -f read_json -d '{ "body": "{ \"name\": \"Danilo Lutz\", \"email\": \"danilolutz@gmail.com\", \"cellphone\": \"16981040000\", \"supplierId\": \"1960c700-3747-11e8-86fb-81ce33840396\", \"plate\": \"AAA-1234\", \"productCategoryId\": \"3f97a780-3295-11e8-ad56-b5128c9774b9\", \"previousDateChange\": \"2018-01-07T08:15:43.511Z\", \"previousKm\": 80000, \"actualKm\": 90000 }" }' -l
{
    "errorMessage": "2018-04-06T00:11:07.245Z dc59c72d-392e-11e8-b589-e9ca6926a722 Task timed out after 60.00 seconds"
}
--------------------------------------------------------------------
START RequestId: dc59c72d-392e-11e8-b589-e9ca6926a722 Version: $LATEST
2018-04-05 21:10:07.244 (-03:00)	dc59c72d-392e-11e8-b589-e9ca6926a722	[INFO]	JSON validation - OK
2018-04-05 21:10:07.268 (-03:00)	dc59c72d-392e-11e8-b589-e9ca6926a722	[INFO]	Category - OK
2018-04-05 21:10:07.275 (-03:00)	dc59c72d-392e-11e8-b589-e9ca6926a722	[INFO]	Supplier - OK
2018-04-05 21:10:07.281 (-03:00)	dc59c72d-392e-11e8-b589-e9ca6926a722	[INFO]	Customer - OK
2018-04-05 21:10:07.282 (-03:00)	dc59c72d-392e-11e8-b589-e9ca6926a722	[INFO]	google_url - Starts - https://www.webgarage.com.br/nps/1960c700-3747-11e8-86fb-81ce33840396/29fb889b-cf1f-431f-a7d0-9ff1ffa84b8a
END RequestId: dc59c72d-392e-11e8-b589-e9ca6926a722
REPORT RequestId: dc59c72d-392e-11e8-b589-e9ca6926a722	Duration: 60001.54 ms	Billed Duration: 60000 ms 	Memory Size: 1024 MB	Max Memory Used: 42 MB	

2018-04-06T00:11:07.245Z dc59c72d-392e-11e8-b589-e9ca6926a722 Task timed out after 60.00 seconds

But is very strange the Google API take so long to response back… maybe a configuration error?

So follow my serverless.yml

provider:
  name: aws
  runtime: python3.6
  timeout: 60
  stage: dev
  region: sa-east-1
  environment:
    DB_HOST: "database.url"
    DB_PORT: 0000
    DB_NAME: "database-name"
    DB_USER: user_for_db
    DB_PWD: XXXXXX

  vpc:
    securityGroupIds:
      - sg-XXXXXX
      - sg-XXXXXX
    subnetIds:
      - subnet-XXXXXXX
      - subnet-XXXXXXX
  iamRoleStatements:
    - Effect: Allow
      Action:
        - "ec2:CreateNetworkInterface"
        - "S3:ListBucket"
      Resource: "*"

functions:
  read_xml:
    handler: reader/read.input_xml
    events:
      - http:
          path: api/reader/xml
          method: post
          cors: true
  read_json:
      handler: reader/read.input_json
      events:
        - http:
            path: api/reader/json
            method: post
            cors: true
  categories:
      handler: category/get.get
      events:
        - http:
            path: api/categories
            method: get
            cors: true

resources:
  Resources:
    WriteDashPostLogGroup:
      Type: AWS::Logs::LogGroup
      Properties:
        RetentionInDays: "15"

plugins:
  - serverless-python-requirements

Finally i figure it out… i just remove VPC group from serverless.yml and it’s working… thank so much @rupakg

@danilolutz Glad to hear that it worked out.

1 Like