Design a site like this with WordPress.com
Get started

[6] [ProjectX] – Using AWS API Gateway with AWS Lambda

In our previous blog post, we created Lambda function and used AWS Console Test button to invoke it.

However, in practical scenarios, we will need something else to expose this function on internet and a way to invoke it. This is what AWS API Gateway service does best.

So, in this blog post, we will configure API Gateway service to call our Lambda function

Big Picture:

Below diagram depicts what we are trying to do.

Creating API

STEP-1:

Log into your AWS Console and open API Gateway service landing page

STEP-2:

Click Create API button. Fill the details as shown below and click Create API button located below

STEP-3:

You will see that the API has been created for you as shown below.

STEP-4:

Now we must create what is called a Resource. Click Action dropdown above and select Create Resource option

STEP-5:

Fill the values as shown below and click Create Resource button

STEP-6:

Next it will show you that resource is created. Now click Actions drop-down and this time select Create Method option

STEP-7:

Click-the drop down and select GET. Then click tick mark besides it

STEP-8:

You will see the screen shown below. Type myFullName in the Lambda Function text box and click Save button at bottom

STEP-9:

You will get a prompt like below. Click OK.

STEP-10:

You will see screen like below, which confirms your API and its Methods are created.

STEP-11:

Now we will go ahead and Deploy the API. Select Deploy API option from Actions drop-down

STEP-12:

Next window will prompt you for Stage details. Enter information as show below

STEP-13:

Finally, your API is deployed, and you will get an Invoke URL as shown below

Copy this Invoke URL someplace, as we will require it for testing.

This completes Creation of API using AWS API Gateway. Now we have created a callable wrapper around our Lambda function, which can be called directly from Internet.

 Check Association with Lambda

How can we be sure that this API we created has been associated as callable wrapper to our Lambda function? Let’s check.

Go to your Lambda function and open Designer

As you can see above, the API Gateway is now associated to left side of our Lambda function.

Testing API and Lambda

Let’s now proceed with testing the API and in turn Lambda function also.

STEP-1:

For this you have to download an API testing utility called Postman https://www.getpostman.com/products

STEP-2:

After downloading and installing it, let’s start with testing our API. Open Postman and enter values as shown below

Url: https://tvq5ic4j0l.execute-api.us-east-1.amazonaws.com/dev/fullname

Drop-down Method – GET

Body: raw

Value in text box: {“firstName”: “Jason”,”lastName”: “Bourne”}

Note: Replace with your URL that you had noted earlier and make note of resource name (fullname) appended at end of the Url

STEP-3:

Click on Send button and you should see the results as below

STEP-4:

Awesome. It works. If you want, you can go to CloudWatch and check LogStreams. It will have logs of our latest call

With this, we have a Serverless function that can be invoked on internet from anywhere in the world. Cool right.

Conclusion:

This completes creation of API as wrapper to our Lambda function. You can give this URL of API to anyone all over the world and they will be able to invoke your Lambda function.

Advertisement

3 thoughts on “[6] [ProjectX] – Using AWS API Gateway with AWS Lambda

  1. […] are already familiar with other ways provided by AWS for resource creation like AWS Console, AWS CLI, AWS SAM, AWS CloudFormation etc. Now we have one more option […]

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: