amplify-cli: API Gateway CORS is blocking localhost access
Before opening, please confirm:
- I have installed the latest version of the Amplify CLI (see above), and confirmed that the issue still persists.
- I have searched for duplicate or closed issues.
- I have read the guide for submitting bug reports.
- I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.
How did you install the Amplify CLI?
npm i -g @aws-amplify/cli
If applicable, what version of Node.js are you using?
16.6.0
Amplify CLI Version
5.3.0
What operating system are you using?
macOS 11.5.2
Amplify Categories
api
Amplify Commands
add, push, update
Describe the bug
After running amplify update API and adding access restrictions to the API endpoint for only authenticated users, I can no longer access the lambda function from my localhost.
After running this:

This happens:

Expected behavior
I would expect that place to edit the CORS policies to be obvious or easier to find. I’ve searched the AWS API Gateway console but I found nothing related to CORS policies anywhere.
Reproduction steps
- Run
amplify ad apiand add some lambda function - Refer to the “Describe the bug section” and run that command
GraphQL schema(s)
# Put schemas below this line
Log output
# Put your logs below this line
Additional information
No response
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 47 (14 by maintainers)
Hey y’all 👋 after restricting access to the API route, I was able to add the CORS headers (or uncomment what’s provided from the templates) to mitigate this issue
and my sample app.js file in a Next.js app making the call with authentication
I had the same error and checked the Lambda logs, turns out the lambda was running out of memory. So I increased the memory and timeout and it solved the issue! Also enabling CORS through API Gateway console didn’t solve the issue for some reason, but enabling CORS in the lambda code helped solve the issue :
Hope this helps anybody struggling with the same issue!
thanks @RoniqueRicketts, I followed your trail and solved my CORS issue, which suddenly appeared after adding groups and cognito in the identity pool
solved this in the IAM ui
amplify-xxxxxxxxx-xxxxxxxxx-xxxxxxxxx-authRolewhich was created by amplify, granting access to each route by method and resourceFor lack of a better option, I resolved this issue by redeploying my backend infrastructure from scratch.
Here’s what I did:
amplifyfolder and theaws-exports.jsfileproductionenvironment from Amplify’s Admin UI (hosting, auth, API, and functions are gone)stagingand you can’t change that)amplify pull --appId xxxxxxxx --envName productionon the root of my repo to get access to that backend from the CLIamplify env add productionto add a backend environment calledproductionamplify env remove stagingto remove the default backend environmentstaging(unfortunately one has to do this gymnastic because you can’t change the default environment name)amplify pushto save changes to the cloudusernamefield on my frontend)amplify add apiallowing access only to authorized usersamplify pushto save changesNow I can only hit my API from logged-in users ✅
My request was getting blocked with a 403 Error code. My requests were getting block at the api gateway level and I was unable to hit my lambda functions associated with the API. The reason behind this failure was that I had added users groups to my Cognito Userpool before running
amplify add api. When user groups are added user roles are also added but no policies are associated with said user role. My suggestion would be to update any user Role policy with the permission to access the api-gateway service when we runamplify add apito a project that already has cognito userpools added.Hey @andrecasal yes I used
amplify add apiand added a GET and POST route.add the API with an unsecured route to confirm no CORS issues
secure the route
modify the Lambda handler to include CORS headers
add a POST route
modify the Lambda handler to respond to POST requests and include CORS headers
run
amplify pushin my frontend Next.js _app.js file:
code
run the Next.js development server with
yarn nextand authenticateWhat is the status code you’re receiving on the failed POST call?
@RoniqueRicketts thanks for the offer to help. I haven’t created groups to my cognito, so it would be a surprise I would require an update to any groups.
My problem was solved literally by removing body:{} from the API call options. This is a call which works against GET paths created with earlier amplify-cli version:
And this is an API call which works post-4.50.2 amplify-cli created API paths:
Using body as part of the GET call obviously is not according to the HTTP definition, but I just happend to use it to move some telemetry data from the app to the backend… Headers themselves have a limit (4-8Kb) depending on webserver of choice and its config) how much data you can transfer and for certain use cases the body is better place to transfer big telemetry payloads.
@pepso as per the information sent by @jasonhargrove above you’ll have to update the API to the groups you make. I surely believe the documentation should update on every update with beginner friendly content. That could minimize a lot of issue that I’m seeing on here. Right now there is also an issue with the API getting
custom_headersbut the documentation suggests how to send them but the information is incorrect as it doesn’t match the actual reaction of the API on API Gateway.I fixed my issue and identified a breaking change in the way how amplify handles api calls/paths between versions:
Background: In my web-app, every API call to my endpoints contain couple of variables in the body of the call (where I transfer some metrics about the user experience). In my backend I parse them with middleware using app.use(metricParsers()).
I found out that there is a change API paths created pre-4.50.2, GET calls with “body” go through API Gatway API paths created post-4.50.2, GET calls fail as CORS error if body is defined as part of the paylod
Basically I conclude that API Gateway configuration have become more strict with API updates in past couple of months.
@josefaidt I strongly suggest this is not a documentation issue, but real technical problem with amplify-cli. Do I create a new issue, or can you reopen this and triage further as per my below details?
Please keep in mind every other API path in the same API Gateway end point continue to work, and be invoked as expected (created and deployed pre-4.50 amplify-cli). This impacts only the new paths created since 4.50.2 upgrade (which was supposed to fix the API migration issues).
I don’t believe this to be documentation only issue, and it got to involve at least newly created out of box api path into an existing api + lambda walkthroughs update so the codegen works as expected.
Please advise what needs to be done to fix the disconnect between amplicy-cli created templates to working API path to Lambda invokes without CORS.
not sure but did not seem to matter which order I added auth or api as reported, so I dug a little further
just now solved this with the cli, which is probably the intended flow
after adding groups
amplify api updatechooseRESTselect the api and path and follow the flow until you get to
restrict API accessselect
restrict access by both(auth and group)select
auth users onlyand methodselect group(s)
it will then ask you to confirm the method for each group
amplify push
confirm it added the policy to the group(s) in the flow via IAM ui
ensure test user is in an impacted group
sign out
sign in, confirm access to resource
Error 403
I think @RoniqueRicketts and me have the exact same problem.
I have the same issue I am on the same version on Amplify (the same issue was on the previous version also). I am on Windows 10 OS. What I found out for this issue so far: