Creating a new API¶
We are going to create a new API with a resource compare-yourself
and 3 methods - POST
, GET
and DELETE
.
Creating an API¶
When creating a new API we have multiple options on how to go about this: - We can create either a REST or WebSockets API. - We can create a new API, import an existing one, import it from a text file in swagger/open api format, create an preset example API.
You can click on the example API
to see on how the swagger format looks like.
Creating resources¶
Now we have to create the compare-yourself
resource, we can do that under Action -> Create resource
. Note that the resources are hierarchical and we need to make sure we have selected the right resource before we are doing this. Currently we have only root resource, so we are ok.
You can define the resource name and path which can be different.
The configure as proxy resource
option means that this will be a catch-all resource that catches all methods and paths under it and pass it to other services.
The CORS option is a security option. This option will allow you to access the API from a different server as well introduce OPTIONS
endpoint that is sent to check endpoint availablility.
Creating an HTTP method¶
Now, while making sure we have selected the compare-yourself
reource, we can click on Actions -> Create Method
.
Then you can choose the method type, we want to select POST
.
Then it will bring up setup wizard, where we can choose what integration type it has:
- Lambda function - run some code on demand
- HTTP - we can pass trough to another HTTP service
- Mock - return a dummy response
- AWS service - pass trough to another AWS service
We want to select the lambda function
.
The option Use lambda proxy
will take the incoming requests and it's headers, pass that as json object to the lambda function. You will need to extract what you need and return a response.
Creating a lambda function¶
Let's click on the Create a lambda Function
link, which will bring us to the lambda console.
Once we click on Create Function
it will open up the lambda function.
The designer section will list all the triggers that calls the function as well all the access permissions it has. By default it has no triggers and has access to write on cloudwatch logs.
In the monitoring section we can see statistics, logs about the function.
On the bottom you can see the function code, which is the code that is being run when executing the function.
We can also set environment variables, tags as well as other settings on this page.
Connecting lamda functions to API Gateway¶
When we have created a lamda function, we are going to change the lambda region to other one and back to refresh the list, start writing the lamda function name, select the correct one.
Now we can click on the test button which is over the client
.
This will open up a page where we can test the endpoint.
Accessing the API from the WEB & Fixing CORS issues¶
In order to access the API from the web, we need to deploy it, we can check this in the stages section.
So, we are going to call Actions -> Deploy API
.
And this is going to create a new stage that gives us an URL, configuration for the stage
Now, if we would request the POST method from our website, it would return a CORS error:
To fix this, we are going to resurces -> post method -> method response
and under Response headers for 200
add Access-Control-Allow-Origin
.
And then under Integration Response
, we can provide it's value. It will be a wildcard in this case.
Body Mapping templates¶
Under Integration Request
we can set Body Mapping templates
.
This will give us a configuration where we can configure on what data is sent to the lambda function as an event.
For reference on how to use the template we can look up http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html
We can customize with something like this.
{
"age" : $input.json('$.personData.age'),
}
The $
refers to request body.
We can also use Body mapping templates
in the Integration Response
as well.
Using Models & Validating requests¶
We can go to the Models
section and create a model.
When created, we can go to Method Request
and add the model.
This will ensure validating all the data that is set in the model according to it's schema.
The json schema can be referenced from here: - https://json-schema.org/ - https://json-schema.org/understanding-json-schema/index.html
Also now, if we go back to Body mapping templates
, we can set model templates:
The template can be modified as well
Adding a DELETE method¶
We are going to do the same steps as previously for creating a DELETE method and creating lambda function for it.
Using path parameters¶
We can create an new child resource with a variable type
.
Under that, we can create a get method with a lambda function assigned to it.
And in the Body mapping templates
we can put something like this:
Accessing the API from the Web the right way¶
When selecting a resource, we can go to Actions -> Enable CORS
to setup CORS.