Skip to main content

Creating API Gateway Resources and Methods in the AWS Console

Hands-On Lab

 

Photo of Fernando Medina Corey

Fernando Medina Corey

Training Architect

Length

01:30:00

Difficulty

Intermediate

In this activity we’ll create an API with multiple resources and methods to integrate with our Lambda function. We’ll rely on the use of the Lambda Proxy integration with API Gateway and group our methods into two resources. Before beginning the lab, go to getpostman.com and download the program for your operating system.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

API Gateway in the AWS Console

For this lab, we are creating an API with multiple Resources and Methods to integrate with our Lambda functions. Using the Lambda Proxy, we will integrate our Lambda functions with the API Gateway by creating two resources and attach methods to those resources that use our functions.

Before we Begin

Got to getpostman.com and download the program for your operating system. We will be using this later when deploying our API.

Create an API

Sign in to the AWS Console using the information provided upon launching the lab. Once signed in, use the Services dropdown and under Networking & Content Delivery select API Gateway.

We're taken to the Amazon API Gateway page. Select Get Started. We are taken to the Create new API page. We have three options to choose from; for this lab, select New API. Name the API Prometheon. Set the description as Our Prometheon API. Leave the Endpoint type as the default, Edge Optimized.

Select Create API.

Create Resources

With our API created, it is time to populate it. We are on the /Methods page where we will add our resources. To begin, select the Actions dropdown, and select Create Resource.

Set the Resource Name to Prometheon and enable the Enable API Gateway CORS. Create Resource. We selected CORS because it provides us with an OPTIONS section for our resource. If it is not selected, then this won't appear.

Child Resource

Select the /Prometheon before using the Actions dropdown again. Select Create Resource. For this resource, name it ID and again select Enable API Gateway CORS. The resource will again have the OPTIONS section underneath it. Note that this only appears once even when multiple items are set to Enable API Gateway CORS.

Methods

With our resources created, we can add methods to them. We will be adding methods both to our main API, Prometheon, and to it's sub-resource, ID.

Once again, select /Prometheon, but this time select Create Method. From the list, choose Post and then click on the checkmark that appears to confirm the selection.

For this method, we'll leave it as Lambda Function. Select Use Lambda Proxy Integration and Use Default Timeout. Set the Lambda Region to us-east-1, which we've been using throughout the rest of our labs. Map the Lambda Function section to create. Select Save. We get a prompt telling us we are giving the API Gateway permission to invoke a Lambda function. Select OK.

We are sent to a page that provides us with a visual flowchart.

Now, we need to add our Get method. To do so, follow the same steps as before, but instead of selecting Post select Get. Keep the Lambda Function selected, chose the Use Lambda Proxy integration box, set it to us-east-1, and then set the Lambda Function to list. Select Save

We now need three more methods under our ID section. All three follow the previous steps and settings, only we will have /ID selected instead of /Prometheon.

For our final three, we need a Get, Put, and Delete method under /ID. Map them to the following Lambda functions:

  • For the Get method, map it to the Get Lambda function.
  • For the Put method, map it to the Update Lambda function.
  • For the Delete method, map it to the Delete Lambda function.

Once these three are in place, we can move on.

Deploy API

With all of our resources and methods in place, it is time for us to deploy our API. To do so, go to Actions and select Deploy API from the drop-down.

On our Deploy API page, fill out the fields as follows:

  • Deployment stage = New stage.
  • Stage name* = Dev
  • Stage description = Dev
  • Deployment description = Dev

Once filled out, select Deploy.

Testing our API

Congratulations! We have a working API! Now, we need to test it. To do so, we will be using the Postman program we downloaded at the beginning of the lab. Open Postman and drop in the Invoke URL that appears on our Dev Stage Editor page. At the end of it, add /prometheon.

Set the drop-down on the left to GET, make sure you have the Authorization tab below it selected, and then select Send. The Body section on the lower half of the page will show all of the items being pulled with our Get method.

Now that we know our Get method is working correctly; let's testPost. Set it as the dropdown. Instead of using Authorization, select the Body tab beneath the URL. Change it from form-dateto raw, then set the dropdown to JSON(application/json) and insert the following:

{"Artist": "TestArtist", "SongTitle":"Test Song", "AlbumTitle": "TestAlbum", "CriticRating": "8.9", "Genre": "Classical", "Price": "1.99"}

This information, once sent, goes from the gateway to the lambda function.

Select Send and the Body section will return all the information that we put in.

We can further check out the function by opening a new tab and going to Dynamo DB.

Go to Tables, PrometheonMusic, and then perform a Query for the item that we just created by entering 'TestArtist' in the Partition Key section. Select Start Search and the item we just added appears.

Query Parameters

The main parts of our API work, wonderful! But we aren't done testing yet. Let's perform a query using nothing but a URL. To do so, add /id?Artist=TestArtist&SongTitle=Test%20Song at the end of our current URL. This string represents a query performed for the Partition key, Artist and the Sort key, SongTitle. Now, as we have our get function set up, this currently will return an error. To fix it, go back into our console and head over to the Lambda section under Services. In here, select get.

Scroll down to find the code for this function. Find the following line:

const data = JSON.parse(event.body);

We don't want to pull from the JSON, but the event queryStringParameters. To do so, replace the line with:

const data = event.queryStringParameters; 

Once finished, select Save.

In Postman, hit Send. We get back all the information back that we created. Copy all information in the second set of {}. Do not copy the "Item": portion.

Note if this test does not work, your get method may not be set up correctly. Go back to your methods, delete the current ones, and re-create them. Make sure both are mapped to their respective settings for /prometheon and /ID and that Use Lambda Proxy Integration is selected in both.

Put Update Function

One child method down, two to go. This time, we'll check on the Put method. Set the URL back to the original API URL with the addition of /prometheon/id at the end.

Select the Headers tab in postman. We should already have a Key called Content-Type, created when we performed our Post method. The value is set to application/json. Go back to the Body tab. The information we used for Post should still be there, if not, past it back in. Now, change the CriticRating to 9.9 and the Genre to Rock 'n Roll.

Hit Send.

To check that this worked, go back to the AWS console. Go back to DynamoDB, PrometheonMusic table, and Query for TestArtist again. A new version with our updates appears.

Delete Method

Our last method to test is Delete. In postman, go back into the Body tab and remove everything but the Artist and SongTitle in the Body, as shown:

{"Artist":"TestArtist","SongTitle":"Test Song"}

Select the Delete method and then Send.

Go back into the AWS console and DynamoDB. Query again for the TestArtist.

Nothing will appear this time because we deleted it.

Review

Congratulations on completing this lab and learning how to not only create resources and methods but how to put them into practice with an API. Remember, APIs can be tricky and vindictive, so don't worry if it takes you a few tries to get everything down. Feel free to keep practicing in the provided environment even after finishing the main lab.