Create an Endpoint With AWS Lambda and API Gateway

Lukas Ruebbelke
InstructorLukas Ruebbelke
Share this video with your friends

Social Share Links

Send Tweet
Published 2 years ago
Updated 2 years ago

AWS makes setting up an endpoint straightforward.

In this lesson, you will learn how to create an AWS Lambda function that returns JSON data, and then use API Gateway to set up an endpoint with a public URL.

Instructor: [0:00] For this example, we are going to use data from College Football Data API. The idea is that we're going to pull in some football data, transform it, and then, ultimately, deploy it as an endpoint in AWS. The first thing we need to do is to authorize our call to this API.

[0:18] In the header, I'm going to add bearer and then paste in my key. Don't worry, I will kill this by the time you're watching this video. Next, we'll call git on the Games endpoint. I'll click Try It Out, add in the year 2021, and add in the team of my choice, "Go Buckeyes." Then I'll hit Execute. This will return an array of regular season games.

[0:47] What's interesting is that this also outputs a curl command that I can call to execute this same operation. I can take this, and I can hop into the terminal and add in a pipe symbol and pbcopy, which will then take the result of this remote call and put it on my clipboard.

[1:06] Now that it's on my clipboard, I can hop into StackBlitz and easily paste it in. This is a very convenient step. Let's go ahead and create a schedule property and paste in the JSON response from the curl command. We'll also take a moment to format this response.

[1:28] Now that we have a legible data structure, let's go ahead and create a method called prettyPrint() that's going to take in the schedule array of Game objects. Next, we'll map over that and do basic string interpolation on the Game object itself. Schedule.map, pass in the game, and from here, we'll pull out the game week, home team, away team, and venue.

[1:57] Now that we have prettyPrint() in place, let's go ahead and demonstrate its output. Let's delete this line and replace it with a pre tag so that the output is formatted. Then, using JSON.stringify(), call prettyPrint() and pass in the schedule. We'll tighten this up a little bit, and we have a data structure we can use for the rest of our demonstration.

[2:24] Next up, to deploy this function into the Cloud, we'll go to the AWS Management Console. I could click on the Lambda() method here, but I typically prefer to hit the search bar, type it in, and that takes me straight to the Lambda service. Now, I can click on the Functions Option on the left and then click Create Function.

[2:46] I usually prefer to author from scratch. We'll leave that as the default, and then we'll type in a function name, here egghead-hello-api. We'll leave this at the 14.x runtime and go ahead and click here to create the function.

[3:08] Once this has been created, we can jump down and see that we have a basic handler that accepts an event and outputs something into the body. We're going to create a test event that we'll call Test, and we'll click Create.

[3:27] Now that I have that event created, I'm going to click Test, and we will see that it echoed the object in the response handler. From here, let's go ahead and take the code that we created in our StackBlitz function and copy it all the way up to the schedule array property.

[3:47] When we finally have all our code selected and copied, we can head back to our Lambda and paste it in. Once our code is pasted in, we will go ahead and replace this JSON.stringify('Hello from Lambda!') with JSON.stringify(prettyPrint()) and update the parameter to use the schedule array. Let's go ahead and test this again.

[4:12] First, we'll go up to the top and we will hit Deploy. Then we can test it. Pro tip, make sure you deploy before you test. You'll now see that we have a long string with a JSON structure inside of it. There's a reason why we're doing that. For now, I'm going to go down to the bottom and replace the call to JSON.stringify() with a direct call to prettyPrint().

[4:36] Let's deploy it one more time and then click Test. You can see here that we are now getting a more standard-formatted JavaScript array, but we still need to expose this to the outside world. To do that, we'll need to create an API gateway, which requires a string instead of a raw JSON object.

[4:58] For now, we'll put JSON.stringify() back with a call to prettyPrint(), and we will deploy it. From here, we'll create an API gateway so we can hit this function from the outside world. We're going to scroll all the way up to the top and add a trigger.

[5:17] We'll click API Gateway, go to the dropdown below, and create a new API by selecting Create an API. We'll go with HTTP API because it's the simplest type. Let's leave security open for this example.

[5:33] Under Additional Settings, we'll name our API. In this case, AWS has tacked on a redundant API suffix, which I will remove. We'll want to click the checkbox to allow cross-origin resource sharing, and then we're clear to go to the bottom and click Add.

[5:51] Now that this has been deployed, we can go back and select Trigger and see that we now have an API endpoint. Let's click on this and see what happens. What do you know? This is the data structure we've been working with this entire time but fully accessible from a public URL. Now, let's go ahead and curl this endpoint from the command line, and we have lift-off.

[6:17] I hope this session has given you an idea of how easy it is to create a basic function, put it into AWS, and finally deploy it and call it from either the browser or the command line.

mathias gheno
mathias gheno
~ 2 years ago

I had a error in the creation of my lambda function. The error was "Warning: Not authorized to perform: lambda:GetFunction for at least one of the lambda functions. Deployment will not be skipped even if service files did not change." and also "Stack:arn:aws:cloudformation:us-east-1:258835071595:stack/aws-lambda-serverless-dev/5d0d0dd0-a33a-11ec-b697-12393fae425f is in ROLLBACK_COMPLETE".

I solved it deleting in the AWS console everything I had in the CloudFormation, I added the "lambda:GetFunction" in the yml file and also created a user as admin because the one with CloudFormation and Lambda full access didnt work. Im just testing so maybe this is not all related.

Action:
  - "lambda:GetFunction"
Resource:
  Fn::Join:
    - ""
    - - "arn:aws:lambda:::"
      - "Ref" : "ServerlessDeploymentBucket"
      - "/*"
Markdown supported.
Become a member to join the discussionEnroll Today