Skip to content

Instantly share code, notes, and snippets.

@SlootSantos
Last active May 22, 2022 22:24
Show Gist options
  • Save SlootSantos/0645880d5164fe533f99adeec082f708 to your computer and use it in GitHub Desktop.
Save SlootSantos/0645880d5164fe533f99adeec082f708 to your computer and use it in GitHub Desktop.
import { Construct } from "constructs";
import { Stage, StageProps } from "aws-cdk-lib";
import { DelegatedDomainStack } from "../stacks/application-stack";
interface DelegatedDomainPipelineStageProps extends StageProps {
stage: "dev" | "prod";
}
export class DelegatedDomainPipelineStage extends Stage {
constructor(
scope: Construct,
id: string,
props: DelegatedDomainPipelineStageProps
) {
super(scope, id, props);
new DelegatedDomainStack(this, "DelegatedDomainStack", {
stageConfig: {
stage: props.stage,
},
});
}
}
import {
CodeBuildStep,
CodePipeline,
CodePipelineSource,
} from "aws-cdk-lib/pipelines";
import * as cdk from "aws-cdk-lib";
import { Construct } from "constructs";
import { accounts } from "../constants/accounts";
import { mainRegion } from "../constants/regions";
import { RootDomainPipelineStage } from "./root-stage";
import { DelegatedDomainPipelineStage } from "./application-stage";
// That is the dev-tools Github connection that you receive when authenticating your AWS account with Github.
const CDK_PIPELINE_SOURCE_CONNECTION =
"connection-string";
// That is the sample repo guiding the Medium article
const cdkSourceInput = CodePipelineSource.connection(
"user/repo",
"main",
{
connectionArn: CDK_PIPELINE_SOURCE_CONNECTION,
}
);
const applicationStages: {
targetAccount: string; // this will be the account we're rolling out to
stageName: "dev" | "prod"; // this is also going to be the subdomain => dev.domain.com
}[] = [
{
stageName: "dev",
targetAccount: accounts.dev,
},
{
stageName: "prod",
targetAccount: accounts.prod,
},
];
export class DNSPipelineStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const pipeline = new CodePipeline(this, "Pipeline", {
crossAccountKeys: true,
pipelineName: "DNS-Pipeline",
synth: new CodeBuildStep("SynthStep", {
input: cdkSourceInput,
installCommands: ["npm install -g aws-cdk"],
commands: ["npm ci", "npm run build", "npx cdk synth"],
}),
});
const rootDomain = new RootDomainPipelineStage(
this,
`root-${accounts.rootDomain}`,
{
env: {
region: mainRegion,
account: accounts.rootDomain,
},
}
);
// manually adding the "special" root domain stack as first stage in the pipeline
pipeline.addStage(rootDomain);
// for each of the application stages, add stage to the pipeline
applicationStages.forEach((stage) => {
const applicationDomain = new DelegatedDomainPipelineStage(
this,
`${stage.stageName}-${stage.targetAccount}`,
{
stage: stage.stageName,
env: {
region: mainRegion,
account: stage.targetAccount,
},
}
);
pipeline.addStage(applicationDomain);
});
}
}
import { Construct } from "constructs";
import { Stage, StageProps } from "aws-cdk-lib";
import { RootDomainStack } from "../stacks/root-stack";
export class RootDomainPipelineStage extends Stage {
constructor(scope: Construct, id: string, props?: StageProps) {
super(scope, id, props);
new RootDomainStack(this, "RootDomainStack");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment