Invoke the methods of REST API Gateway

Without API Gateway

Without an API Gateway, you directly invoke the Lambdas function using different function URLs, with any HTTP methods as in previous workshop:

curl 'https://u2z6j6noy3vnrynejsjqmkgiry0asnnm.lambda-url.ap-southeast-1.on.aws/'
curl 'https://qzpsv22gd3s4qbnfwz2v5yefoy0dmipa.lambda-url.ap-southeast-1.on.aws/' \
  -H 'content-type: application/json' \
  -d '{ "id": "a3127179-6ba4-4c3b-855a-4f65d4ee6345" }'
curl 'https://zvybyad2wvq5dto3upm2mgtcwa0epmul.lambda-url.ap-southeast-1.on.aws/' \
  -H 'content-type: application/json' \
  -d '{ "name": "First Cloud Journey", "email": "fcj@example.com" }'
curl 'https://hk3icryf6br2ociwan5ier2gqe0gyaoi.lambda-url.ap-southeast-1.on.aws/' \
  -H 'content-type: application/json' \
  -d '{ "id": "bcfe3cf9-1607-489e-8501-f99c194e6cc9", "email": "fcj@aws.com" }'
curl 'https://5ywq2njgsehqpl3xl2nrs334ue0inscy.lambda-url.ap-southeast-1.on.aws/' \
  -H 'content-type: application/json' \
  -d '{ "id": "bcfe3cf9-1607-489e-8501-f99c194e6cc9" }'

Notice each time, you make a HTTP call to a different URL, you - as the end user - are handling the routing to the API for CRUD operations.

With API Gateway

After integrate the Lambda functions to the API Gateway, our architect now looks like this:

alt text

After integrate the Lambda functions to the API Gateway with the type of REST API,

  • instead of directly invoking the Lambda functions,
  • you call the REST API Gateway using:
    • the same URL (the Invoke URL of the API in a stage)
    • different resource paths
    • different HTTP methods
  • then the API Gateway also directly invoke your Lambda functions.

API Gateway acts as a trigger for Lambda functions

When an other AWS service directly invoke your Lambda function, that’s service is called a trigger for your Lambda function.

In our case, the API Gateway is acting as a trigger for the Lambda functions.

Under the hood, when we call the API Gateway - an event occurs in API Gateway:

  • API Gateway generates data for that occurred event

    The data is in JSON format contains

    • the event type
    • other information…

This generated data is also know as generated event (or in short event).

  • API Gateway then directly invoke your Lambda function with the (generated) event.

Congratulation, you just create your first event-driven architect in AWS.


Invoke our REST API Gateway

Replace https://pg6xn32zdc.execute-api.ap-southeast-1.amazonaws.com/dev with your API’s Invoke URL.

# Make a GET request to /users path (to invoke list-users)
curl 'https://pg6xn32zdc.execute-api.ap-southeast-1.amazonaws.com/dev/users'
# Make a POST request to /users path (to invoke create-user)
curl 'https://pg6xn32zdc.execute-api.ap-southeast-1.amazonaws.com/dev/users' \
  -X POST \
  -H 'content-type: application/json' \
  -d '{ "name": "First Cloud Journey (API)", "email": "fcj-api@example.com" }'
# Make a GET request to /users/{userId} path (to invoke get-user)
curl 'https://pg6xn32zdc.execute-api.ap-southeast-1.amazonaws.com/dev/users/5aab87c6-1328-44c9-98c6-c556c1351591'
# Make a PATCH request to /users/{userId} path (to invoke update-user)
curl 'https://pg6xn32zdc.execute-api.ap-southeast-1.amazonaws.com/dev/users/5aab87c6-1328-44c9-98c6-c556c1351591' \
  -X PATCH \
  -H 'content-type: application/json' \
  -d '{ "name": "First Cloud Journey (API)", "email": "fcj-api@aws.com" }'
# Make a DELETE request to /users/{userId} path (to invoke delete-user)
curl 'https://pg6xn32zdc.execute-api.ap-southeast-1.amazonaws.com/dev/users/5aab87c6-1328-44c9-98c6-c556c1351591' \
  -X DELETE

alt text

If you still keep the function URLs in previous workshop, now you can delete all of them. The API Gateway can invoke these Lambda functions without the function URLs.