Create an API Gateway REST API with AWS CDK

Tomasz Łakomy
InstructorTomasz Łakomy
Share this video with your friends

Social Share Links

Send Tweet
Published 6 months ago
Updated 6 months ago

Now that we have successfully created our AWS Lambda functions, it's time to expose them using an API Gateway REST API. In this lesson, we're going to learn how to create, deploy, and test an API Gateway REST API with AWS CDK.

Instructor: [0:05] Now that we have created and deployed our Lambda functions, it is time for us to start building a serverless API. To do that, first of all, I'm going to import the API Gateway module from CDK, and I'm going to create a new REST API. I'm going to go over here and I'm going to type const todoServiceApi, and that's going to be a new apigateway.RestApi().

[0:23] The first argument is going to be this, the second argument I'm going to call it "TodoRestAPI", and for the props, I'm going to use the restApiName which I'm going to call "TODO Service API". Next up, we need to create a new RestApi Resource which I'm going to call todo. To do that, I'm going to do const todos = TodoServiceApi, and I'm going to add a new Resource which I'm going to call "todos".

[0:50] Next up, we need to add methods to our Resource. I'm going to start with todos.addMethod, and I'm going to add a "POST" method and a new apigateway.Integration. Whenever we send a POST request to this endpoint, I'm going to call new apigateway.LambdaIntegration(), and I'm going to pass in the createTodosFunction.

[1:18] Those 10 lines are going to do the following, we are going to create a new apigateway.RestApi, we are going to add a new Resource which I'm going to call "todos", and whenever we send a "POST" request to /todos, we are going to invoke the createTodosFunction that we have previously created.

[1:36] Now let me grab that, and copy and paste that two times, because for "GET" method, we are going to call the listTodosFunction -- let me copy and paste that over here -- and for "DELETE" method, we are going to call our deleteTodosFunction. Here it is. Now we can deploy our serverless API to AWS cloud.

[1:57] I'm going to open up the terminal, and run cdk diff. Here we can see that CDK is going to deploy all the resources, all the permissions, and everything required for us to use the API. We don't have to configure any of those by hand. With all of that, I'm going to go ahead and deploy this stack to AWS.

[2:17] After a successful deployment, let me go back to AWS console. I'm going to go to API Gateway console. Over here, we can see our brand new TODO Service API. Let's test if it works. I'm going to go over here. First of all, here we can see our /todos Resource and all the three methods that we have created in CDK.

[2:44] There's the POST, GET and DELETE. We can see that in the console. I'm going to click on GET, scroll down a little, and click on Test button. Here we can send a test request to this endpoint. I'm going to click on Test. Here we can see that the Request was successful and we got the Response body containing our todo items.

[3:04] Let's test the other two methods by adding and deleting a todo item. I'm going to click over here on POST, and I'm going to add a Request body of "todo" which is going to be equal to "Test the API". After clicking on the Test button, we can see the results over here. The Test the API todo item was added to our DynamoDB table.

[3:31] After I sent a test GET request, we can see that our todo item was added to the database. I'm going to grab this id and copy that because I would like to delete this item. After clicking on the DELETE button, I'm going to paste in the Request body and send the request.

[3:42] Here, we can see that the Todo item with this id was deleted from the database. As such, we have established that our serverless API works as intended.

egghead
egghead
~ 10 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today