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>21</source>
<target>21</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-terraform</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-terraform/
TemplateOutput TypeDescription
shared.ftlTerraformGenerates shared resources (provider blocks, logging)
handler.ftlJavaGenerates the Java wrapper for the handler
function.ftlTerraformGenerates the Terraform registration for the handler
apiIntegrationClass.ftlJavaGenerates an APIFunction
apiIntegration.ftlJSONGenerates an OpenAPI 2.0 snippet
apiRegistration.ftlTerraformGenerates a Terraform function registration
schedule.ftlTerraformGenerates a google_cloud_scheduler_job resource
api.ftlTerraformGenerates a google_api_gateway_api resource

ℹ️ 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(phase = GenerationPhase.SHARED, templateName = "shared.ftl", outputFileExtension = "tf", outputSubDirectory = "", javaFile = false),
@Template(phase = GenerationPhase.FUNCTION, templateName = "handler.ftl", outputFileExtension = "java", outputSubDirectory = "generated", javaFile = true),
@Template(phase = GenerationPhase.FUNCTION, templateName = "function.ftl", outputFileExtension = "tf", outputSubDirectory = "", javaFile = false),
@Template(phase = GenerationPhase.API, templateName = "apiIntegrationClass.ftl", outputFileExtension = "java", outputSubDirectory = "", javaFile = true),
@Template(phase = GenerationPhase.API, templateName = "apiIntegration.ftl", outputFileExtension = "json", outputSubDirectory = "api-routes/", javaFile = false),
@Template(phase = GenerationPhase.API, templateName = "apiRegistration.ftl", outputFileExtension = "tf", outputSubDirectory = "", javaFile = false),
@Template(phase = GenerationPhase.SCHEDULE, templateName = "schedule.ftl", outputFileExtension = "tf", outputSubDirectory = "", javaFile = false),
@Template(phase = GenerationPhase.FINALIZE, templateName = "api.ftl", outputFileExtension = "tf", outputSubDirectory = "", javaFile = false)
}
)
public class GcpTerraformJava21TemplateRegistration { ... }

@Template uses templateName (the .ftl file), phase (one of FUNCTION, API, SCHEDULE, FINALIZE, SHARED), outputFileExtension, optional outputSubDirectory, and javaFile.

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


📂 Output​

ArtifactLocation
Java handler classestarget/generated-sources/gcp-terraform/
Terraform configurationtarget/deployment/gcp-terraform/*.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.