Skip to main content

☁️ GCP Terraform Generator

This module provides a Cloudhopper generator for deploying annotated Java functions as Google Cloud Functions (HTTP-triggered) using Terraform.

It is used by the Cloudhopper annotation processor to generate:

  • GCP-specific Java handler classes
  • Terraform infrastructure resources
  • Supporting metadata for integration and deployment

📦 How to Use

To use this generator in your own project, follow these steps:


1. Add the Maven dependency

<dependency>
<groupId>eu.cloudhopper.mc</groupId>
<artifactId>generator-gcp-terraform</artifactId>
<version>${cloudhopper.version}</version>
</dependency>

2. Configure the annotation processor

<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>17</source>
<target>17</target>
<encoding>UTF-8</encoding>
<annotationProcessors>
<annotationProcessor>
eu.cloudhopper.mc.deployment.config.generator.ServerlessFunctionProcessor
</annotationProcessor>
</annotationProcessors>
<compilerArgs>
<arg>-Acloudprovider=gcp</arg>
<arg>-AgeneratorId=gcp-terraform</arg>
<arg>-AconfigOutputDir=${project.build.directory}/deployment/gcp</arg>
<arg>-AtargetDir=${project.build.directory}</arg>
<arg>-AartifactId=${project.artifactId}</arg>
<arg>-Aversion=${project.version}</arg>
<arg>-Aclassifier=gcp</arg>
</compilerArgs>
</configuration>
</plugin>

This configuration ensures that the correct templates are applied and the output is structured for GCP.


📁 Templates

Templates for this generator are located at:

src/main/resources/templates/gcp/
TemplateOutput TypeDescription
function.ftlTerraformDefines GCP Cloud Function resource
api.ftlTerraformAPI Gateway or routing proxy configuration
apiIntegration.ftlTerraformBinds function with GCP API Gateway (if needed)
schedule.ftlTerraformScheduled (cron) job configuration via Cloud Scheduler
handler.ftlJavaGenerates function class that wraps the logic
shared.ftlTerraformCommon infrastructure like IAM roles, logging, etc.

ℹ️ The doc/ folder in the template path is not used during generation. It may contain examples or template prototypes.


🔌 Template Registration

This generator registers itself using:

@TemplateRegistration(
generatorId = "gcp-terraform",
templates = {
@Template(name = "handler", phase = GenerationPhase.SOURCES),
@Template(name = "function", phase = GenerationPhase.DEPLOYMENT),
@Template(name = "api", phase = GenerationPhase.DEPLOYMENT),
@Template(name = "apiIntegration", phase = GenerationPhase.DEPLOYMENT),
@Template(name = "schedule", phase = GenerationPhase.DEPLOYMENT),
@Template(name = "shared", phase = GenerationPhase.DEPLOYMENT)
}
)
public class GcpTerraformJava21TemplateRegistration { ... }

This registration is auto-discovered using Java’s SPI (META-INF/services/...) mechanism.


📂 Output

ArtifactLocation
Java handler classestarget/generated-sources/gcp/
Terraform configurationtarget/deployment/gcp/*.tf
Deployment metadatatarget/classes/META-INF/cloudhopper/
Optional ZIP for deploymenttarget/${artifactId}-${version}-gcp.zip

🧩 What’s Special About GCP

  • Handler Generation: The generator produces a Java class that implements com.google.cloud.functions.HttpFunction.
  • Context Mapping: The handler uses GcpCloudFunctionRequestHandler, which adapts GCP’s request/response and environment into Cloudhopper's CloudRequestHandler interface.
  • Terraform Output: The generated .tf files define the Cloud Function, IAM permissions, and optional API Gateway or scheduler bindings.

ModuleDescription
provider-gcpContains GcpCloudFunctionRequestHandler used by handlers
deployment-config-apiDefines annotations and runtime interfaces

📝 Use this generator to compile and deploy Cloudhopper functions as Google Cloud Functions using Terraform.