Skip to content

GCP CDKTF Plugin

The @stacksolo/plugin-gcp-cdktf is the core plugin that generates Terraform CDK code for GCP resources.

{
"project": {
"plugins": ["@stacksolo/plugin-gcp-cdktf"],
"networks": [{
"name": "main",
"functions": [{ "name": "api" }]
}]
}
}
ConfigGCP Resource
storageBucketsCloud Storage
functionsCloud Functions Gen2
uisFirebase Hosting
containersCloud Run
loadBalancerHTTP(S) Load Balancer

Create Cloud Storage buckets within a network. These can be used as trigger sources for functions.

{
"networks": [{
"name": "main",
"storageBuckets": [
{ "name": "myapp-uploads" },
{ "name": "myapp-processed" }
]
}]
}
PropertyTypeDefaultDescription
namestring-Bucket name (required, globally unique)
locationstringregionBucket location
storageClassstringSTANDARDSTANDARD, NEARLINE, COLDLINE, ARCHIVE
versioningbooleanfalseEnable object versioning
uniformBucketLevelAccessbooleantrueUse uniform IAM access

Deploy Cloud Functions Gen2 with HTTP or event triggers.

{
"functions": [{
"name": "api",
"entryPoint": "handler",
"allowUnauthenticated": true
}]
}

Process files automatically when uploaded to a bucket:

{
"networks": [{
"name": "main",
"storageBuckets": [
{ "name": "myapp-uploads" },
{ "name": "myapp-processed" }
],
"functions": [{
"name": "processor",
"entryPoint": "handler",
"memory": "1Gi",
"timeout": 300,
"trigger": {
"type": "storage",
"bucket": "myapp-uploads",
"event": "finalize"
},
"env": {
"OUTPUT_BUCKET": "myapp-processed"
}
}]
}]
}

Storage trigger events:

EventWhen triggered
finalizeFile created or overwritten (default)
deleteFile deleted
archiveFile archived (versioned buckets)
metadataUpdateFile metadata changed

The plugin automatically:

  • Enables Eventarc API
  • Grants IAM permissions for GCS to publish events
  • Grants the function permission to receive events
  • Configures the Eventarc trigger

The load balancer routes traffic to your functions, containers, and UIs based on URL paths.

{
"loadBalancer": {
"name": "gateway",
"routes": [
{ "path": "/api/*", "functionName": "api" },
{ "path": "/*", "uiName": "web" }
]
}
}
{
"loadBalancer": {
"name": "gateway",
"domain": "app.example.com",
"enableHttps": true,
"redirectHttpToHttps": true,
"routes": [
{ "path": "/*", "functionName": "api" }
]
}
}

When using the Cloudflare Plugin, DNS records are created automatically:

{
"project": {
"cloudflare": {
"zoneId": "your-zone-id",
"apiToken": "@secret/cloudflare-api-token"
},
"networks": [{
"name": "main",
"loadBalancer": {
"name": "gateway",
"domain": "app.example.com",
"enableHttps": true,
"dns": {
"provider": "cloudflare",
"proxied": true
},
"routes": [{ "path": "/*", "functionName": "api" }]
}
}]
}
}
PropertyTypeDefaultDescription
namestring-Load balancer name (required)
routesarray-URL path to backend mappings (required)
domainstring-Custom domain for HTTPS
enableHttpsbooleanfalseEnable managed SSL certificate
redirectHttpToHttpsbooleanfalseRedirect HTTP to HTTPS
dns.providerstring-DNS provider (cloudflare)
dns.proxiedbooleantrueEnable Cloudflare proxy

Code is generated to .stacksolo/cdktf/ using Terraform CDK (TypeScript).

Terminal window
# View generated code
cat .stacksolo/cdktf/main.ts
# Run terraform commands directly
cd .stacksolo/cdktf && npx cdktf plan

These GCP APIs are enabled automatically:

  • Cloud Functions
  • Cloud Build
  • Cloud Run
  • Cloud Storage
  • Compute Engine
  • Eventarc (for storage/pubsub triggers)