Heroku Deployment with SFAI SDK¶
This notebook demonstrates how to deploy applications to Heroku using the SFAI SDK. Heroku deployment is perfect for production applications that need to integrate with the Salesforce ecosystem.
Overview¶
Heroku deployment allows you to deploy your application to Salesforce's preferred cloud platform. The platform automatically:
- Creates and manages Heroku apps with team and private space support
- Supports both container and buildpack deployment types
- Provides enterprise features like private spaces and internal routing
- Integrates seamlessly with Salesforce ecosystem
Prerequisites¶
- Heroku CLI installed and configured
- SFAI SDK installed (
pip install sfai
) - Git repository initialized in your application directory
- Valid Heroku account with appropriate team access
- For container deployment: Dockerfile in your application directory
- For buildpack deployment: requirements.txt or appropriate buildpack files
# Initialize the app (this creates the basic app context)
!sfai app init
Step 2: Initialize Heroku Platform¶
Configure the Heroku platform with your team, private space, and deployment preferences.
# Initialize Heroku platform with container deployment
!sfai platform init --cloud heroku --deployment-type container --team-name your-team-name --private-space your-private-space
Step 3: Deploy to Heroku¶
Deploy your application to the configured Heroku environment.
# Deploy the application to Heroku
!sfai app deploy
Step 4: Open Application in Browser¶
Access your deployed application.
# Open the application in your browser
!sfai app open
Step 5: View Application Logs¶
Monitor your application's logs for debugging and monitoring.
# View application logs
!sfai app logs
Step 6: Check Application Status¶
Check the current status of your Heroku application.
# Check application status
!sfai app status
Step 7: Clean Up (Optional)¶
Remove the application when you're done.
# Delete the application (optional)
!sfai app delete
Method 2: Python API Workflow¶
You can also manage the entire deployment process using the Python API for more programmatic control.
Step 1: Initialize Platform¶
from sfai.platform import init
# Initialize Heroku platform with container deployment
result = init(
cloud="heroku",
deployment_type="container", # or "buildpack"
team_name="your-team-name",
private_space="your-private-space",
routing="public" # or "internal"
)
print(f"Platform initialization: {result}")
Step 2: Deploy Application¶
from sfai.app import deploy
# Deploy the application
result = deploy()
print(f"Deployment result: {result}")
Step 3: Open Application¶
from sfai.app import open_app
# Open the application
result = open_app(path="/docs") # Opens the docs endpoint
print(f"App opened: {result}")
Step 4: Check Status and Logs¶
from sfai.app import status, logs
# Check application status
status_result = status()
print(f"Status: {status_result}")
# View logs
logs_result = logs()
print(f"Logs: {logs_result}")
Step 5: Clean Up¶
from sfai.app import delete
# Delete the application (optional)
result = delete()
print(f"Deletion result: {result}")
Complete Python Example¶
Here's a complete function that demonstrates the entire Heroku deployment workflow:
def complete_heroku_deployment_workflow(
team_name="your-team-name",
private_space="your-private-space",
deployment_type="container",
routing="public"
):
"""
Complete Heroku deployment workflow using SFAI SDK.
Args:
team_name: Heroku team name
private_space: Heroku private space name
deployment_type: "container" or "buildpack"
routing: "public" or "internal"
"""
from sfai.platform import init
from sfai.app import deploy, open_app, status, logs
try:
# Step 1: Initialize platform
print("🚀 Initializing Heroku platform...")
init_result = init(
cloud="heroku",
deployment_type=deployment_type,
team_name=team_name,
private_space=private_space,
routing=routing
)
print(f"✅ Platform initialized: {init_result}")
# Step 2: Deploy application
print("🚀 Deploying application...")
deploy_result = deploy()
print(f"✅ Application deployed: {deploy_result}")
# Step 3: Check status
print("📊 Checking application status...")
status_result = status()
print(f"✅ Status checked: {status_result}")
# Step 4: Open application
print("🌐 Opening application...")
open_result = open_app(path="/docs")
print(f"✅ Application opened: {open_result}")
print("\n🎉 Heroku deployment completed successfully!")
return {
"init": init_result,
"deploy": deploy_result,
"status": status_result,
"open": open_result
}
except Exception as e:
print(f"❌ Error during deployment: {e}")
return {"error": str(e)}
# Example usage:
# result = complete_heroku_deployment_workflow(
# team_name="det-ai-platform",
# private_space="det-ai-platform-sandbox",
# deployment_type="container",
# routing="public"
# )
Deployment Types¶
The SFAI SDK supports two Heroku deployment types:
1. Container Deployment¶
- Uses Docker containers
- Requires a
Dockerfile
in your project root - More control over the runtime environment
- Better for complex applications with specific dependencies
2. Buildpack Deployment¶
- Uses Heroku's buildpack system
- Requires
requirements.txt
(for Python) or appropriate language files - Automatic dependency management
- Simpler for standard applications
Common Commands Reference¶
CLI Commands¶
# Initialize app
sfai app init
# Initialize Heroku platform
sfai platform init --cloud heroku --deployment-type container --team-name your-team --private-space your-space
# Deploy to Heroku
sfai app deploy
# Open application
sfai app open
# Check status
sfai app status
# View logs
sfai app logs
# Delete application
sfai app delete
Python API¶
from sfai.platform import init
from sfai.app import deploy, open_app, status, logs, delete
# Initialize platform
init(cloud="heroku", deployment_type="container", team_name="your-team")
# Deploy
deploy()
# Open
open_app(path="/docs")
# Status and logs
status()
logs()
# Delete
delete()
Configuration Options¶
Platform Initialization Parameters¶
cloud
: Always "heroku" for Heroku deploymentdeployment_type
: "container" or "buildpack"team_name
: Your Heroku team name (required for enterprise)private_space
: Heroku private space name (optional)routing
: "public" or "internal" (for private spaces)app_name
: Custom app name (optional, auto-generated if not provided)
Example Configurations¶
Enterprise Setup with Private Space¶
init(
cloud="heroku",
deployment_type="container",
team_name="det-ai-platform",
private_space="det-ai-platform-sandbox",
routing="internal"
)
Simple Public Deployment¶
init(
cloud="heroku",
deployment_type="buildpack",
team_name="your-team",
routing="public"
)
Troubleshooting¶
Common Issues and Solutions¶
1. Heroku CLI Not Found¶
Error: Heroku CLI not installed
Solution: Install Heroku CLI:
# macOS
brew install heroku/brew/heroku
# Or download from https://devcenter.heroku.com/articles/heroku-cli
2. Authentication Issues¶
Error: Heroku login failed
Solution: Login to Heroku:
heroku login
3. App Name Already Taken¶
Error: name is already taken
Solution: The SDK automatically generates a unique suffix. If this persists, specify a custom app name:
init(cloud="heroku", app_name="my-unique-app-name")
4. Team Access Issues¶
Error: forbidden
or team access errors Solution: Ensure you have access to the specified team and private space:
heroku teams
heroku spaces --team your-team-name
5. Container Deployment Issues¶
Error: Dockerfile not found or container build fails Solution:
- Ensure
Dockerfile
exists in your project root - Test Docker build locally:
docker build -t test-app .
- Check Dockerfile syntax and dependencies
6. Git Repository Issues¶
Error: not a git repository
or not a Heroku repository
Solution:
- Initialize git:
git init
- For buildpack deployment, ensure Heroku remote is added
- Check git remotes:
git remote -v
7. Deployment Timeouts¶
Error: Deployment takes too long or times out Solution:
- Check application logs:
sfai app logs
- Optimize Docker build (use multi-stage builds, .dockerignore)
- Ensure application starts within Heroku's timeout limits
Best Practices¶
1. Environment Management¶
- Use environment variables for configuration
- Store secrets in Heroku config vars
- Use different apps for different environments (dev, staging, prod)
2. Container Optimization¶
- Use multi-stage Docker builds
- Include
.dockerignore
to reduce build context - Use appropriate base images (slim versions)
3. Security¶
- Use private spaces for sensitive applications
- Configure internal routing for backend services
- Regularly update dependencies
4. Monitoring¶
- Regularly check application logs
- Monitor application performance
- Set up alerts for critical issues
5. Deployment¶
- Test deployments in staging first
- Use meaningful commit messages
- Keep deployments small and frequent
Next Steps¶
After successfully deploying to Heroku, you can:
- Scale Your Application: Use Heroku's scaling features to handle more traffic
- Add Add-ons: Integrate databases, monitoring, and other services
- Set Up CI/CD: Automate deployments with GitHub integration
- Monitor Performance: Use Heroku metrics and logging
- Integrate with Salesforce: Connect your app to Salesforce services
- Publish to MuleSoft: Use SFAI SDK to publish APIs to MuleSoft
Related Documentation¶
Support¶
For issues or questions:
- Check the troubleshooting section above
- Review application logs:
sfai app logs
- Consult the SFAI SDK documentation
- Contact your team's DevOps or platform team