Creating an AWS Lambda Function and API Endpoint
Published: December 15, 2016
Introduction
This is a step-by-step guide to setting up an AWS Lambda function and attaching it to an API endpoint. The goal of this tutorial is to get you familiar with setting up an AWS Lambda function that you can POST data to and return a response.
Disclaimer
This example uses a minimal AWS security configuration, which should not be used for production applications.
Prerequisites
- An AWS account with admin privileges
- cURL to test the API Endpoint (optional).
Walkthrough
If you prefer to watch a walkthrough, here is a recording of the steps below in action. 👀
Getting Started
Select a Blueprint
After clicking Get Started Now, you will be presented with a list of blueprints to select. This is an optional step that we will skip for now. Instead, click Configure triggers in the top left navigation menu.
Configure Triggers
An AWS Lambda is only a function, so something will have to trigger that function. In our tutorial, we want to POST data to a url and trigger the function. To do this in the AWS world, you will use the API Gateway trigger.
Step 1
Click the dotted-grey box and select API Gateway in the menu.
Step 2
Here you will select the API to use and how it will be invoked. If this is your first time using the API Gateway, AWS will setup a gateway titled LambdaMicroservice. AWS will also create a resource named /null which isn't ideal, but I'll show you how to change that later. For now, make sure to update the following:
- Method = POST - Since our tutorial will be submitting POST requests to the API
- Security = Open - This makes the endpoint publicly accessible without requiring a token
Configure Function
Here we'll setup the function to run. Most of this should have the correct defaults, but AWS stores your previous settings in local storage, so double check that everything matches below.
Step 1
Give the function a name and description. We'll use the default function for this example. In case this looks different for you, here is the code:
exports.handler = (event, context, callback) => {
// TODO implement
callback(null, 'Hello from Lambda');
};
Step 2
Scroll down till you reach the Role* option. This is where we specify the AWS security role to run the function as. If this is your first AWS Lambda function, I'd recommend creating a new role from template and giving it a name. In the example, I used slack-events as the name of my new role. Everything else can remain the same.
Review
The last step is just to confirm the we got everything entered correctly. If everything looks good, click Create function.
Success
If all goes well, you should have a fancy new Lambda function waiting for requests and a URL to send requests to.
Testing
AWS has a test console in the Web UI, but I prefer testing it externally. This example shows a sample cURL post.
curl -X POST https://1grvd23rfe.execute-api.us-east-1.amazonaws.com/prod/null
Update Endpoint [optional]
Things are working, but that /null endpoint is bothering you, right? No problem! Now that Lamba did all the heavy lifting of setting up the AWS API Gateway service for us, we can add a new endpoint to listen to easily.
Configuration
- Goto your Lambda function
- Click the Triggers tab
- Click the method POST
- Use the left tree to navigate to APIs > LambdaMicroservices > Resources
- Select to root / and click Actions > Create Resource just above it
- Name your resource and desired path
- Select your newly created resource and click Actions > Create Method
- Select POST in the new dropdown under your resource and click the checkmark to the right
- Select Lambda Function as the integration type
- Select the region you created your function in
- Type in the name of your function. This should auto-complete and find it for you.
- Click the Actions > Deploy API
- Select prod and click Deploy
- Success!
Walkthrough
There are a bunch of steps, but it sounds worse than it really is. Here's a recording of the the steps in action.
Testing
curl -X POST https://1grvd23rfe.execute-api.us-east-1.amazonaws.com/prod/slack