Skip to content

Instantly share code, notes, and snippets.

@hoo29
Last active September 12, 2022 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hoo29/16a6695c813c4670f210812d68961a9b to your computer and use it in GitHub Desktop.
Save hoo29/16a6695c813c4670f210812d68961a9b to your computer and use it in GitHub Desktop.
Example CDKTF file
import { Construct } from "constructs";
import { App, RemoteBackend, TerraformStack, TerraformHclModule, TerraformOutput } from "cdktf";
import { AwsProvider } from "@cdktf/provider-aws";
// These variables are hardcoded here for brevity but the values could be stored and fetched from anywhere.
const DEPLOYMENT_TARGETS = {
// region : aws account id
"ap-northeast-1": "582318560864",
"ap-northeast-2": "600734575887",
"ap-south-1": "718504428378",
"ap-southeast-1": "114774131450",
"ca-central-1": "985666609251",
"eu-central-1": "054676820928",
"eu-west-1": "156460612806",
"eu-west-2": "652711504416",
"eu-west-3": "009996457667",
"sa-east-1": "507241528517",
"us-east-1": "127311923021",
"us-east-2": "033677994240",
"us-west-1": "027434742980",
"us-west-2": "797873946194",
};
const MODULE_VERSIONS = {
default: "1.0.0",
regionalOverride: {
"eu-west-2": "2.0.0",
},
};
/**
* Utility function, create aws providers for all regions.
*/
function createAllProviders(scope: Construct, deploymentTargets: Record<string, string>) {
const allProviders: Record<string, AwsProvider> = {};
for (const [region, accountId] of Object.entries(deploymentTargets)) {
allProviders[region] = new AwsProvider(scope, `a${accountId}-${region}`, {
alias: `a${accountId}-${region}`,
region,
assumeRole: {
roleArn: `arn:aws:iam::${accountId}:role/IaC`,
},
defaultTags: {
tags: {
System: "core",
},
},
});
}
return allProviders;
}
interface StackConfig {
deploymentTargets: Record<string, string>;
moduleVersions: {
default: string;
regionalOverride: Record<string, string | undefined>;
};
}
class MyCdkStack extends TerraformStack {
constructor(scope: Construct, stackName: string, config: StackConfig) {
super(scope, stackName);
// Create all the provider configurations
const allProviders = createAllProviders(this, config.deploymentTargets);
// Create module resources for all generated providers.
const allModules: Record<string, TerraformHclModule> = {};
for (const [region, provider] of Object.entries(allProviders)) {
allModules[region] = new TerraformHclModule(this, `normal-hcl-module-${region}`, {
source: "terraform_registry",
variables: {
module_input_var: "a value"
},
// Overide module version based on region.
version: config.moduleVersions.regionalOverride?.[region] || config.moduleVersions.default,
providers: [provider],
});
}
// Bubble up module outputs to top level
for (const [region, module] of Object.entries(allModules)) {
new TerraformOutput(this, `hcl-module-${region}-output`, {
value: module.get("module_output_name"),
description: `Output for region ${region}`,
staticId: true,
});
}
}
}
const app = new App();
const myCdkStack = new MyCdkStack(app, "stack", {
deploymentTargets: DEPLOYMENT_TARGETS,
moduleVersions: MODULE_VERSIONS,
});
new RemoteBackend(myCdkStack, {
organization: "org",
hostname: "hostname",
workspaces: {
name: "cdktf",
},
});
app.synth();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment