A working API in less than 5 minutes
Lane Thompson
Chief Executive Officer @ Seedling | Creating Cutting Edge Low Code Tools
Easily, the best thing to happen in cloud development is the advent of serverless computing. If you aren't familiar with that concept, simply put, it is using cloud resources to run your code on hardware that is fully managed by someone else.
What makes server less nice is that you don't need to worry about managing any kind of hardware. There is almost no configuration needed. That means you can build and deploy an API in minutes.
For a recent project, I needed an email signup form. There are of course, a lot of services that handle that, but I knew I could build my own in less time than it would take to research, read the docs of, and integrate a 3rd party.
I'm going to show you what and how I did it.
AWS Lambda
My go to for almost every project is AWS Lambda, which is a serverless (no hardware management) service on Amazon Web Services. The advantage of lambdas is that I can build and deploy an API in less than 5 minutes.
The other huge advantage is the way Lambdas scale. If I have zero users my bill is $0. Then as I get more users my costs will scale up at exactly the same pace as my user growth. Meaning, I can know with near certainty how much it will cost me to have any number of users.
Setup
I timed myself and if you know where everything is this process takes less than 30 seconds.
Get to the Lambda service
First login to the [https://console.aws.amazon.com](AWS Console)
Once you are logged in search for "Lambda"
In the upper right hand corner click "Create Function"
Creating the Lambda
The default is "Author from scratch" which is what you usually want to do. You also need to name the function. Make sure you pick something descriptive so you can find the function later.
The big choice you will need to make is "Runtime." This allows you to change the language and language version of your function. For most people I recommend "Node.js 18.x" this is a fairly up to date version of Node and most developers can move fastest in Node. The other popular choice is Python. But I hate Python, so you can look elsewhere for that advice!
To spend a little bit more time on those choices. If you are a .NET or Java developer, you will notice there is support for certain versions. I recommend against using either of those in this context. Lambda gives you another option "Container image." You can use that to use a Docker container. Using this you can use any programming language you like!
The other two areas on this page are "Permissions" and "Advanced settings"
Permissions in AWS are the most complex and difficult to use system in the cloud. Anyone who wants to disagree will have to contend with the fact that the majority of vulnerabilities are down to permissions issues.
For a getting started project you can leave the permissions with the default "Create a new role with basic Lambda permission." Remember, you can always change these later based on what you are doing.
The final section is "Advanced settings." You can enable several helpful features here. "Enable function URL" and "Enable VPC" are the ones you would most commonly choose, but you don't need to choose either.
Now click "Create function"
Enabling function URLs
领英推荐
Once it finishes creating you will be on the page for your new function. The feature I want to point out is the "Configuration" tab. It contains all the advanced options from the creation process but also a bunch more options. Most of which you will never use. For a quick API you will only need to mess with the "Function URL."
Function URLs are a way to bypass a lot of complexity. Without them you will need to configure an API Gateway which is an extraordinarily complex service. Simply put, what the Function URLs do is make an endpoint that you can immediately access. That means you can copy and paste the associated URL into your frontend and have an actual working, integrated API instantly!
If you are setting up Function URLs for an existing lambda, these are the steps.
Go to the "Configuration" tab.
Click on "Function URL"
Click "Create function URL"
It will now ask you to choose the "Auth type"
Typically, you will want this to be "NONE." If you need security on your endpoint then you will probably want to use more powerful systems like API Gateway.
Now, open "Additional settings."
Click the checkbox on "Configure cross-origin resource sharing (CORS)"
This will pop open a BUNCH of CORS options.
Allow origin is what urls will be allowed to access this function. For testing purposes the "*" default value is fine.
The only change you will want to make is to change the "Allow method" Depending on what you are doing with this API you will want to choose which options are best for you. For a simple test endpoint I would change it to "*" which is the top option.
Finally click "Save"
Testing
On the main page for your lambda you should see a "Function URL" that looks like this https://gy3eoeynwbw7egp26jmz2e7u4m0jplyj.lambda-url.us-east-1.on.aws/
Now you can add this URL to your app.
const data = await axios.post(
"https://gy3eoeynwbw7egp26jmz2e7u4m0jplyj.lambda-url.us-east-1.on.aws/",
{ test: "test" }
);
It will respond with 'Hello from Lambda!'
You did it!
That's all you have to do! In another article I will discuss how to modify the source code and make a super useful starter CRUD API!