Last active
December 19, 2019 19:34
-
-
Save BrutalSimplicity/da6bda118ccbc5621db53dbf3c1fe373 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { safeLoad } from 'js-yaml'; | |
import { readFileSync } from 'fs'; | |
import { getOutputValues, EnvironmentProps } from './infrastructure/environments'; | |
import { LoadBalancerServiceProps, Route53Stack } from './infrastructure/route53'; | |
import { App } from '@aws-cdk/core'; | |
type FargateConfig = { | |
environments: EnvironmentProps[]; | |
} | |
const app = new App(); | |
const configFile = (app.node.tryGetContext('config') || 'config.dev1.yml') as string; | |
if (!configFile) { | |
console.error('A "config" context variable must be defined (e.g. cdk -c config=config.dev1.yml)'); | |
} | |
const fargateConfig = safeLoad(readFileSync(`config/${configFile}`, { encoding: 'utf8' }), { filename: configFile }) as FargateConfig; | |
deploy(); | |
async function deploy(): Promise<void> { | |
const loadBalancerProps: LoadBalancerServiceProps = {}; | |
for (const environment of fargateConfig.environments) { | |
const pairs = (await getOutputValues(['DnsName', 'HostedZoneId'], `nginx-fargate-${environment.name}-${environment.region}`, environment.region)); | |
const [dnsName, hostedZoneId] = [pairs[0].value, pairs[1].value]; | |
loadBalancerProps[environment.region] = { dnsName, hostedZoneId }; | |
} | |
let zoneDomainName = (await getOutputValues(['DomainName'], fargateConfig.environments[0].globalResourcesStack, fargateConfig.environments[0].region))[0].value; | |
new Route53Stack(app, 'nginx-route-53', { | |
stack: { | |
env: { | |
account: fargateConfig.environments[0].accountId, | |
region: fargateConfig.environments[0].region | |
} | |
}, | |
subDomainName: fargateConfig.environments[0].subDomainName, | |
zoneDomainName, | |
loadBalancers: loadBalancerProps | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cdk = require('@aws-cdk/core'); | |
import { CfnRecordSetGroup, HostedZone } from '@aws-cdk/aws-route53'; | |
export type LoadBalancerServiceProps = { | |
[region: string]: { | |
hostedZoneId: string; | |
dnsName: string; | |
} | |
} | |
export interface Route53StackProps { | |
stack?: cdk.StackProps; | |
loadBalancers: LoadBalancerServiceProps; | |
zoneDomainName: string; | |
subDomainName: string; | |
} | |
export class Route53Stack extends cdk.Stack { | |
constructor(scope: cdk.Construct, id: string, props: Route53StackProps) { | |
super(scope, id, props.stack); | |
const zone = HostedZone.fromLookup(this, 'hosted-zone', { | |
domainName: props.zoneDomainName | |
}); | |
const hostedZoneId = zone.hostedZoneId.replace('/hostedzone/', ''); | |
const recordSets: CfnRecordSetGroup.RecordSetProperty[] = []; | |
for (const region in props.loadBalancers) { | |
recordSets.push({ | |
name: `${props.subDomainName}.${props.zoneDomainName}`, | |
type: 'A', | |
aliasTarget: { | |
dnsName: props.loadBalancers[region].dnsName, | |
hostedZoneId: props.loadBalancers[region].hostedZoneId, | |
evaluateTargetHealth: true | |
}, | |
region, | |
setIdentifier: `nginx-${region}` | |
}); | |
} | |
new CfnRecordSetGroup(this, 'group-record-set', { | |
recordSets, | |
hostedZoneId | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment