-
Notifications
You must be signed in to change notification settings - Fork 104
docs(deployments): add Vercel deployment guide (#390) #605
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
Open
echonesis
wants to merge
5
commits into
apache:main
Choose a base branch
from
echonesis:vercel-deploy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| <!-- | ||
| Licensed to the Apache Software Foundation (ASF) under one | ||
| or more contributor license agreements. See the NOTICE file | ||
| distributed with this work for additional information | ||
| regarding copyright ownership. The ASF licenses this file | ||
| to you under the Apache License, Version 2.0 (the | ||
| "License"); you may not use this file except in compliance | ||
| with the License. You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, | ||
| software distributed under the License is distributed on an | ||
| "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| KIND, either express or implied. See the License for the | ||
| specific language governing permissions and limitations | ||
| under the License. | ||
| --> | ||
|
|
||
| # Deploy Burr in Vercel | ||
|
|
||
| [Vercel](https://vercel.com/) - serverless platform for frontend frameworks and serverless functions. | ||
|
|
||
| Here we have an example of how to deploy a Burr application as a Vercel Serverless Function. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - **Node.js**: Required for Vercel CLI (v14 or higher) | ||
| - **Vercel Account**: Sign up at [vercel.com](https://vercel.com/signup) (free tier available) | ||
|
|
||
| ## Step-by-Step Guide | ||
|
|
||
| ### 1. Install Vercel CLI: | ||
|
|
||
| ```bash | ||
| npm install -g vercel | ||
| ``` | ||
|
|
||
| ### 2. Local tests: | ||
|
|
||
| Start the local development server: | ||
|
|
||
| ```bash | ||
| vercel dev | ||
| ``` | ||
|
|
||
| Send test request to check if the function executes correctly: | ||
|
|
||
| ```bash | ||
| curl -X POST "http://localhost:3000/api/counter" \ | ||
| -H "Content-Type: application/json" \ | ||
| -d '{"number": 5}' | ||
| ``` | ||
|
|
||
| Expected response: | ||
|
|
||
| ```json | ||
| {"counter": 5, "counter_limit": 5, "__SEQUENCE_ID": 5, "__PRIOR_STEP": "result"} | ||
| ``` | ||
|
|
||
| ### 3. Login to Vercel: | ||
|
|
||
| ```bash | ||
| vercel login | ||
| ``` | ||
|
|
||
| This will open your browser to authenticate with your Vercel account. | ||
|
|
||
| ### 4. Deploy to Vercel (Preview): | ||
|
|
||
| Deploy to a preview environment for testing: | ||
|
|
||
| ```bash | ||
| vercel | ||
| ``` | ||
|
|
||
| ### 5. Test Preview Deployment: | ||
|
|
||
| Vercel will provide a preview URL. Test it: | ||
|
|
||
| ```bash | ||
| curl -X POST "https://your-project-xxx.vercel.app/api/counter" \ | ||
| -H "Content-Type: application/json" \ | ||
| -d '{"number": 5}' | ||
| ``` | ||
|
|
||
| ### 6. Deploy to Production: | ||
|
|
||
| Once preview testing is successful, deploy to production: | ||
|
|
||
| ```bash | ||
| vercel --prod | ||
| ``` | ||
|
|
||
| Your production URL will be: | ||
|
|
||
| ``` | ||
| https://your-project.vercel.app | ||
| ``` | ||
|
|
||
| ### 7. Test Production Deployment: | ||
|
|
||
| ```bash | ||
| curl -X POST "https://your-project.vercel.app/api/counter" \ | ||
| -H "Content-Type: application/json" \ | ||
| -d '{"number": 5}' | ||
| ``` | ||
|
|
||
| ## Alternative: Deploy via Git Integration (Recommended) | ||
|
|
||
| ### Import project in Vercel Dashboard: | ||
|
|
||
| - Go to https://vercel.com/new | ||
| - Click "Import Git Repository" | ||
| - Select your repository | ||
| - Click "Deploy" | ||
|
|
||
| ## Troubleshooting | ||
|
|
||
| ### If deployment fails: | ||
|
|
||
| View detailed logs: | ||
|
|
||
| ```bash | ||
| vercel logs --follow | ||
| ``` | ||
|
|
||
| ### If function returns 404: | ||
|
|
||
| Ensure your handler file is in the `api/` directory with correct format. | ||
|
|
||
| ### If you see deployment URL instead of production URL: | ||
|
|
||
| The production URL is always in the format: `https://your-project.vercel.app` | ||
|
|
||
| Check your Vercel Dashboard → Domains section for the correct URL. | ||
|
|
||
| ## Resources | ||
|
|
||
| - [Vercel Documentation](https://vercel.com/docs) | ||
| - [Vercel Python Runtime](https://vercel.com/docs/functions/runtimes/python) | ||
| - [Vercel CLI Reference](https://vercel.com/docs/cli) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| """ | ||
| Vercel Serverless Function for counter application | ||
| Endpoint: /api/counter | ||
| """ | ||
| from http.server import BaseHTTPRequestHandler | ||
| import json | ||
| from app import counter_app | ||
|
|
||
|
|
||
| class handler(BaseHTTPRequestHandler): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there a way to stream results back? doesn't have to be this commit, but I think it's a good way to show case something happening. |
||
| """Vercel Serverless Function handler. | ||
|
|
||
| This class handles HTTP requests for the counter API endpoint. | ||
| Must inherit from BaseHTTPRequestHandler to work with Vercel's Python runtime. | ||
| """ | ||
|
|
||
| def do_POST(self): | ||
| """Handle POST requests to increment counter. | ||
|
|
||
| Expects JSON body with 'number' field indicating count limit. | ||
| Returns serialized application state on success. | ||
| """ | ||
| try: | ||
| # Read request body | ||
| content_length = int(self.headers.get('Content-Length', 0)) | ||
| body = self.rfile.read(content_length) | ||
|
|
||
| # Parse JSON payload | ||
| data = json.loads(body.decode('utf-8')) | ||
|
|
||
| # Extract parameter (equivalent to Lambda's event["body"]["number"]) | ||
| count_up_to = int(data.get("number", 0)) | ||
|
|
||
| # Validate input | ||
| if count_up_to <= 0: | ||
| self.send_error_response(400, "number must be greater than 0") | ||
| return | ||
|
|
||
| # Execute business logic (identical to Lambda implementation) | ||
| app = counter_app.application(count_up_to) | ||
| action, result, state = app.run(halt_after=["result"]) | ||
|
|
||
| # Return success response with serialized state | ||
| self.send_json_response(200, state.serialize()) | ||
|
|
||
| except json.JSONDecodeError: | ||
| self.send_error_response(400, "Invalid JSON format") | ||
| except ValueError as e: | ||
| self.send_error_response(400, f"Invalid number format: {str(e)}") | ||
| except KeyError as e: | ||
| self.send_error_response(400, f"Missing required field: {str(e)}") | ||
| except Exception as e: | ||
| # Log error for debugging | ||
| print(f"Error in counter handler: {str(e)}") | ||
| import traceback | ||
| traceback.print_exc() | ||
| self.send_error_response(500, "Internal server error") | ||
|
|
||
| def do_GET(self): | ||
| """Handle GET requests - not allowed. | ||
|
|
||
| Returns 405 Method Not Allowed error. | ||
| """ | ||
| self.send_error_response(405, "Only POST method is allowed") | ||
|
|
||
| def do_PUT(self): | ||
| """Handle PUT requests - not allowed. | ||
|
|
||
| Returns 405 Method Not Allowed error. | ||
| """ | ||
| self.send_error_response(405, "Only POST method is allowed") | ||
|
|
||
| def do_DELETE(self): | ||
| """Handle DELETE requests - not allowed. | ||
|
|
||
| Returns 405 Method Not Allowed error. | ||
| """ | ||
| self.send_error_response(405, "Only POST method is allowed") | ||
|
|
||
| def send_json_response(self, status_code, data): | ||
| """Send JSON response to client. | ||
|
|
||
| Args: | ||
| status_code: HTTP status code | ||
| data: Response data (dict, list, or any JSON-serializable object) | ||
| """ | ||
| self.send_response(status_code) | ||
| self.send_header('Content-Type', 'application/json') | ||
| self.end_headers() | ||
|
|
||
| if isinstance(data, (dict, list)): | ||
| response_body = json.dumps(data, ensure_ascii=False) | ||
| else: | ||
| response_body = str(data) | ||
|
|
||
| self.wfile.write(response_body.encode('utf-8')) | ||
|
|
||
| def send_error_response(self, status_code, message): | ||
| """Send error response to client. | ||
|
|
||
| Args: | ||
| status_code: HTTP error status code | ||
| message: Error message string to include in response body | ||
| """ | ||
| self.send_response(status_code) | ||
| self.send_header('Content-Type', 'application/json') | ||
| self.end_headers() | ||
|
|
||
| error_body = json.dumps({'error': message}) | ||
| self.wfile.write(error_body.encode('utf-8')) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| """ | ||
| This is a very simple counting application. | ||
|
|
||
| It's here to help you get the mechanics of deploying a Burr application to AWS Lambda. | ||
| """ | ||
|
|
||
| import time | ||
|
|
||
| import burr.core | ||
| from burr.core import Application, Result, State, default, expr | ||
| from burr.core.action import action | ||
| from burr.core.graph import GraphBuilder | ||
|
|
||
|
|
||
| @action(reads=["counter"], writes=["counter"]) | ||
| def counter(state: State) -> State: | ||
| result = {"counter": state["counter"] + 1} | ||
| time.sleep(0.5) # sleep to simulate a longer running function | ||
| return state.update(**result) | ||
|
|
||
|
|
||
| # our graph. | ||
| graph = ( | ||
| GraphBuilder() | ||
| .with_actions(counter=counter, result=Result("counter")) | ||
| .with_transitions( | ||
| ("counter", "counter", expr("counter < counter_limit")), | ||
| ("counter", "result", default), | ||
| ) | ||
| .build() | ||
| ) | ||
|
|
||
|
|
||
| def application(count_up_to: int = 10) -> Application: | ||
| """function to return a burr application""" | ||
| return ( | ||
| burr.core.ApplicationBuilder() | ||
| .with_graph(graph) | ||
| .with_state(**{"counter": 0, "counter_limit": count_up_to}) | ||
| .with_entrypoint("counter") | ||
| .build() | ||
| ) | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| burr | ||
| # for tracking you'd add extra dependencies |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you add to the docs/ as well a link to this -- i.e. edit this page
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I will update it in the next commit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cool, I think that's all that's really needed for this to be ready.