# Best way to secure AWS API Gateway from public

**URL:** <https://awshq.com/t/best-way-to-secure-aws-api-gateway-from-public/12487>\
**Category:** AWS Cloud Talk\
**Created:** [March 1, 2021, 4:48am UTC](https://awshq.com/t/best-way-to-secure-aws-api-gateway-from-public/12487 "2021-03-01T04:48:48Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![cysronald](https://awshq.com/letter_avatar_proxy/v4/letter/c/779978/32.png) [@cysronald](https://awshq.com/u/cysronald)\
**Post date:** [March 1, 2021, 4:48am UTC](https://awshq.com/t/best-way-to-secure-aws-api-gateway-from-public/12487/1 "2021-03-01T04:48:48Z")

</div>

Hi,

we are building a mobile app, that needs to access API gateway.  
we want to secure the api by allowing only access from our mobile app.  
reading some comments from a github source, they don’t recommend API keys.  
but to use the signature version 4 instead.

any recommendations for this use case?

* * *

**Related discussions:**

- [The Technologies That Will Replace Traditional Industries#FutureTechnologies #EmergingTechnologies #IndustryDisruption #ArtificialIntelligence #AIRevolution #Automation #Robotics #CloudComputing #DigitalTransformation #Innovation #TechnologyTrends #FutureOfIndustries #SmartManufacturing #Industrial](https://awshq.com/t/the-technologies-that-will-replace-traditional-industries-futuretechnologies-emergingtechnologies-industrydisruption-artificialintelligence-airevolution-automation-robotics-cloudcomputing-digitaltransformation-innovation-technologytrends-futureofindustrie/21750)
- [Agentic AI: The Rise of Self-Operating Intelligent Systems](https://awshq.com/t/agentic-ai-the-rise-of-self-operating-intelligent-systems/21771)
- [New UI rolling out, feels way faster!](https://awshq.com/t/new-ui-rolling-out-feels-way-faster/21690)
- [49, recently laid off Sys Admin, could use some encouragement!](https://awshq.com/t/49-recently-laid-off-sys-admin-could-use-some-encouragement/21668)
- [Permanently Closed??](https://awshq.com/t/permanently-closed/21779)

---

<div class="post-metadata">

**Author:** ![thenickdude](https://awshq.com/letter_avatar_proxy/v4/letter/t/f475e1/32.png) [@thenickdude](https://awshq.com/u/thenickdude)\
**Post date:** [March 1, 2021, 5:05am UTC](https://awshq.com/t/best-way-to-secure-aws-api-gateway-from-public/12487/2 "2021-03-01T05:05:31Z")

</div>

This is impossible. Anything your mobile app can do can be done without the mobile app, by ripping the credentials out of your app.

A really half-assed approach is to validate the User Agent header, because this will exclude unmodified web browsers from hitting your endpoint. It doesn’t prevent anybody determined from doing it, however.

---

<div class="post-metadata">

**Author:** ![KnitYourOwnSpaceship](https://awshq.com/letter_avatar_proxy/v4/letter/k/bc79bd/32.png) [@KnitYourOwnSpaceship](https://awshq.com/u/KnitYourOwnSpaceship)\
**Post date:** [March 1, 2021, 5:43am UTC](https://awshq.com/t/best-way-to-secure-aws-api-gateway-from-public/12487/3 "2021-03-01T05:43:53Z")

</div>

You’re probably better off tying the Auth in the API to your users, rather than to the App itself. When a user signs in to your app (let’s say via Cognito or Auth0), then you can have a custom authoriser within API gateway that checks the validity of the credentials (the JWT token) and allows or blocks access.

---

<div class="post-metadata">

**Author:** ![DrFiveTheHiveMind](https://awshq.com/letter_avatar_proxy/v4/letter/d/d78d45/32.png) [@DrFiveTheHiveMind](https://awshq.com/u/DrFiveTheHiveMind)\
**Post date:** [March 1, 2021, 5:44am UTC](https://awshq.com/t/best-way-to-secure-aws-api-gateway-from-public/12487/4 "2021-03-01T05:44:58Z")

</div>

Three primary ways to secure your API only from your app are

- IAM (The Sigv4 you mentioned)
- Cognito
- Lambda Authorizer

IAM is usually best suited for AWS to AWS type situations so you’ll be better off with either Cognito or Lambda Auth since the use case is a mobile app.

If your using amplify to build your mobile app, it has quite a few native integrations with Cognito.

You setup cognito to manage your mobile users for you and then you tell API gateway to authorize your request by deferring to the cognito user pool you set up to your app.

If your not using cognito, you can use lambda custom authorizers. This is usually best if your using an identity provider like AuthO or Okta. Or if your managing your own traditional user database (not recommended nowadays). Essentially lambda will pull the JWT token from the Authorization header and then ask your identity provider “hey, is this a valid token?” If it is then your can tell API gateway that the request is good to go

---

<div class="post-metadata">

**Author:** ![DrFiveTheHiveMind](https://awshq.com/letter_avatar_proxy/v4/letter/d/d78d45/32.png) [@DrFiveTheHiveMind](https://awshq.com/u/DrFiveTheHiveMind)\
**Post date:** [March 1, 2021, 5:49am UTC](https://awshq.com/t/best-way-to-secure-aws-api-gateway-from-public/12487/5 "2021-03-01T05:49:44Z")

</div>

Confusing name, I know, but Private APIs are designed for when you need your API to be isolated to your local network (VPC). In this use case, mobile users shouldn’t have access to your VPC because we are hosting out API in a public fashion (mobile app users from around the world)

---

<div class="post-metadata">

**Author:** ![2fast2nick](https://awshq.com/letter_avatar_proxy/v4/letter/2/9e8a1a/32.png) [@2fast2nick](https://awshq.com/u/2fast2nick)\
**Post date:** [March 1, 2021, 5:54am UTC](https://awshq.com/t/best-way-to-secure-aws-api-gateway-from-public/12487/6 "2021-03-01T05:54:31Z")

</div>

You can put a custom authorizer on your api. Then setup your app to get an oauth token or whatever auth you decide

---

<div class="post-metadata">

**Author:** ![rendyfebry13](https://awshq.com/letter_avatar_proxy/v4/letter/r/8797f3/32.png) [@rendyfebry13](https://awshq.com/u/rendyfebry13)\
**Post date:** [March 1, 2021, 6:33am UTC](https://awshq.com/t/best-way-to-secure-aws-api-gateway-from-public/12487/7 "2021-03-01T06:33:42Z")

</div>

> [@thenickdude](#):
>
> This is impossible. Anything your mobile app can do can be done without the mobile app, by ripping the credentials out of your app. A really half-assed approach is to validate the User Agent header, because this will …

This. Tecnically you can’t verify if the request actually come from your app or not.

I would suggest to add cert pinning, so no one can sniff the request or put proxy between your app and your api. That way at least you make the cred leaking a bit harder.

---

<div class="post-metadata">

**Author:** ![aurelieus](https://awshq.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@aurelieus](https://awshq.com/u/aurelieus)\
**Post date:** [March 1, 2021, 6:54am UTC](https://awshq.com/t/best-way-to-secure-aws-api-gateway-from-public/12487/8 "2021-03-01T06:54:51Z")

</div>

> [@rendyfebry13](#):
>
> This. Tecnically you can’t verify if the request actually come from your app or not. I would suggest to add cert pinning, so no one can sniff the request or put proxy between your app and your api. That way at least you …

can you please eli5 cert pinning?

---

<div class="post-metadata">

**Author:** ![strike2counter](https://awshq.com/letter_avatar_proxy/v4/letter/s/ecb155/32.png) [@strike2counter](https://awshq.com/u/strike2counter)\
**Post date:** [March 1, 2021, 7:27am UTC](https://awshq.com/t/best-way-to-secure-aws-api-gateway-from-public/12487/9 "2021-03-01T07:27:48Z")

</div>

> [@DrFiveTheHiveMind](#):
>
> Three primary ways to secure your API only from your app are - IAM (The Sigv4 you mentioned) - Cognito - Lambda Authorizer IAM is usually best suited for AWS to AWS type situations so you’ll be better off with either …

You’re grammar is so jarring that your distracting from the content of you’re post.

(Yes /s)

---

<div class="post-metadata">

**Author:** ![fukitol](https://awshq.com/letter_avatar_proxy/v4/letter/f/b9bd4f/32.png) [@fukitol](https://awshq.com/u/fukitol)\
**Post date:** [March 1, 2021, 8:43am UTC](https://awshq.com/t/best-way-to-secure-aws-api-gateway-from-public/12487/10 "2021-03-01T08:43:18Z")

</div>

You’ll need some logic to perform the authentication for you, API Gateway won’t do it on its own. Check into [JWT and/or HMAC based API authorization](https://softwareengineering.stackexchange.com/questions/297417/rest-api-security-hmac-key-hashing-vs-jwt/297612#297612). But this still won’t stop someone particularly invested in hitting your API simply doing so with their own code.

---

<div class="post-metadata">

**Author:** ![morosis1982](https://awshq.com/letter_avatar_proxy/v4/letter/m/f08c70/32.png) [@morosis1982](https://awshq.com/u/morosis1982)\
**Post date:** [March 1, 2021, 11:06am UTC](https://awshq.com/t/best-way-to-secure-aws-api-gateway-from-public/12487/11 "2021-03-01T11:06:27Z")

</div>

The best way is to leave auth to a service that does proper, secure auth and validate your users auth token through that service. Cognito would be the relevant AWS service but you can use something else if that’s your preference.

At work we use Okta and use a custom authorizer to read the users token, verify it with the okta auth server, pull some user related data and continue on with the request.

Depending on your app and whether you want the user to sign in each time or ‘remember’ them you’ll have to figure out how to store a secure token that can be used to reauthenticate at will. That I do not know, yet.

This method can be used by Lambda@edge to secure web content served from S3 also. We have a web front end that gets secured this way.

We also use API keys for B2B, but that API also uses ip whitelisting as there are only a handful of consumers.

---

<div class="post-metadata">

**Author:** ![TheRealFender](https://awshq.com/letter_avatar_proxy/v4/letter/t/f04885/32.png) [@TheRealFender](https://awshq.com/u/TheRealFender)\
**Post date:** [March 1, 2021, 11:51am UTC](https://awshq.com/t/best-way-to-secure-aws-api-gateway-from-public/12487/12 "2021-03-01T11:51:50Z")

</div>

> [@fukitol-](#):
>
> You’ll need some logic to perform the authentication for you, API Gateway won’t do it on its own. Check into [JWT and/or HMAC based API …

HMAC won’t help here since the signing key could be ripped out of the mobile app.

---

<div class="post-metadata">

**Author:** ![ryankearney](https://awshq.com/letter_avatar_proxy/v4/letter/r/b3f665/32.png) [@ryankearney](https://awshq.com/u/ryankearney)\
**Post date:** [March 1, 2021, 1:34pm UTC](https://awshq.com/t/best-way-to-secure-aws-api-gateway-from-public/12487/13 "2021-03-01T13:34:18Z")

</div>

> [@aurelieus](#):
>
> can you please eli5 cert pinning?

It’s a half assed attempt to prevent someone from setting up a MITM proxy to capture the network traffic and inspect it.

It’s half assed because anyone with the app could just replace the pinned certificate or patch the code to ignore it.

Security through obscurity if you will.

---

<div class="post-metadata">

**Author:** ![ScottFoster90](https://awshq.com/letter_avatar_proxy/v4/letter/s/7ea924/32.png) [@ScottFoster90](https://awshq.com/u/ScottFoster90)\
**Post date:** [March 1, 2021, 2:32pm UTC](https://awshq.com/t/best-way-to-secure-aws-api-gateway-from-public/12487/14 "2021-03-01T14:32:03Z")

</div>

If you’re using a REST endpoint, you can create a private endpoint: [https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-private-apis.html](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-private-apis.html)

If you’re doing HTTP this doesn’t apply.

---

<div class="post-metadata">

**Author:** ![fukitol](https://awshq.com/letter_avatar_proxy/v4/letter/f/b9bd4f/32.png) [@fukitol](https://awshq.com/u/fukitol)\
**Post date:** [March 1, 2021, 2:57pm UTC](https://awshq.com/t/best-way-to-secure-aws-api-gateway-from-public/12487/15 "2021-03-01T14:57:55Z")

</div>

> [@TheRealFender](#):
>
> HMAC won’t help here since the signing key could be ripped out of the mobile app.

Yeah someone who’s really devoting their time to hit any public API will be able to do so. You can get rid of a lot of the low hanging fruit, but it’s less security and more a matter of inconveniencing/discouraging others that might want to use your APIs in their apps.

---

<div class="post-metadata">

**Author:** ![brozium](https://awshq.com/letter_avatar_proxy/v4/letter/b/a5b964/32.png) [@brozium](https://awshq.com/u/brozium)\
**Post date:** [March 1, 2021, 3:25pm UTC](https://awshq.com/t/best-way-to-secure-aws-api-gateway-from-public/12487/16 "2021-03-01T15:25:53Z")

</div>

> [@thenickdude](#):
>
> This is impossible. Anything your mobile app can do can be done without the mobile app, by ripping the credentials out of your app. A really half-assed approach is to validate the User Agent header, because this will …

I haven’t used it personally but shouldn’t it be possible to use Google’s SafetyNet and Apple’s DeviceCheck to validate that the requests are coming from real devices (in conjunction with any other methods)?

---

<div class="post-metadata">

**Author:** ![im-a-smith](https://awshq.com/letter_avatar_proxy/v4/letter/i/e47774/32.png) [@im-a-smith](https://awshq.com/u/im-a-smith)\
**Post date:** [March 1, 2021, 3:31pm UTC](https://awshq.com/t/best-way-to-secure-aws-api-gateway-from-public/12487/17 "2021-03-01T15:31:46Z")

</div>

You’ll want to do a few things:

1. You need to authenticate your users first before anything else. You’ll want to put a WAF in front and use that to rate limit and prevent credential stuffing

2. You’ll want to issue your users some kind of token, a shared/private secret is one route to go (HMAC signing headers), use that to validate the users request to private APIs—again using WAF to protect the resource from rate limit issues

---

<div class="post-metadata">

**Author:** ![brunokktro](https://awshq.com/letter_avatar_proxy/v4/letter/b/e0b2c6/32.png) [@brunokktro](https://awshq.com/u/brunokktro)\
**Post date:** [March 2, 2021, 12:25am UTC](https://awshq.com/t/best-way-to-secure-aws-api-gateway-from-public/12487/18 "2021-03-02T00:25:35Z")

</div>

Amazon Cognito + AWS Shield + AWS WAF

---

<div class="post-metadata">

**Author:** ![cysronald](https://awshq.com/letter_avatar_proxy/v4/letter/c/779978/32.png) [@cysronald](https://awshq.com/u/cysronald)\
**Post date:** [March 2, 2021, 2:46am UTC](https://awshq.com/t/best-way-to-secure-aws-api-gateway-from-public/12487/19 "2021-03-02T02:46:32Z")

</div>

Hi everyone,

thank you for the overwhelming help,  
will look through each of your suggestions and check the best options for our case.
