# Amazon API Gateway

### Features

API GW can be said to be a bridge between different AWS services or between Users of a Application and their corresponding backend. It can act as the "front door" for applications to access data, business logic, or functionality from the backend services. &#x20;

<figure><img src="https://3681896347-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjfQTFfcSjS8MYnjfKw2c%2Fuploads%2FETZkXCiGQOAOXI6xzDTm%2Fimage.png?alt=media&#x26;token=b20869fe-eb71-43b4-b6b4-8fc73bf72879" alt=""><figcaption><p>Source: <a href="https://docs.aws.amazon.com/apigateway/latest/developerguide/welcome.html">https://docs.aws.amazon.com/apigateway/latest/developerguide/welcome.html</a></p></figcaption></figure>

**Amazon API Gateway is a fully managed service to create, publish, maintain, monitor, and secure APIs at any scale with the help of API Keys and its tracking via usage plan**. Note that usage plan can be used to put limitations/restrictions on a particular API with the help of Quotas or throttling etc.

API Gateway supports RESTful APIs, WebSocket APIs and even HTTP APIs&#x20;

They support containerized and serverless workloads, as well as web applications. One such use case is its integration with Lambda:

<figure><img src="https://3681896347-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjfQTFfcSjS8MYnjfKw2c%2Fuploads%2FLycEN3dDGaShTSZUgKfL%2Fimage.png?alt=media&#x26;token=2d3d2a5b-7f79-4f1f-876a-3ec0810b3ac5" alt=""><figcaption><p>Source: <a href="https://dev.to/aws-builders/two-ways-to-directly-integrate-aws-lambda-function-with-amazon-api-gateway-3can">https://dev.to/aws-builders/two-ways-to-directly-integrate-aws-lambda-function-with-amazon-api-gateway-3can</a></p></figcaption></figure>

API Gateway supports thousands of concurrent API calls, including traffic management, CORS support, authorization and access control, throttling, monitoring, and API version management. &#x20;

The url is of the format: https\://<**random-api-key/id**>.execute-api.<**region**>-amazonaws.com/<**stage-such-as-dev**>/<**resource-name-such-as-default**>

Note that Stage can also have stage variables, which can be used to store configuration without modifying the code (example, URI for uat stage and prod stage can be different and this is stored in stage variables, so dynamically the value can be taken at runtime and used further by the API)

An HTTP or REST API GW can have versions with each version having a method (GET/POST...), Authorization and API Key&#x20;

<figure><img src="https://3681896347-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjfQTFfcSjS8MYnjfKw2c%2Fuploads%2FvuYbHmbaDBzrsGcPtX17%2Fimage.png?alt=media&#x26;token=c20a9d5a-fdbb-40c9-a696-b6d9eb27e76d" alt=""><figcaption></figcaption></figure>

API Gateway can also be private and **can be only accessed via VPC endpoints**.&#x20;

<figure><img src="https://3681896347-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjfQTFfcSjS8MYnjfKw2c%2Fuploads%2Fc3FZOWELacQqL3vkVYDu%2Fimage.png?alt=media&#x26;token=55c5b234-0c51-4df4-87e8-a699fa9f4361" alt=""><figcaption><p>Source: <a href="https://javapatel.wordpress.com/wp-content/uploads/2023/10/rest-private-api-coverimage.png?w=816">https://javapatel.wordpress.com/wp-content/uploads/2023/10/rest-private-api-coverimage.png?w=816</a></p></figcaption></figure>

### Authentication and Authorization

* API GW also supports Standard AWS IAM roles and policies, Endpoint policies for interface VPC endpoints
* Just like S3, it can also have Resource Policy, which can be used to configure for IAM users, Source IP Address ranges or CIDR blocks and even specified VPCs or VPC endpoints (any AWS account).&#x20;

{% hint style="info" %}
Please note that it best practice to specify VPC ID for private API GW in condition.
{% endhint %}

```json
// Generated by ChatGPT
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": "*",
      "Action": "execute-api:Invoke",
      "Resource": "arn:aws:execute-api:us-east-1:123456789012:abcd1234ef/*",
      "Condition": {
        "IpAddress": {
          "aws:SourceIp": "203.0.113.0/24"
        }
      }
    },
    {
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::098765432109:role/MyRole"
      },
      "Action": "execute-api:Invoke",
      "Resource": "arn:aws:execute-api:us-east-1:123456789012:abcd1234ef/*"
    }
  ]
}

```

The authorization workflow with IAM and resource policy can be highlighted as shown below:

<figure><img src="https://3681896347-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjfQTFfcSjS8MYnjfKw2c%2Fuploads%2FDpkIOV9lWyJRWdJzo3k2%2Fimage.png?alt=media&#x26;token=539e4f42-c775-4ad5-b181-d301cfeddfec" alt=""><figcaption><p>Source: <a href="https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-authorization-flow.html#apigateway-resource-policies-iam-policies-interaction">https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-authorization-flow.html#apigateway-resource-policies-iam-policies-interaction</a></p></figcaption></figure>

