Lambdas & API Gateway
Sweet! Now that you know that you have a basic idea of what a Lambda is, let’s build one!
In WebStorm, start a new project.
Name it your “first-name-last-name-lambda” (for example, mine is calvin-fischer-lambda).
This project is going to have 5 files - “.gitignore”, “buildspec.yml”, “handel.yml”, “handel-codepipeline.yml”, and “index.js”. Make them now.
Let’s start with our “.gitignore” file. It’s just going to contain “.idea”, which is a file added on by WebStorm that we don’t want on GitHub.
Then let’s build the “handel-codepipeline.yml” file. This should be really similar to other handel-codepipline files you’ve made.
Then let’s make the “buildspec.yml” file. This will also be about the same as others you have done.
You can make a lambda using handel (https://handel.readthedocs.io/en/latest/supported-services/lambda.html), but we are going to make an api gateway with a lambda built in (https://handel.readthedocs.io/en/latest/supported-services/apigateway.html). That way we will have a URL we can call it on. Take note that the handler of the lambda is index.handler. That is where it is going to look for code to run. Build your “handel.yml” function like this-
Now, let’s write our “index.js” file.
The API gateway expects data in a certain way, so we will need to write a function to format the resonse.
Then we can write our handler.
Your Lambda is done! Now you can go and deploy it.
Run “git init”, “git add *”, and “git commit -m “initial commit””.
Then create a new repository on GitHub(if you don’t remember how to do that, check out Create Web Server)
Push your changes to GitHub, then deploy it using DevBot (if you don’t remember how to do this, check out Deploy to AWS - Beanstalk using Handel/Rockefeller)
Then login to AWS and watch it deploy.
Then go to services and click “API Gateway”
That will take you to a page with all the API Gateways, it’s a bit of an adventure to find your gateway, but you can do it.
Click the “Actions” button and deploy your API Gateway-
Choose “dev” as your deployment stage.
That will take you to a page with a URL at the top.
Use your browser to send a request to that URL-
Oh no, what happened? Well, with the API Gateway you need to have something in the path. Add “/hello” to the path and try again-
Yay! It works!