Using AWS API Gateway and Dynamodb for a simple api

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so I've been trying to figure out all day how to get a dynamodb table to hook up to the API gateway to give me a publicly accessible API URL so I think I finally figured it out so let's see how this goes I'm going to click on dynamo DB and I'm going to click on create a table we're going to call this test the partition key is like the primary key we're going to call this my ID just for the fun of it we're going to keep it as a string and use default settings should be fine create and now it's creating a table so you can see over here we've got two tables not what is the one I just made called test which I managed to misspell so we're off to a perfect start so I'm gonna click on items and this is basically the rows there's nothing in here yet because I haven't entered in any data so let's start doing that put a couple lines in here create an item and here you got my ID and then we're going to give it a string of 1 and then we'll go ahead and append another item to this called a string and we'll say notes and here we'll just say AWS is hard sometimes and we'll add another one down here say string again and I'll say date and I don't just type in today's date probably not the way you would do that in real world but I'm just trying to add some data click Save and now you can see I have one row do another one my ID string to append string notes and then when I skip the date I guess I could add it in here let's see insert a pen probably string date and we'll just do it in a different format just because this is fun and there you go I've got a couple lines why don't we do one more while we're at it my d3 go down here pen string notes date now okay so now I got three rows in my TST table so that's all we need to do over here although I am going to go back over to here and we're going to need this Amazon resource name so I'm going to go ahead and copy this so now the next thing we're going to do is we're going to get a policy so I'm going to go back click on this and we'll go to I am and we're gonna go over to policies and here we're going to create a policy I'm going to use a policy generator and we're going to do allow we're going to click find an service which is going to be a dynamo which for some reason is not where you think it would be there it is actions what I'm going to be doing in this one is we're just going to query it I'm going to ask for the information I'm not going to pass any information in I just want to see what was in there and then here's where I just copied it in pace I just need to paste that in and I'm actually going to go back a couple of steps to just where it says table and I'm going to put an asterisk and there's gonna be a wild card you can get anything I'll be able to get any table hopefully if this works I'm gonna click on add statement and there you go go ahead and click next step I didn't see this the first time through policy name we're gonna call this dynamo test and here you can see the policy basically allow dynamo DB query resource the database dynamo DB the region you are in and then your account number and then if you want to lock it down you'd have the table name here I just did asterisk so I can get any table and it doesn't like me because put a space in there and now if I filter this by types customer managed and you can see I've got dynamo test so when you've got the policy then you need a role so go over on roles and we're gonna create a new role and we're gonna say dynamo test role will do no spaces this time and it's where I got really lost so there's a bunch of pre-made roles and here's the Amazon API gateway select and then there's just one random policy that's not what I want so you can go past this next step and we'll say create role and there's my role and then we're going to attach the policy that we already made I'll do filter attach policy and then you also have to have a trust relationship and I think that's already right I didn't do anything we'll see if it works and one thing that was helpful in troubleshooting this is this simulate policy and I already got that open up here and you have your policy over here the things you wanted to do and then run simulation the tricky thing over here is that by default the simulation resource is asterisk which is everything but you probably want to have it locked down over here to just one thing and that doesn't allow access to every thing and so they kept failing that was very frustrating okay so we've got a policy we've got a role we've got the database table now we need to go do the API gateway so we're going to create an API new API API name we're going to call this dynamo test create an API under actions here we're going to do create a resource and we're just going to call this my ID that's the field that we're going to use and so this is going to end up being the URL URL of whatever Amazon is going to give us slash my ID and then slash the number create resource and then we're going to do another resource create resource this time it's going to be curly brackets well not up there my ID here again and then down here it's going to be curly brackets my ID sort of a way to get the variable my ID out of what we're going to pass in great resource and then we're going to do a method and this method is going to be gets because we're trying to get something at least I think that's what is for and then here we're going to do advanced AWS proxy do your region I was doing US West West one a bunch of times it's really us West two is where I'm at pick your service this is dynamo nothing in the sub domain method everything for dynamo DB is a post so even though over here we did get this is still post use action name the action is going to be query and then here we need the role that we gave it so we're going to go back over here and here's our roles this is the test role and there is the AR and again I'm going to copy that back over here to the Gateway I'm going to paste that in there okay I'm going to click Save and then you get to this sort of workflow one thing that I found very frustrating is there's no way to edit this that I've been able to find once you that last screen we're on it's gone now and I haven't been able to figure out and get it back to just edit something if I did the role name wrong I wanted to be able to edit I can't figure out how to do it so in here the only thing we're going to change is this integration we press integration request and down here we're going to do mapping I have been checking this middle one not out of percent sure what that means still learning this add mapping template it wants you to type application slash JSON but it won't actually give it to you so you have to type it in and then click the check mark and then that gives you this place over here to do a template and the template that I've been using is sort of based on this and this is the whole thing I've been doing is based on this article using Amazon API gateway as a proxy for DynamoDB scroll down to the bottom is where he did his get comments and that is this part right here this is what he has in the mapping we're going to copy that go back to the Gateway paste this in here and then we need to change it so the table name is not comments it was TST the index name it's not an index name we're gonna have to change that it was my ID but it was an index name I believe it's primary index we didn't actually create an index and over here page ID this also needs to be my ID and one more time down here this needs to be my ID and actually that's not primary index it's primary key I believe not sure if capitalization matters but give a shot table name TST match the primary key my ID and my ID is going to be what's going to be passed in as a parameter on the URL click Save and we're across our fingers we're going to go back up and back to method execution and we're going to do tests and the first one I did was one so if we click one and click test what do you know it actually worked count one items my ID one a date notes AWS is hard to change this the three there's three change it to four there isn't one it's going to give an error of some sort and so now you've got this working next thing you have to do is to deploy it so under actions deploy API deployment stage there's going to be new I'm going to call this test because that's what everything else has been called deploy and now here it's giving you a URL we'll click on that you're not going to get anything because you haven't done the path that we said we're going to do my ID and then a number and now look at that we got what we put in there this isn't properly formatted yet I haven't gotten this far you can see count 1 probably didn't want their items should just be my ID : one date and then the date it's got this s in there I believe when you go back to here and not on the stage won't be on resource that I believe here in it in the response you can clean that up
Info
Channel: ephotopros
Views: 45,466
Rating: undefined out of 5
Keywords: aws, api-gatway
Id: fHcslTrkMz4
Channel Id: undefined
Length: 12min 44sec (764 seconds)
Published: Sat Jun 11 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.