A simple Python3 Lambda function on AWS with an API Gateway

As will be apparent from my previous post I've been playing around with AWS's Lambda service. I'm loving these 'serverless' offerings - they make simple sites just so cheap and easy to host and you don't have to be worried about an entire back-end system just because of one annoying newsletter signup form.

I just thought I'd do a quick run through of how to set up a basic Lambda function with an API Gateway which will hopefully help someone along the way.

Firstly, login to your AWS account and go to the Lambda console, then click 'New Function'. You'll get a list of template there, I just search for hello-world-python3 and select it.

On the next page you'll want to 'add a trigger', specifically an AWS API Gateway. In the security options you'll want to select 'open' because we'll be triggering it with some Javascript later on which is executed by the browser.

Select 'Open' for security option

Click 'next', then give your function a name and a description.

I'll let you decide how to setup your function and let it do what you want it to do. One tip though, CORS becomes and issue when hitting these endpoints from the browser.

To make sure that your response Access-Control-Allow-Origin header is set to '*' I generally create a function that looks like this:

def response(message, status_code):  
    return {
        'statusCode': str(status_code),
        'body': json.dumps(message),
        'headers': {
            'Content-Type': 'application/json',
            'Access-Control-Allow-Origin': '*'

Then my main function will look like this:

def lambda_handler(event, context):  
        print("value1 = " + event['key1'])
        print("value2 = " + event['key2'])
        return response({'message': 'Big Thumbs up'}, 200)
    except Exception as e:
        return response({'message': e.message}, 400)

Finally, we create a new role for the function. There's no need to select a policy template in this instance. An AIM will be created with basic lambda execution permissions.

Click 'next' and now we're ready to head over to our API Gateway.

An API endpoint will have been created for us already. So login to the API Gateway console and click on 'APIs > Lambda Microservice > Resources' and we'll see our newly created endpoint there.

Firstly, we need to create a new 'method', in my case I created a new 'POST' method buy selecting 'create method' from the 'Actions' dropdown. Select your required method, then on the next screen you'll want to set the 'Integration type' to 'Lambda function', then select your region and auto complete your Lambda function name.

Next we need to 'Enable CORS' which can be done from the 'Actions' menu with the top level resource selected.

From there you should be able to just click the 'Enable CORS and replace existing CORS headers' button with the defaults selected. It will enable CORS for both the OPTIONS method and the POST method (or whatever other method you might have created).

Finally from the same 'Actions' menu you need to select 'Deploy API'.

Hopefully that's not all too vague and will point people in the right direction.