Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Serverless Land Pattern waf-cloudfront-websocketapi-serverless #2560

Conversation

kothsidh
Copy link
Contributor

Issue #, if available:

Description of changes:
Created a serverless land pattern that implements a secure WebSocket API using AWS CDK, integrating CloudFront for distribution and WAF for protection through AWS CDK with Python. It makes use of API keys to ensure that the Websocket endpoint can only be accessed via the CloudFront distribution by passing the API key as custom header from CloudFront.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Comment on lines 21 to 22
git clone <repository-url>
cd <repository-directory>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please put actual URL and path?

Comment on lines 1 to 15
# Protecting WebSocket API with CloudFront and WAF Integration

This pattern implements a secure WebSocket API using AWS CDK, integrating CloudFront for distribution and WAF for protection through AWS CDK with Python. It makes use of API keys to ensure that the Websocket endpoint can only be accessed via the CloudFront distribution by passing the API key as custom header from CloudFront.

The WebSocket API provides real-time communication capabilities, while CloudFront ensures low-latency content delivery. The Web Application Firewall (WAF) adds an extra layer of security by protecting against common web exploits and controlling access based on configurable rules.

![Alt text](images/architecturediagram.png?raw=true "Architecture Diagram for WebSocket API with CloudFront and WAF Integration")

### Prerequisites

- Python 3.9 or later
- AWS CDK CLI
- AWS CLI configured with appropriate credentials

***Please note that AWS WAF is available globally for CloudFront distributions. So you must use the Region us-east-1 region while deploying the stack (N. Virginia)***
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please refer other PRs to see template for README. It is recommended to put path for to-be serverlessland pattern in description.

Comment on lines 71 to 85
## Data Flow

The WebSocket API handles data flow as follows:

1. Client initiates a WebSocket connection to the CloudFront distribution URL.
2. CloudFront forwards the request to the API Gateway WebSocket API with the "x-api-key" as custom header.
3. Websocket API validates the API key and routes the request based on the route selection expression.
4. The Lambda function is invoked to handle the WebSocket event.

```
[Client] <-> [CloudFront] <-> [API Gateway WebSocket API] <-> [Lambda Function]
^ |
| |
+---------------------------------------------------------------+
```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally optional but if possible I'd recommend to add a flow diagram for explanation

@julianwood julianwood merged commit 830d8d0 into aws-samples:main Jan 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants