Deploying Restful APIs
BACK

Deploying Restful APIs

Services used: API Gateway, Lambda

Languages used: Python, JSON

Objective: Use Amazon API Gateway and an AWS Lambda function to create and deploy an API. Create an Amazon API Gateway REST API. Deploy the REST API. Invoke an AWS Lambda function by using API Gateway proxy integration. Use a sample Python file to create a new Lambda function. Create a new API Gateway endpoint and integrate it with the new Lambda function.

First, search for and open Lambda.

Click Functions and then Create functions.

Choose Author from scratch and give it the name labFunction. Select Python from the Runtime drop-down.

Scroll down and expand the Change default execution role section. Select Use an existing role and choose lab_function_role. Click Create function.

Scroll down to Code source and delete the default data. Paste the code from our sample Python file. Review the inputs and click Deploy, then click Test.

Create a new event and give it the name FindAllPets. Select the Template for an API Gateway AWS Proxy.

Scroll down to the Event JSON and change line 3 resources to pets. Click Save.

Choose FindAllPets in the Test dropdown menu. Click Test and review the output. Choose Configure test event in the Test dropdown menu.

Create a new event and call it FindPetById. Keep the other default settings and go to Template and select FindAllPets. Go to Event JSON line 3 and add the id element. Edit line 16 to include id and value. Scroll down and click Save.

Choose FindPetById in the Test dropdown. Click Test and review the id.

Go to the search bar and open API Gateway.

Scroll down and select Build REST API (not Private).

Select REST and then New API. Give it the name ApiLab and description API to support lab. Click Create API.

Go to the Actions dropdown and select Create Resource.

Give it the name pets then click Create Resource.

Go back to the Actions dropdown and select Create Method.

Choose GET and click the check mark. Ensure the type is a Lambda function, check the box for Use Lambda Proxy integration, and select the Lambda function labFunction. Click Save.

Click OK in the pop-up.

Select GET in the Method Execution window and click TEST. Scroll down and click Test.

Review the Status and Response body.

In the Resources pane click /pets. Go to the Actions dropdown and select Create Resource. Give it the name id and add curly brackets to the id in Resource Path. Click Create Resource.

In the Resource pane, click /{id}. Then from the Actions dropdown select Create Method.

Select GET and click the check mark.

Select Use Lambda Integration proxy and type labFunction for Lambda Function. Click Save. Click OK again for the pop up.

Click the id GET and then TEST.

Type 1 for the Path id and scroll down and click Test.

Review the Response Body and ensure that the correct id and name is displayed.

Go to the Actions dropdown and select Deploy API.

Select New Stage, and give it the name lab and description Lab API then Lab deployment. Click Deploy.

Copy provided Invoke URL.

In a new tab or window, paste the copied URL and /pets to the end of the URL. Review the response.

Add /3 to the end of the URL. Review the response. Success!