How To Build Your First Serverless API with AWS Lambda and API Gateway
In this piece I’ll be guiding you through the process of building a Serverless API on AWS.
I have written up a more complex follow up article here that would be an excellent next step once you have read this one.
- An AWS account (https://portal.aws.amazon.com/billing/signup#/start)
Creating Your First Lambda Function
Login to the AWS console and navigate to the Lambda service:
Click Create Function and you should see the following configuration screen:
We will use the default Author from scratch option for now.
Name your function (I called mine myFirstFunction), select Node.js for the runtime and Create a new role with basic Lambda permissions.
Finally, click Create function.
We now have our first function!
As you can see above, the page has an editor where you can modify the function code.
Change the “Hello From Lambda!” to something more interesting and click Save.
Now we can test the function. Click Test and a modal will appear where you will need to create a test event. We won’t be doing anything special here so name your event (I called mine MyTestEvent) and click Create.
Finally, click Test again and your function will run with the test event you just created. As you can see below, my function returned “Hello Andrew” in the body of the response:
Creating Your First Serverless API
Now that we have created our first function, it is time to turn to the API side of things. Navigate to the API Gateway service where, if you have never used the service before, you should see an introductory screen:
Click Get Started, select REST and New API. Name your API (I called mine My First API), select Regional and click Create API. We use a Regional API here as using CloudFront to spin up our API around the world is out of the scope of this guide:
Now that your API has been created, you have many options available to you. You can create API Keys, use Custom Domain Names, implement authorisers etc. I recommend you conduct a thorough exploration after this tutorial!
For now, we are going to create a resource on your API. Click the Actions dropdown and then click Create Resource.
Name your resource (I named mine events) and click Create Resource.
Now we can add various methods on our new resource. Click the Actions dropdown and this time click Create Method:
A small dropdrown then appears with various HTTP methods to choose from. In our case, we are going to use the GET method. Once selected, click the circle tick icon.
On the right, you can now configure your new method. We are now going to link the GET events method to the Lambda function we created earlier. Select Lambda Function as the integration type and type the name of the function you created earlier before clicking Save.
A modal will appear requesting permission for API Gateway to invoke your function. Click OK.
AWS then shows a really neat diagram of the entire flow. You can click into any of the steps to modify your configuration if you wish.
Our final step is to deploy our new API resource and method. Click Actions again and Deploy API:
A modal then appears asking which stage you wish to deploy the API to. Create a new stage and name it production before clicking Deploy.
You will then be taken to the Stages screen where your deployed domain will be made available for you. Click the Invoke URL to test it out.
Remember to add
events to the end of your provided URL. You should see the response from your Lambda function:
Now navigate back to your Lambda function and modify it to be more interesting. In this case, I return some mock events:
This guide barely scratches the surface of what is available with API Gateway. You can, among other things:
- Add request validation
- Use AWS Cognito to require authentication to access your APIs
- Deploy the API to your own custom domain
- Use API keys to limit the API use
As I mentioned earlier, I have written up a more complex article here that would be an excellent next step if you are interested in pursuing these ideas further.
Looking for technology-related content? We can create a newsfeed for you. It’s easy. 👋 Join us today and receive similar stories each week in your inbox!
If this post was helpful, please click the clap 👏 button below a few times to show your support! ⬇
For any questions, contact AVM Consulting: firstname.lastname@example.org.