[version_1.0]
The exercises in this course will have an associated charge in your AWS account. In this exercise, you will create the following resource:
The final exercise includes instructions to delete all the resources that you create in the exercises.
Familiarize yourself with Amazon API Gateway pricing and the AWS Free Tier.
In this exercise, you build the REST API that’s used to list and add dragons. You first create the API by using mock integrations. You can use mock integrations to create a testable API before any code is written for your backend services.
After the API is created, you deploy an updated version of the web application. The web application now contains the frontend logic to access the GET
and POST
methods of your /dragons
resource.
Later in the course, we will discuss authentication for the API and implement the backend services.
In this task, you will build the REST API. The REST API will have a single resource, /dragons
, with GET
and POST
methods.
In the console, open the Amazon API Gateway dashboard by searching for and choosing API Gateway.
Make sure you are in the N. Virginia Region.
If you already have APIs, choose Create API. If not, continue to the next instruction.
Under Choose an API type, locate REST API, and choose Build.
Note: Do not select the REST API Private option because you will access this API from the internet.
If this API is your first one, close the Create your first API window by choosing OK. You should be presented with a sample PetStore API. You won’t be using this data, so select the New API option.
DragonsApp
Choose Create API.
In this task, you will create the /dragons
resource and add a GET
method. For now, you will configure the GET
method with a mock integration.
After you create the REST API, you will be redirected to the Resources navigation pane. Your API currently has a single root-level resource.
In the Resources navigation pane, select Actions > Create Resource.
In Resource Name, enter dragons
.
In Resource Path, enter dragons
.
Choose Create Resource.
In the Resources navigation pane, ensure that the /dragons
resource is selected.
Select Actions > Create Method.
Under the /dragons
resource, you should see a selection menu for methods. Choose the menu and select GET.
Save the new method by choosing the check icon.
For Integration type, select Mock.
Choose Save.
In the Resources navigation pane, you should see that the GET
method for /dragons
is selected. On the right, choose Integration Response.
In the table, expand the single default mapping, and then expand Mapping Templates.
Under Content-Type, choose Add mapping template. Then enter application/json, and choose the check icon to save your changes.
In the text area under Generate Template, paste the Apache Velocity Template (VTL) code. You can copy this code from the following expandable section.
The VTL code contains a JavaScript Object Notation (JSON) array. Ensure that you are copying all the code, including the opening bracket and the closing bracket.
Expand for mapping template VTL.
[
#if( $input.params('family') == "red" )
{
"description_str":"Xanya is the fire tribe's banished general. She broke ranks and has been wandering ever since.",
"dragon_name_str":"Xanya",
"family_str":"red",
"location_city_str":"las vegas",
"location_country_str":"usa",
"location_neighborhood_str":"e clark ave",
"location_state_str":"nevada"
}, {
"description_str":"Eislex flies with the fire sprites. He protects them and is their guardian.",
"dragon_name_str":"Eislex",
"family_str":"red",
"location_city_str":"st. cloud",
"location_country_str":"usa",
"location_neighborhood_str":"breckenridge ave",
"location_state_str":"minnesota" }
#elseif( $input.params('family') == "blue" )
{
"description_str":"Protheus is a wise and ancient dragon that serves on the grand council in the sky world. He uses his power to calm those near him.",
"dragon_name_str":"Protheus",
"family_str":"blue",
"location_city_str":"brandon",
"location_country_str":"usa",
"location_neighborhood_str":"e morgan st",
"location_state_str":"florida"
}
#elseif( $input.params('dragonName') == "Atlas" )
{
"description_str":"From the northern fire tribe, Atlas was born from the ashes of his fallen father in combat. He is fearless and does not fear battle.",
"dragon_name_str":"Atlas",
"family_str":"red",
"location_city_str":"anchorage",
"location_country_str":"usa",
"location_neighborhood_str":"w fireweed ln",
"location_state_str":"alaska"
}
#else
{
"description_str":"From the northern fire tribe, Atlas was born from the ashes of his fallen father in combat. He is fearless and does not fear battle.",
"dragon_name_str":"Atlas",
"family_str":"red",
"location_city_str":"anchorage",
"location_country_str":"usa",
"location_neighborhood_str":"w fireweed ln",
"location_state_str":"alaska"
},
{
"description_str":"Protheus is a wise and ancient dragon that serves on the grand council in the sky world. He uses his power to calm those near him.",
"dragon_name_str":"Protheus",
"family_str":"blue",
"location_city_str":"brandon",
"location_country_str":"usa",
"location_neighborhood_str":"e morgan st",
"location_state_str":"florida"
},
{
"description_str":"Xanya is the fire tribe's banished general. She broke ranks and has been wandering ever since.",
"dragon_name_str":"Xanya",
"family_str":"red",
"location_city_str":"las vegas",
"location_country_str":"usa",
"location_neighborhood_str":"e clark ave",
"location_state_str":"nevada"
},
{
"description_str":"Eislex flies with the fire sprites. He protects them and is their guardian.",
"dragon_name_str":"Eislex",
"family_str":"red",
"location_city_str":"st. cloud",
"location_country_str":"usa",
"location_neighborhood_str":"breckenridge ave",
"location_state_str":"minnesota"
}
#end
]
Under the template box, choose Save.
You have designed the first steps of your API. Next, you will deploy the API to a stage, where you can test the resource method in a browser.
Select Actions > Deploy API.
prod
Choose Deploy.
You will be redirected to the Stages navigation pane.
In the Stages navigation pane, expand the prod stage.
Choose the GET method for the /dragons
resource. On the right, you will see an Invoke URL for the selected method.
Copy the Invoke URL. It will be in a format that is similar to https://abcde01234.execute-api.us-east-1.amazonaws.com/prod/dragons
. Paste this URL into a file, so that you can refer to it later in the exercise.
Paste the URL into a new browser tab and confirm that you see the expected JSON output from the VTL that you configured previously.
Experiment by adding some query strings to the URL, for example:
In this task, you will create a dragon model that will be used to validate the POST
method.
In the navigation pane, under API: DragonsApp, choose Models.
Choose Create.
dragon
application/json
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Dragon",
"type": "object",
"properties": {
"dragonName": { "type": "string" },
"description": { "type": "string" },
"family": { "type": "string" },
"city": { "type": "string" },
"country": { "type": "string" },
"state": { "type": "string" },
"neighborhood": { "type": "string" },
"reportingPhoneNumber": { "type": "string" },
"confirmationRequired": { "type": "boolean" }
}
}
Choose Create model.
In this task, you will create a POST
method for the /dragons
resource. Now that you have configured validation, you can test different request bodies. This feature of API Gateway handles much of the validation for you, which means that you can focus on your code.
To return to the resources pane, locate the navigation pane and under API: DragonsApp, choose Resources.
In the Resources navigation pane, choose /dragons.
Select Actions > Create Method.
Choose the menu of methods for the /dragons
resource and select POST.
Save the new POST
method by choosing the check icon.
For Integration type, select Mock.
Choose Save.
In the Resources navigation pane, you should see that the POST
method for /dragons
is selected. On the right, choose Method Request.
Next to Request Validator, choose the pencil (edit) icon and select Validate body.
Save this setting by choosing the check (update) icon.
Expand the Request Body section.
application/json
Save this setting by choosing the check (create) icon.
Next, you will test the request validation before you deploy the API.
To return to the method execution view, in the top area of the window, choose ← Method Execution.
Choose TEST ⚡.
First, test with a valid request body. For Request Body, paste the following valid JSON data for a dragon:
Choose ⚡ Test.
On the right, observe a successful validation. In Logs, you will see Request validation succeeded for content type application/json.
Now, try testing the method with data that will not validate, such as an empty request body or a non-valid property:
The APIs will be accessed from a web application that is served on a different domain than the APIs. A request from a web application to another domain is called a cross-origin HTTP request.
To allow the browser to use resources from another domain, you will enable Cross-Origin Resource Sharing (CORS) for the /dragons
resource. For additional information, see Enabling CORS for a REST API resource in the Amazon API Gateway Developer Guide.
In the Resources navigation pane, choose /dragons.
Select Actions > Enable CORS.
Choose Enable CORS and replace existing CORS headers.
Confirm the action by choosing Yes, replace existing values.
Now, deploy your changes to the prod
resource. Select Actions > Deploy API.
For Deployment stage, select prod.
Choose Deploy.
In this task, you will deploy an updated edition of the web application and use it to communicate with the newly mocked API.
The updated web application is available at RELATIVEdownloads/webapp2.zipRELATIVE
. You might want to attempt the deployment yourself before you follow the step-by-step instructions.
In the AWS Cloud9 or local IDE terminal, download and extract the web application.
Copy all the contents of the webapp2 folder to Amazon Simple Storage Service (Amazon S3).
Run the following AWS Command Line Interface (AWS CLI) command.
Visit the Dragons web application by pasting the URL output into a web browser.
On the Home page of your application, under Dragons Endpoint, enter the invoke URL that you previously saved into a file.
At the end of the invoke URL, delete the dragons
resource. For example, https://abcde01234.execute-api.us-east-1.amazonaws.com/prod/dragons
becomes https://abcde01234.execute-api.us-east-1.amazonaws.com/prod/
.
In the web application, choose List and then choose GET /dragons. You should see the API response in a table. You can experiment with some different values for Name and Family.
In the web application, choose Add.
Enter values for a new dragon observation, and choose POST /dragons. A successful post will clear the form for another observation to be entered.