- Newest
- Most votes
- Most comments
Yes, it is possible to create an IAM policy to deny access to specific routes in a WebSocket API Gateway while still allowing users to connect and use other routes. However, the ARN format you're using for WebSocket APIs is slightly different from what's required.
For WebSocket APIs, the ARN format for routes is:
arn:aws:execute-api:region:account-id:api-id/stage-name/route-key
In your case, to deny access to the admin route, you would use:
arn:aws:execute-api:REGION:ACCOUNT-ID:API-ID/STAGE/admin
Here's an example of an IAM policy that would deny access to the admin route while still allowing access to other routes:
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "execute-api:Invoke",
"Resource": [
"arn:aws:execute-api:REGION:ACCOUNT-ID:API-ID/STAGE/$connect",
"arn:aws:execute-api:REGION:ACCOUNT-ID:API-ID/STAGE/$disconnect",
"arn:aws:execute-api:REGION:ACCOUNT-ID:API-ID/STAGE/$default"
]
},
{
"Effect": "Deny",
"Action": "execute-api:Invoke",
"Resource": "arn:aws:execute-api:REGION:ACCOUNT-ID:API-ID/STAGE/admin"
}
]
}
This policy explicitly allows access to the $connect, $disconnect, and $default routes, while denying access to the admin route. The "Deny" statement takes precedence over any "Allow" statements, ensuring that access to the admin route is blocked.
Remember to replace REGION, ACCOUNT-ID, API-ID, and STAGE with your actual values. Also, ensure that IAM authorization is enabled for your WebSocket API routes.
If you're still experiencing issues, double-check that your route selection expression is working correctly and that the admin route is indeed being triggered when the message contains {"group":"admin"}. You might also want to enable CloudWatch logs for your API to help troubleshoot any unexpected behavior.
Sources
Control access to WebSocket APIs with IAM authorization - Amazon API Gateway
Control and manage access to WebSocket APIs in API Gateway - Amazon API Gateway
Relevant content
- AWS OFFICIALUpdated 2 years ago

This did absolutely not work. It's not even in the right format.
I cannot put "arn:aws:execute-api:REGION:ACCOUNT-ID:API-ID/STAGE/admin" as the resource - it shows an error in the permissions editor
Resource ARN does not match the expected ARN format. Update the resource portion of the ARN.the closest I can do is "arn:aws:execute-api:REGION:ACCOUNT-ID:API-ID/*/admin
but that does not seem to be doing anything, i.e. i can still access /admin