* It also supports Amazon Cognito User Pools
* One different authorization it supports is Lamda Authorizers

### Lambda Authorizers

It is a lambda function to control the access to a APIs behind a API Gateway. It can be used to build custom authentication or authorization apart from the usual IAM based or API key based.

It uses custom authorization scheme:

* Based on bearer token authentication strategy
* Uses request parameters to determine the caller's identity

There are two types:

* TOKEN authorizer (token-based)
* REQUEST authorizer (request parameter based)&#x20;

Based on authorizer values, access can be granted or denied as shown below:

<figure><img src="https://3681896347-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjfQTFfcSjS8MYnjfKw2c%2Fuploads%2F5acwoPGDcWKfeXPyTkJo%2Fimage.png?alt=media&#x26;token=48aecb1a-93d2-494d-97d0-310feee7eaa7" alt=""><figcaption><p>Source: <a href="https://docs.aws.amazon.com/images/apigateway/latest/developerguide/images/custom-auth-workflow.png">https://docs.aws.amazon.com/images/apigateway/latest/developerguide/images/custom-auth-workflow.png</a></p></figcaption></figure>

&#x20;Note that policy is usually cached for 30 seconds

### Steps to setup an API Gateway

<figure><img src="https://3681896347-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjfQTFfcSjS8MYnjfKw2c%2Fuploads%2FbayQYyLdde2WIAv3NVpr%2Fimage.png?alt=media&#x26;token=a82dcc35-33c7-4612-916e-71aec761053d" alt=""><figcaption></figcaption></figure>

<figure><img src="https://3681896347-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjfQTFfcSjS8MYnjfKw2c%2Fuploads%2F4Vu2uTusF6skaYrfmw8p%2Fimage.png?alt=media&#x26;token=a04eb0f3-05ac-4778-8201-c8e58b917e40" alt=""><figcaption></figcaption></figure>

<figure><img src="https://3681896347-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjfQTFfcSjS8MYnjfKw2c%2Fuploads%2FgNPeUnb2ClCNiETyhNqB%2Fimage.png?alt=media&#x26;token=d91ea88a-5d75-4174-b7c6-e823b0cfe3e2" alt=""><figcaption></figcaption></figure>

<figure><img src="https://3681896347-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjfQTFfcSjS8MYnjfKw2c%2Fuploads%2F9Q60YyFPkfpElY2nEE0O%2Fimage.png?alt=media&#x26;token=7698df22-8884-4cae-9558-6338c88cd075" alt=""><figcaption></figcaption></figure>

<figure><img src="https://3681896347-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjfQTFfcSjS8MYnjfKw2c%2Fuploads%2FjWiOGznxh3WJ24rg5TyR%2Fimage.png?alt=media&#x26;token=37b09dfa-6e53-402d-a16a-7604b35bd779" alt=""><figcaption></figcaption></figure>

<figure><img src="https://3681896347-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjfQTFfcSjS8MYnjfKw2c%2Fuploads%2FTz3X3BQ3Xeg3txsGrJYn%2Fimage.png?alt=media&#x26;token=6efc93f0-4c05-42bb-937f-71a9ec954b07" alt=""><figcaption></figcaption></figure>

<figure><img src="https://3681896347-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjfQTFfcSjS8MYnjfKw2c%2Fuploads%2FYdKijm2Oa6AGngLGU8J1%2Fimage.png?alt=media&#x26;token=2c50bc15-4d94-4070-af76-dcd17af75a22" alt=""><figcaption></figcaption></figure>

<figure><img src="https://3681896347-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjfQTFfcSjS8MYnjfKw2c%2Fuploads%2F3FsxYu2Pp2Lb10g9Sznu%2Fimage.png?alt=media&#x26;token=241df170-80f4-4847-802e-a437b2669acd" alt=""><figcaption></figcaption></figure>

<figure><img src="https://3681896347-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjfQTFfcSjS8MYnjfKw2c%2Fuploads%2FWRfzme3oVNdE8faym4d4%2Fimage.png?alt=media&#x26;token=3792c87e-9f44-49b4-866c-cca575cf92e2" alt=""><figcaption></figcaption></figure>

> Head back to Lambda to complete setting up trigger and then use API Gateway URL

<figure><img src="https://3681896347-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjfQTFfcSjS8MYnjfKw2c%2Fuploads%2FQQIhWuOJ4BAlVd43z012%2Fimage.png?alt=media&#x26;token=95b53e9e-5092-4a0d-8076-7687cc99a5d6" alt=""><figcaption></figcaption></figure>
