Quick start to GET or POST AWS API gateway with Lambda python

Create Lambda for API GET

  • 1. Create Lambda with python 3.6+

  • 2. Implement code

# Helper class to convert a DynamoDB item to JSON.
class DecimalEncoder(json.JSONEncoder):
    def default(self, o):
        if isinstance(o, decimal.Decimal):
            return str(o)
        return super(DecimalEncoder, self).default(o)


def lambda_handler(event, context):
    # Get path parameter from GET
    inputParameter = event['pathParameters']['Your_Parameter_Here']
    inputParameter = event['queryStringParameters']['Your_Parameter_Here']

    # Get request body from POST
    input = json.loads(event['body'])
    inputParameter = input['Your_Parameter_Here']
    
    return {
        'statusCode': 200,
        'body': 'callback(' + json.dumps({ "result":{ event }}
        , cls=DecimalEncoder) + ')'
    }    

API Gateway – create endpoint

  • 1. APIs -> Create API -> Resource -> Create Method (GET/POST) -> Check Use Lambda Proxy integration -> Choose Lambda Function (what you just create above)

  • 2. Deploy it -> Get url -> Start GET/POST now.

110 Comments

Leave a Reply

Your email address will not be published.