How to configure APIs for Lambda

51 Views Asked by At

I made a lambda function that calls an API and returns the content returned from said API in JSON format:

import json
import requests

print('Loading function')


def lambda_handler(event, context):
    URL = "https://geocoding.geo.census.gov/geocoder/geographies/onelineaddress?address="
    URLB = "&benchmark=Public_AR_Census2020&vintage=Census2020_Census2020&layers=10&format=json"
    # print(event)
    # address = event["address"]
    address = "1111 Morse Ave Sunnyvale, CA 94089"
    req = requests.get(URL + address + URLB)
    data = req.text
    return {
    'statusCode': 200,
    'headers': {'Content-Type': 'application/json'},
    'body': event
    }   
    #raise Exception('Something went wrong')


Right now I am having a peculiar issue: I can't seem to figure out how to link an API to it.

So I have 2 questions:

  1. When I am configuring a REST API via the triggers menu, is there any special configuration I have to do with the API?
  2. What format am I supposed to write the URL in to define event['address']?

thanks!

When I set the address variable to something constant, it works fine and returns the correct value.

When it is not, I get {"message": "Internal server error"} back from the API call.

I am pretty sure I need to include parameters in the API call but I don't know how.

1

There are 1 best solutions below

0
On

It appears that you are wanting to trigger an AWS Lambda function via a URL.

There are two ways to do this:

Both methods allow parameters to be passed via the URL, which will be provided to the AWS Lambda function via the event parameter.