# Pieces slowly falling into place... struggling to understand API Gateway to Lambda.

**URL:** <https://awshq.com/t/pieces-slowly-falling-into-place-struggling-to-understand-api-gateway-to-lambda/18337>\
**Category:** Security & IAM\
**Tags:** sqs, lambda, cognito\
**Created:** [April 27, 2024, 11:47am UTC](https://awshq.com/t/pieces-slowly-falling-into-place-struggling-to-understand-api-gateway-to-lambda/18337 "2024-04-27T11:47:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![stormy3000](https://awshq.com/letter_avatar_proxy/v4/letter/s/e5b9ba/32.png) [@stormy3000](https://awshq.com/u/stormy3000)\
**Post date:** [April 27, 2024, 11:47am UTC](https://awshq.com/t/pieces-slowly-falling-into-place-struggling-to-understand-api-gateway-to-lambda/18337/1 "2024-04-27T11:47:50Z")

</div>

Hi,  
i’ve been learning AWS for a couple of months now. Initially understanding some key things via the console, then the Amplify CLI. Now I’m trying to build something up via CDK for the backend, and Amplify SDK (react) on the front end. (\*Not using Amplify CLI).

I’ve got:  
**CDK**  
CDK Master File, that creates a few different stacks (Cognito, Amplify, Lambda, SQS and API Gateway). Which I can successfully deploy.  
\*With SQS queue being passed to the lambda stack.

**Front End:**  
Using Amplify for hosting, and the SDK.  
Pushing up dates via github with CI/CL.  
Currently using withauthenticator and can register/signin and works with the userpool created via CDK and Cognito.

I’ve got a page with a simple button. Which I wanted to use to trigger the lambda function. Which should then simply write a message to the SQS queue. Just for testing as I get my head around how this all works.

**On my test page.**

**I have a button:**

 Test Lambda Function 

**Which should trigger the below code:**

import { APIGateway } from ‘aws-sdk’;  
export const testLambdaFunction = async () =\> {  
const apigateway = new APIGateway();  
const apiName = ‘LambdaTestAPI’; // replace with your actual API name or ID  
const path = ‘/’; // replace with your actual API path  
try {  
const params = {  
restApiId: apiName,  
resourcePath: path,  
httpMethod: ‘GET’,  
};  
const response = await apigateway.makeRequest(params).promise();  
console.log(response);  
} catch (error) {  
console.error(error);  
}  
};

I wonder if I’m approaching this all wrong… or on the right lines.

---

<div class="post-metadata">

**Author:** ![gkelly642](https://awshq.com/letter_avatar_proxy/v4/letter/g/c4cdca/32.png) [@gkelly642](https://awshq.com/u/gkelly642)\
**Post date:** [April 27, 2024, 11:55am UTC](https://awshq.com/t/pieces-slowly-falling-into-place-struggling-to-understand-api-gateway-to-lambda/18337/2 "2024-04-27T11:55:55Z")

</div>

The API gateway should have a URL which you send a HTTP request to, which in turn invokes the backing lambda.

Have a look round the gateway console for the URL, you will need to structure your request properly and use the correct auth mechanism etc.

You can use a library like axios to make the http request to your gateway.

---

<div class="post-metadata">

**Author:** ![cachemonet0x0cf6619](https://awshq.com/letter_avatar_proxy/v4/letter/c/5f9b8f/32.png) [@cachemonet0x0cf6619](https://awshq.com/u/cachemonet0x0cf6619)\
**Post date:** [April 27, 2024, 12:05pm UTC](https://awshq.com/t/pieces-slowly-falling-into-place-struggling-to-understand-api-gateway-to-lambda/18337/3 "2024-04-27T12:05:20Z")

</div>

nah. this is… no you misunderstand the api gateway.

you’d create the api gateway in cdk and your lambda would post to a rest api endpoint that you get from the cdk stack output.

you should not need to use the api gateway library. you shouldn’t need the aws sdk at all to test the endpoint.

also your using an old version of the sdk and possibly of node.

---

<div class="post-metadata">

**Author:** ![server\_kota](https://awshq.com/letter_avatar_proxy/v4/letter/s/dc4da7/32.png) [@server\_kota](https://awshq.com/u/server_kota)\
**Post date:** [April 27, 2024, 1:58pm UTC](https://awshq.com/t/pieces-slowly-falling-into-place-struggling-to-understand-api-gateway-to-lambda/18337/4 "2024-04-27T13:58:31Z")

</div>

This is how I do it:

- get the JWT token from Amplify js Auth module (since you are using Amplify).
- make a request to API Gateway.
- API gateway (which has Rate Limiting and CORS) passes it to AWS Lambda.
- AWS lambda extracts the token and checks its validity with keys from AWS Cognito.
- user authenticated

Basically I use API Gateway as Rate Limiting and CORS proxy before AWS Lambda is triggered.  
There is even integration in CDK.

api\_gateway = api\_gw.HttpApi(self, f"{app\_basics.app\_name}-Gateway",  
api\_name=f"{app\_basics.app\_name}-Gateway",  
cors\_preflight=api\_gw.CorsPreflightOptions(  
allow\_origins=[‘_‘] if app\_basics.is\_dev() else [  
f"https://{secrets.app\_secret\_manager.domain\_name}", “[https://stripe.com](https://stripe.com)”],  
allow\_methods=[  
api\_gw.CorsHttpMethod.GET,  
api\_gw.CorsHttpMethod.OPTIONS,  
api\_gw.CorsHttpMethod.POST,  
api\_gw.CorsHttpMethod.DELETE  
],  
allow\_headers=[’_’],  
max\_age=Duration.days(10)  
),  
default\_integration=integrations.HttpLambdaIntegration(“Integration”,  
handler=self.api\_lambda.function))

---

<div class="post-metadata">

**Author:** ![MrDiem](https://awshq.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@MrDiem](https://awshq.com/u/MrDiem)\
**Post date:** [April 27, 2024, 2:27pm UTC](https://awshq.com/t/pieces-slowly-falling-into-place-struggling-to-understand-api-gateway-to-lambda/18337/5 "2024-04-27T14:27:43Z")

</div>

Lambda run code inside your AWS account.  
Your web app is rendered inside your browser.  
You then need from your web app to be able to contact your backend inside AWS. In your case your lambda.  
Peoples often use REST api as en entry point to handle HTTP request coming from your client interface.  
Regarding your question, you may need to look at what an API is.  
It’s where API gateway is coming into place, it’s a managed REST API, this way you don’t need to self host a flask or express server for example.  
With API gateway you can connect lambda functions or other AWS ressources to execute actions on specific HTTP calls.

Also, as API gateway is enabled entry point to your code you will need to look on authorization and authentication mecanism.  
Depending of your API gateway type you will found multiple solutions.

---

<div class="post-metadata">

**Author:** ![BigSpringBag](https://awshq.com/letter_avatar_proxy/v4/letter/b/779978/32.png) [@BigSpringBag](https://awshq.com/u/BigSpringBag)\
**Post date:** [April 27, 2024, 3:14pm UTC](https://awshq.com/t/pieces-slowly-falling-into-place-struggling-to-understand-api-gateway-to-lambda/18337/6 "2024-04-27T15:14:13Z")

</div>

I think not only misunderstood API gateway by reading other comments… you might be on the wrong path as well… why not start with Amplify, Amplify CLI would give you most of the stuff that you need. you can create a REST API in Amplify, and you can call this API you just created using Amplify on the FE. it’s using API Gateway at the back btw…

---

<div class="post-metadata">

**Author:** ![stormy3000](https://awshq.com/letter_avatar_proxy/v4/letter/s/e5b9ba/32.png) [@stormy3000](https://awshq.com/u/stormy3000)\
**Post date:** [April 28, 2024, 7:10am UTC](https://awshq.com/t/pieces-slowly-falling-into-place-struggling-to-understand-api-gateway-to-lambda/18337/7 "2024-04-28T07:10:48Z")

</div>

> [@BigSpringBag](#):
>
> I think not only misunderstood API gateway by reading other comments… you might be on the wrong path as well… why not start with Amplify, Amplify CLI would give you most of the stuff that you need. you can create a REST …

Thanks for the reply. I’ve jumped back and forth on the approach of using Amplify CLI, but in the end I decided I preferred to learn the CDK approach as it seems like it will be more useful for future project plans, albeit with a steeper learning curve iniitally.
