Fur-ocious Fun: How to Use AWS API Gateway as a Cat Facts Proxy
Tags: api, aws, awsapigateway, cloudcomputing
Published: 2023-01-07 13:49:31

---
title: Fur-ocious Fun: How to Use AWS API Gateway as a Cat Facts Proxy
published: true
date: 2023-01-07 01:53:13 UTC
tags: API,AWS,AWSAPIGateway,CloudComputing
canonical_url: https://techdevblog.io/fur-ocious-fun-how-to-use-aws-api-gateway-as-a-cat-facts-proxy/
---

 ![Fur-ocious Fun: How to Use AWS API Gateway as a Cat Facts Proxy](https://techdevblog.io/content/images/2023/01/image_9-1.png)

Welcome to our tutorial on using [AWS API Gateway](https://aws.amazon.com/api-gateway/) as a proxy for the [Cat Facts API](https://alexwohlbruck.github.io/cat-facts/docs/)! Are you ready to learn how to leverage the power of the cloud to bring some feline fun to your application? Let's dive in!

First things first, you'll need to have the [AWS CDK (Cloud Development Kit)](https://aws.amazon.com/cdk/) installed on your system. If you don't have it already, head over to [the AWS documentation](https://aws.amazon.com/cdk/) to get it set up.

Next, you'll want to create a new CDK project for your [Cat Facts API](https://alexwohlbruck.github.io/cat-facts/docs/) proxy. You can do this by running the following command in your terminal:

```
cdk init --language=typescript

```

This will create a new directory for your project and set up some basic files and dependencies. Now it's time to add the [AWS API Gateway](https://aws.amazon.com/api-gateway/) HTTP Proxy library to your project. You can do this by running the following command in your terminal:

```
npm install @aws-cdk/aws-apigateway-http-proxy

```

With the library installed, it's time to start coding! Open up your `index.ts` file and import the following modules at the top of the file:

```
import * as cdk from 'aws-cdk-lib';
import * as apigateway from '@aws-cdk/aws-apigateway';
import * as proxy from '@aws-cdk/aws-apigateway-http-proxy';

```

Next, we'll define our [Cat Facts API](https://alexwohlbruck.github.io/cat-facts/docs/) endpoint URL as a constant. Replace `YOUR_API_ENDPOINT_HERE` with the actual endpoint URL for the Cat Facts API:

```
const CAT_FACTS_API_ENDPOINT = 'YOUR_API_ENDPOINT_HERE';

```

Now it's time to create a new API Gateway resource using the `apigateway.HttpApi` class. Add the following code to your `index.ts` file:

```
const api = new apigateway.HttpApi(this, 'CatFactsApi', {
  defaultIntegration: new apigateway.MockIntegration({
    integrationResponses: [{
      statusCode: '200',
      responseParameters: {
        'integration.response.body.message': '"Hello from the mock integration!"',
      },
    }],
  }),
});

```

This creates a new HTTP API with a mock integration, which will allow us to test our proxy without actually calling the [Cat Facts API](https://alexwohlbruck.github.io/cat-facts/docs/).

Now let's create a new resource for our Cat Facts endpoint. Add the following code to your `index.ts` file:

```
const catFactsResource = api.addResource('cat-facts');

```

With our resource created, it's time to add a proxy integration to it. This is where the magic happens! Add the following code to your `index.ts` file:

```
const catFactsProxy = new proxy.HttpProxyIntegration({
  integrationHttpMethod: 'ANY',
  options: {
    passthroughBehavior: apigateway.PassthroughBehavior.WHEN_NO_MATCH,
    integrationResponses: [{
      statusCode: '200',
    }],
  },
  uri: CAT_FACTS_API_ENDPOINT,
});
catFactsResource.addMethod('ANY', catFactsProxy);

```

This code creates a new `HttpProxyIntegration` and assigns it to our `catFactsResource`. The `HttpProxyIntegration` allows us to forward any incoming request to the [Cat Facts API](https://alexwohlbruck.github.io/cat-facts/docs/) endpoint and return the response to the client.

Now let's deploy our API Gateway. Add the following code to your `index.ts` file:

```
api.deploymentStage.addCorsPreflight({
	allowHeaders: [''],
	allowMethods: ['GET'],
	allowOrigins: [''],
});

```

This code sets up CORS (Cross-Origin Resource Sharing) for our API Gateway, allowing it to be accessed from any origin.

Finally, let's deploy our stack to AWS by running the following command in your terminal:

```
cdk deploy

```

This will create all the necessary resources in your AWS account and deploy your API Gateway.

And that's it! You now have a fully functional [Cat Facts API](https://alexwohlbruck.github.io/cat-facts/docs/) proxy using [AWS API Gateway](https://aws.amazon.com/api-gateway/) and the [AWS CDK](https://aws.amazon.com/cdk/). You can test it out by sending a GET request to the API Gateway endpoint, which should return a random cat fact from the [Cat Facts API](https://alexwohlbruck.github.io/cat-facts/docs/).

We hope you enjoyed this tutorial and learned something new about using [AWS API Gateway](https://aws.amazon.com/api-gateway/) as a proxy. Happy coding!

<!--kg-card-end: markdown-->