Reusable CDKTF Infrastructure Library for Google Cloud Platform
Project description
๐๏ธ x-infra-kit
Reusable CDKTF Infrastructure Library for Google Cloud Platform
Production-ready infrastructure constructs that enable you to deploy GCP resources with a single line of code.
๐ Table of Contents
- Features
- Architecture
- Quick Start
- Installation
- Usage
- Configuration
- API Reference
- Examples
- Testing
- Contributing
โจ Features
| Feature | Description |
|---|---|
| Single-Line Deployment | Deploy complete GCP infrastructure with StandardPlatform |
| Golden Path Profiles | Pre-configured Dev, Staging, and Prod environments |
| Override Support | Customize any parameter while keeping sensible defaults |
| Type Safety | Full type hints and dataclass validation |
| Best Practices | Private clusters, Workload Identity, VPC-native networking |
| Modular Design | Use composite or individual building blocks |
๐๏ธ Architecture
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ StandardPlatform โ
โ (Composite Construct - Recommended for most users) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ Profile โ โ Profile โ โ Profile โ โ
โ โ DevProfile โ โStagingProfileโ โ ProdProfile โ โ
โ โโโโโโโโฌโโโโโโโ โโโโโโโโฌโโโโโโโ โโโโโโโโโโโโฌโโโโโโโโโโโ โ
โ โ โ โ โ
โ โโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ
โ โผ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ NetworkConfig / ClusterConfig โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ
โ โโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโ โ
โ โผ โผ โผ โ
โ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โ
โ โ StandardVPC โ โStandardClusterโ โStandardSecretsโ โ
โ โ โ โ โ โ(optional) โ โ
โ โ โข VPC โ โ โข GKE โ โ โ โ
โ โ โข Subnet โ โ โข Node Pool โ โโโโโโโโโโโโโโโค โ
โ โ โข Router โ โ โ โStandardIdentityโ โ
โ โ โข NAT โ โ โ โ(optional) โ โ
โ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
GCP Resources Created
| Resource | Description |
|---|---|
google_compute_network |
Custom VPC |
google_compute_subnetwork |
Subnet with secondary ranges |
google_compute_router |
Cloud Router for NAT |
google_compute_router_nat |
Cloud NAT for private egress |
google_container_cluster |
Private GKE cluster |
google_container_node_pool |
Managed node pool with autoscaling |
google_secret_manager_secret |
Secret Manager secrets (optional) |
google_service_account |
Workload Identity SA (optional) |
google_project_iam_member |
IAM role bindings (optional) |
google_service_account_iam_binding |
Workload Identity binding (optional) |
๐ Quick Start
1. Install the library
pip install -e .
# or
pip install x-infra-kit # when published to PyPI
2. Create your stack
from cdktf import App, TerraformStack
from cdktf_cdktf_provider_google.provider import GoogleProvider
from infrastructure_lib import StandardPlatform
class MyStack(TerraformStack):
def __init__(self, scope, id):
super().__init__(scope, id)
GoogleProvider(self, "Google",
project="my-gcp-project",
region="europe-west1"
)
# ๐ One line to create complete infrastructure!
platform = StandardPlatform(self, "platform",
project_id="my-gcp-project",
region="europe-west1",
env="prod",
prefix="myapp"
)
app = App()
MyStack(app, "my-stack")
app.synth()
3. Deploy
cdktf synth # Generate Terraform JSON
cdktf deploy # Apply to GCP
๐ฆ Installation
Prerequisites
- Python 3.9+
- Node.js 16+ (for cdktf-cli)
- Google Cloud SDK (
gcloud) - Terraform 1.0+ (installed automatically by cdktf)
Install from source
git clone https://github.com/your-org/x-infra-kit.git
cd x-infra-kit
# Create virtual environment
python -m venv venv
source venv/bin/activate # Linux/Mac
# or
.\venv\Scripts\activate # Windows
# Install dependencies
pip install -r requirements.txt
Install as package
pip install -e .
๐ Usage
StandardPlatform (Recommended)
The easiest way to use the library. Creates VPC, GKE, and optionally Secrets and Workload Identity.
from infrastructure_lib import StandardPlatform
# Minimal usage (just 4 required parameters)
platform = StandardPlatform(stack, "platform",
project_id="my-gcp-project",
region="europe-west1",
env="dev",
prefix="myapp"
)
# Access created resources
print(platform.cluster_name) # myapp-dev-cluster
print(platform.cluster_endpoint) # https://...
print(platform.network_id) # projects/.../networks/myapp-dev-vpc
print(platform.subnet_id) # projects/.../subnetworks/myapp-dev-subnet
With Secrets and Workload Identity
platform = StandardPlatform(stack, "platform",
project_id="my-gcp-project",
region="europe-west1",
env="prod",
prefix="myapp",
# Optional: Create secrets
secret_ids=["db-password", "api-key", "jwt-secret"],
# Optional: Setup Workload Identity
workload_identity={
"sa_id": "external-secrets-sa",
"k8s_namespace": "external-secrets",
"k8s_sa_name": "external-secrets",
"roles": ["roles/secretmanager.secretAccessor"]
}
)
# Access optional resources (check first!)
if platform.has_secrets:
print(platform.secrets.secret_ids)
if platform.has_identity:
print(platform.identity_email)
With Cluster Overrides
platform = StandardPlatform(stack, "platform",
project_id="my-gcp-project",
region="europe-west1",
env="prod",
prefix="myapp",
# Override any cluster setting
machine_type="n2-standard-8",
max_nodes=20,
disk_size=200
)
Building Blocks (Advanced)
For more control, use individual constructs:
from infrastructure_lib import (
DevProfile, ProdProfile,
StandardVPC, StandardCluster,
StandardSecrets, StandardIdentity,
NetworkConfig, ClusterConfig
)
# 1. Choose a profile
profile = ProdProfile(
project_id="my-project",
region="us-central1",
env="prod",
prefix="myapp"
)
# 2. Create VPC with custom CIDR
vpc = StandardVPC(stack, "vpc",
config=profile.get_network_config(cidr="10.1.0.0/16")
)
# 3. Create GKE with custom settings
cluster = StandardCluster(stack, "gke",
config=profile.get_cluster_config(
machine_type="n2-standard-8",
max_nodes=15
),
network_id=vpc.network_id,
subnet_id=vpc.subnet_id
)
# 4. Create secrets separately
secrets = StandardSecrets(stack, "secrets",
secret_ids=["db-password", "redis-password"]
)
# 5. Setup Workload Identity
identity = StandardIdentity(stack, "identity",
project_id="my-project",
sa_id="my-service-account",
k8s_namespace="default",
k8s_sa_name="my-k8s-sa",
roles=["roles/secretmanager.secretAccessor"]
)
โ๏ธ Configuration
Required Parameters
These must always be provided:
| Parameter | Type | Description | Example |
|---|---|---|---|
project_id |
str |
GCP Project ID | "my-gcp-project-123" |
region |
str |
GCP Region | "europe-west1" |
env |
str |
Environment name | "dev", "staging", "prod" |
prefix |
str |
Resource naming prefix | "myapp" |
Optional Parameters
Network Configuration
| Parameter | Default | Description |
|---|---|---|
cidr |
10.0.0.0/16 |
Primary subnet CIDR |
pod_cidr |
10.11.0.0/21 |
Secondary range for pods (~2000 pods) |
service_cidr |
10.12.0.0/21 |
Secondary range for services |
pod_range_name |
pod-ranges |
Name of pod secondary range |
service_range_name |
service-ranges |
Name of service secondary range |
Cluster Configuration
| Parameter | Dev Default | Prod Default | Description |
|---|---|---|---|
zone |
{region}-a |
{region}-a |
GCP zone |
machine_type |
e2-medium |
n2-standard-4 |
Node machine type |
node_count |
1 |
3 |
Initial node count |
min_nodes |
1 |
3 |
Autoscaler minimum |
max_nodes |
3 |
10 |
Autoscaler maximum |
disk_size |
50 |
100 |
Node disk size (GB) |
spot_instances |
true |
false |
Use spot/preemptible VMs |
master_cidr |
172.16.0.0/28 |
172.16.0.0/28 |
Private cluster master CIDR |
Environment Profiles
| Profile | Use Case | Key Characteristics |
|---|---|---|
DevProfile |
Development | Cost-optimized, spot instances, small nodes |
StagingProfile |
Pre-production | Production-like but with spot instances |
ProdProfile |
Production | HA, no spot, larger nodes, deletion protection |
๐ API Reference
StandardPlatform
StandardPlatform(
scope: Construct,
id: str,
project_id: str, # Required
region: str, # Required
env: str, # Required
prefix: str, # Required
secret_ids: list[str], # Optional
workload_identity: dict, # Optional
**cluster_overrides # Optional
)
Properties:
vpcโStandardVPCclusterโStandardClustersecretsโStandardSecrets(raisesValueErrorif not configured)identityโStandardIdentity(raisesValueErrorif not configured)has_secretsโboolhas_identityโboolcluster_nameโstrcluster_endpointโstrnetwork_idโstrsubnet_idโstridentity_emailโstr
StandardVPC
StandardVPC(
scope: Construct,
id: str,
config: NetworkConfig,
labels: dict[str, str] = None # Optional
)
Properties:
networkโComputeNetworksubnetโComputeSubnetworkrouterโComputeRouternatโComputeRouterNatnetwork_idโstrsubnet_idโstr
StandardCluster
StandardCluster(
scope: Construct,
id: str,
config: ClusterConfig,
network_id: str,
subnet_id: str
)
Properties:
clusterโContainerClusternode_poolโContainerNodePool
StandardSecrets
StandardSecrets(
scope: Construct,
id: str,
secret_ids: list[str]
)
Properties:
secret_idsโlist[str]
Methods:
get_secret(secret_id: str)โSecretManagerSecret
StandardIdentity
StandardIdentity(
scope: Construct,
id: str,
project_id: str,
sa_id: str, # 6-30 characters
k8s_namespace: str,
k8s_sa_name: str,
roles: list[str] = None # Optional
)
Properties:
gsaโServiceAccountemailโstr
๐ก Examples
Example 1: Basic Dev Environment
platform = StandardPlatform(stack, "dev",
project_id="my-project",
region="us-central1",
env="dev",
prefix="myapp"
)
Creates:
- VPC:
myapp-dev-vpc - Subnet:
myapp-dev-subnet - Cluster:
myapp-dev-cluster(e2-medium, 1-3 nodes, spot)
Example 2: Production with HA
platform = StandardPlatform(stack, "prod",
project_id="my-project",
region="europe-west1",
env="prod",
prefix="myapp",
max_nodes=20
)
Creates:
- Cluster with n2-standard-4 nodes
- 3-20 nodes autoscaling
- Deletion protection enabled
- No spot instances
Example 3: Multiple Environments
import os
env = os.getenv("ENV", "dev")
platform = StandardPlatform(stack, f"{env}-platform",
project_id=os.getenv("GCP_PROJECT_ID"),
region=os.getenv("GCP_REGION", "us-central1"),
env=env,
prefix="myapp"
)
๐งช Testing
Run the test suite:
# Run all tests
pytest tests/ -v
# Run with coverage
pytest tests/ --cov=infrastructure_lib
# Run specific test
pytest tests/test_config.py::TestClusterConfig -v
Test Coverage
- โ Configuration validation
- โ Auto-generated properties
- โ Profile configuration
- โ Override mechanism
๐ Project Structure
x-infra-kit/
โโโ infrastructure_lib/ # Main library
โ โโโ __init__.py # Public exports
โ โโโ config.py # Configuration dataclasses
โ โโโ profiles.py # Environment profiles
โ โโโ networking.py # VPC, Subnet, NAT
โ โโโ gke.py # GKE Cluster
โ โโโ security.py # Secrets, Workload Identity
โ โโโ composites.py # StandardPlatform
โโโ tests/ # Unit tests
โ โโโ test_config.py
โโโ main.py # Example usage
โโโ setup.py # Package definition
โโโ requirements.txt # Dependencies
โโโ cdktf.json # CDKTF configuration
โโโ README.md # This file
๐ค Contributing
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Follow the existing code style:
- Type hints on all functions
- Docstrings on all classes and public methods
- Validation in constructors
- Tests for new features
- Run tests (
pytest tests/ -v) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Acknowledgments
- CDKTF - Cloud Development Kit for Terraform
- Google Cloud Provider - Terraform Google Cloud Provider
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file x_infra_kit-1.0.0.tar.gz.
File metadata
- Download URL: x_infra_kit-1.0.0.tar.gz
- Upload date:
- Size: 19.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b892a9bd4ca52928287e1c78be562f6692b1b67005cdf0c6bde6acc1e8d05739
|
|
| MD5 |
82ef9086662e530e471873d971abd508
|
|
| BLAKE2b-256 |
4f147821990d85741e6b51d9657c176d3e25cb8cc6e48dee92a8edc916cb2fb5
|
Provenance
The following attestation bundles were made for x_infra_kit-1.0.0.tar.gz:
Publisher:
ci.yml on xofyy/x-infra-kit
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
x_infra_kit-1.0.0.tar.gz -
Subject digest:
b892a9bd4ca52928287e1c78be562f6692b1b67005cdf0c6bde6acc1e8d05739 - Sigstore transparency entry: 757759483
- Sigstore integration time:
-
Permalink:
xofyy/x-infra-kit@040104bfcfb502d50e17f8cc01a5612bd37578f5 -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/xofyy
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@040104bfcfb502d50e17f8cc01a5612bd37578f5 -
Trigger Event:
push
-
Statement type:
File details
Details for the file x_infra_kit-1.0.0-py3-none-any.whl.
File metadata
- Download URL: x_infra_kit-1.0.0-py3-none-any.whl
- Upload date:
- Size: 17.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bf2db72608920c0fa8ca415ead8e12510dd95bc3841e942e8c3c27b848985327
|
|
| MD5 |
f1df0f3372081be1eeb786210b104dc9
|
|
| BLAKE2b-256 |
f30efa7566e3ead52819f3b5af1dc1dbce89a4013180029c0b6b65fefa2e722a
|
Provenance
The following attestation bundles were made for x_infra_kit-1.0.0-py3-none-any.whl:
Publisher:
ci.yml on xofyy/x-infra-kit
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
x_infra_kit-1.0.0-py3-none-any.whl -
Subject digest:
bf2db72608920c0fa8ca415ead8e12510dd95bc3841e942e8c3c27b848985327 - Sigstore transparency entry: 757759491
- Sigstore integration time:
-
Permalink:
xofyy/x-infra-kit@040104bfcfb502d50e17f8cc01a5612bd37578f5 -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/xofyy
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@040104bfcfb502d50e17f8cc01a5612bd37578f5 -
Trigger Event:
push
-
Statement type: