Implementing a Deployment solution for AWS #152
@@ -12,3 +12,7 @@ The `cdk.json` file tells the CDK Toolkit how to execute your app.
|
|||||||
* `cdk deploy` deploy this stack to your default AWS account/region
|
* `cdk deploy` deploy this stack to your default AWS account/region
|
||||||
* `cdk diff` compare deployed stack with current state
|
* `cdk diff` compare deployed stack with current state
|
||||||
* `cdk synth` emits the synthesized CloudFormation template
|
* `cdk synth` emits the synthesized CloudFormation template
|
||||||
|
|
||||||
|
## configuration, please set them as env var
|
||||||
|
* `LOCALTUNNEL_BASE_DOMAIN` the base domain for the localtunnel server, must be a root domain like example.com
|
||||||
|
* `LOCALTUNNEL_ROUTE53_ID` the ID of the route53 hosted zone for the domain
|
||||||
|
|||||||
@@ -3,5 +3,6 @@ import 'source-map-support/register';
|
|||||||
import * as cdk from '@aws-cdk/core';
|
import * as cdk from '@aws-cdk/core';
|
||||||
import { DeployStack } from '../lib/deploy-stack';
|
import { DeployStack } from '../lib/deploy-stack';
|
||||||
|
|
||||||
const app = new cdk.App();
|
let app = new cdk.App();
|
||||||
new DeployStack(app, 'DeployStack');
|
// replace them with HostedZoneID in route53, the domain name for the localtunnel, and the hosted zone domain
|
||||||
|
new DeployStack(app, 'DeployStack', process.env.LOCALTUNNEL_ROUTE53_ID as string , process.env.LOCALTUNNEL_BASE_DOMAIN as string);
|
||||||
|
|||||||
@@ -5,8 +5,11 @@ import * as ec2 from "@aws-cdk/aws-ec2";
|
|||||||
import * as ecs from "@aws-cdk/aws-ecs";
|
import * as ecs from "@aws-cdk/aws-ecs";
|
||||||
import * as ecs_patterns from "@aws-cdk/aws-ecs-patterns";
|
import * as ecs_patterns from "@aws-cdk/aws-ecs-patterns";
|
||||||
|
|
||||||
|
// tls certificate with ACM
|
||||||
|
import * as route53 from '@aws-cdk/aws-route53';
|
||||||
|
import * as acm from '@aws-cdk/aws-certificatemanager';
|
||||||
export class DeployStack extends cdk.Stack {
|
export class DeployStack extends cdk.Stack {
|
||||||
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
|
constructor(scope: cdk.Construct, id: string, hostedZoneID: string, domainName: string, props?: cdk.StackProps) {
|
||||||
super(scope, id, props);
|
super(scope, id, props);
|
||||||
// create vpc with 3 subnets
|
// create vpc with 3 subnets
|
||||||
const vpc = new ec2.Vpc(this, "LocalTunnelVPC", {
|
const vpc = new ec2.Vpc(this, "LocalTunnelVPC", {
|
||||||
@@ -16,18 +19,46 @@ export class DeployStack extends cdk.Stack {
|
|||||||
const cluster = new ecs.Cluster(this, "LocalTunnelCluster", {
|
const cluster = new ecs.Cluster(this, "LocalTunnelCluster", {
|
||||||
vpc: vpc
|
vpc: vpc
|
||||||
})
|
})
|
||||||
|
// create acm cert
|
||||||
|
const DNSZone = route53.HostedZone.fromHostedZoneAttributes(this, 'HostedZone', {
|
||||||
|
zoneName: domainName,
|
||||||
|
hostedZoneId: hostedZoneID
|
||||||
|
})
|
||||||
|
// add wildcard CNAME
|
||||||
|
new route53.CnameRecord(this, 'CnameRecordWildcard', {
|
||||||
|
zone: DNSZone,
|
||||||
|
recordName: "*",
|
||||||
|
domainName: domainName
|
||||||
|
})
|
||||||
|
const cert = new acm.Certificate(this, 'Cert', {
|
||||||
|
domainName: domainName,
|
||||||
|
subjectAlternativeNames: ["*."+domainName],
|
||||||
|
validation: acm.CertificateValidation.fromDns(DNSZone)
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// create LBed Fargate service
|
// create LBed Fargate service
|
||||||
new ecs_patterns.ApplicationLoadBalancedFargateService(this, "LocalTunnelService", {
|
let localtunnelsvc = new ecs_patterns.ApplicationLoadBalancedFargateService(this, "LocalTunnelService", {
|
||||||
cluster: cluster,
|
cluster: cluster,
|
||||||
cpu: 512,
|
cpu: 512,
|
||||||
desiredCount: 1,
|
desiredCount: 1,
|
||||||
taskImageOptions: {
|
taskImageOptions: {
|
||||||
image: ecs.ContainerImage.fromRegistry("defunctzombie/localtunnel-server:latest")
|
image: ecs.ContainerImage.fromRegistry("defunctzombie/localtunnel-server:latest")
|
||||||
|
|
||||||
},
|
},
|
||||||
memoryLimitMiB: 2048,
|
memoryLimitMiB: 2048,
|
||||||
publicLoadBalancer: true,
|
publicLoadBalancer: true,
|
||||||
assignPublicIp: true
|
certificate: cert,
|
||||||
|
redirectHTTP: true,
|
||||||
|
recordType: ecs_patterns.ApplicationLoadBalancedServiceRecordType.ALIAS,
|
||||||
|
listenerPort: 443,
|
||||||
|
domainName: domainName,
|
||||||
|
domainZone: DNSZone
|
||||||
|
})
|
||||||
|
// set health route
|
||||||
|
localtunnelsvc.targetGroup.configureHealthCheck({
|
||||||
|
path: "/api/status"
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user