Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cfn class after #2

Merged
merged 2 commits into from
Jan 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 15 additions & 29 deletions lib/cdk-workshop-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,32 @@ import genVpc from './constructs/vpc'
import genSg from './constructs/sg'
import genElb from './constructs/elb'
import genAsg from './constructs/asg'
import { ConstructProps, NetworkConstructs } from '../types'

export class CdkWorkshopStack extends Stack {
constructor (scope: App, id: string, props?: StackProps) {
super(scope, id, props)

const vpc = genVpc({
const opt: ConstructProps = {
stack: this,
scope,
id,
props
})
}
const network: NetworkConstructs = {
subnets: {
public: []
},
loadBalancer: {},
fleet: {}
}

const sg = genSg({
stack: this,
scope,
id,
props
},
vpc
)
genVpc(opt, network)

const elb = genElb({
stack: this,
scope,
id,
props
},
vpc,
sg
)
genSg(opt, network)

genAsg({
stack: this,
scope,
id,
props
},
vpc,
elb,
sg
)
genElb(opt, network)

genAsg(opt, network)
}
}
24 changes: 18 additions & 6 deletions lib/constructs/asg.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
import { CfnAutoScalingGroup, CfnLaunchConfiguration } from '@aws-cdk/aws-autoscaling'
import { ConstructProps } from '../../types/index'
import { ConstructProps, NetworkConstructs } from '../../types/index'

const targetCapacity = {
minSize: '1',
default: '2'
}

export default function ({ stack, scope, id, props }: ConstructProps, vpc: any, elb: any, sg: any): any {
export default function (
{ stack, scope, id, props }: ConstructProps,
network: NetworkConstructs
): void {
if (typeof network.fleet.sg === 'undefined') {
throw new Error('VPC not created')
}
if (typeof network.fleet.tg === 'undefined') {
throw new Error('VPC not created')
}
if (typeof network.vpc === 'undefined') {
throw new Error('VPC not created')
}
const targetFleetLaunchConfig = new CfnLaunchConfiguration(stack, 'targetFleetLaunchConfig', {
imageId: 'ami-01bbe152bf19d0289',
instanceType: 't3.micro',
securityGroups: sg.targetFleetSg.ref,
securityGroups: [network.fleet.sg.ref],
instanceMonitoring: false,
associatePublicIpAddress: true
})
new CfnAutoScalingGroup(stack, 'targetFleetAutoScalingGroup', {
minSize: targetCapacity.minSize,
maxSize: targetCapacity.default,
vpcZoneIdentifier: [
vpc.publicSubnet1.ref,
vpc.publicSubnet2.ref
network.subnets.public[0].ref,
network.subnets.public[1].ref
],
availabilityZones: [
stack.availabilityZones[0],
Expand All @@ -30,7 +42,7 @@ export default function ({ stack, scope, id, props }: ConstructProps, vpc: any,
healthCheckType: 'EC2',
healthCheckGracePeriod: 60,
targetGroupArns: [
elb.targetFleetTg.ref
network.fleet.tg.ref
]
})
}
30 changes: 19 additions & 11 deletions lib/constructs/elb.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
import { CfnLoadBalancer, CfnListener, CfnTargetGroup } from '@aws-cdk/aws-elasticloadbalancingv2'
import { ConstructProps } from '../../types/index'
import { ConstructProps, NetworkConstructs } from '../../types/index'

export default function ({ stack, scope, id, props }: ConstructProps, vpc: any, sg: any): any {
export default function (
{ stack, scope, id, props }: ConstructProps,
network: NetworkConstructs
): void {
if (typeof network.loadBalancer.sg === 'undefined') {
throw new Error('VPC not created')
}
if (typeof network.vpc === 'undefined') {
throw new Error('VPC not created')
}
// Alb
const alb = new CfnLoadBalancer(stack, 'alb', {
network.loadBalancer.elb = new CfnLoadBalancer(stack, 'alb', {
loadBalancerAttributes: [
{
key: 'idle_timeout.timeout_seconds',
value: '60'
}
],
scheme: 'internet-facing',
securityGroups: sg.publicAlbSg.ref,
securityGroups: [network.loadBalancer.sg.ref],
subnets: [
vpc.publicSubnet1.ref,
vpc.publicSubnet2.ref
network.subnets.public[0].ref,
network.subnets.public[1].ref
]
})

// Target Group
const targetFleetTg = new CfnTargetGroup(stack, 'targetFleetTg', {
vpcId: vpc.vpc.ref,
network.fleet.tg = new CfnTargetGroup(stack, 'targetFleetTg', {
vpcId: network.vpc.ref,
port: 80,
protocol: 'HTTP',
healthCheckIntervalSeconds: 30,
Expand Down Expand Up @@ -49,12 +58,11 @@ export default function ({ stack, scope, id, props }: ConstructProps, vpc: any,
defaultActions: [
{
type: 'forward',
targetGroupArn: targetFleetTg.ref
targetGroupArn: network.fleet.tg.ref
}
],
loadBalancerArn: alb.ref,
loadBalancerArn: network.loadBalancer.elb.ref,
port: 80,
protocol: 'HTTP'
})
return { targetFleetTg }
}
25 changes: 15 additions & 10 deletions lib/constructs/sg.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { CfnSecurityGroup, CfnSecurityGroupIngress } from '@aws-cdk/aws-ec2'
import { ConstructProps } from '../../types/index'
import { ConstructProps, NetworkConstructs } from '../../types/index'

export default function (
{ stack, scope, id, props }: ConstructProps,
network: NetworkConstructs
): void {
if (typeof network.vpc === 'undefined') {
throw new Error('VPC not created')
}

export default function ({ stack, scope, id, props }: ConstructProps, vpc: any): any {
// PublicAlbSg
const publicAlbSg = new CfnSecurityGroup(stack, 'publicAlbSg', {
network.loadBalancer.sg = new CfnSecurityGroup(stack, 'publicAlbSg', {
groupDescription: 'SecurityGroup for Public ALB',
securityGroupIngress: [
{
Expand All @@ -14,11 +21,11 @@ export default function ({ stack, scope, id, props }: ConstructProps, vpc: any):
toPort: 80
}
],
vpcId: vpc.vpc.ref
vpcId: network.vpc.ref
})

// TargetFleetSg
const targetFleetSg = new CfnSecurityGroup(stack, 'targetFleetSg', {
network.fleet.sg = new CfnSecurityGroup(stack, 'targetFleetSg', {
groupDescription: 'SecurityGroup for Target Fleet',
securityGroupIngress: [
{
Expand All @@ -29,17 +36,15 @@ export default function ({ stack, scope, id, props }: ConstructProps, vpc: any):
toPort: 22
}
],
vpcId: vpc.vpc.ref
vpcId: network.vpc.ref
})

new CfnSecurityGroupIngress(stack, 'targetFleetSgIngress1', {
sourceSecurityGroupId: publicAlbSg.ref,
sourceSecurityGroupId: network.loadBalancer.sg.ref,
description: 'Rule For HTTP Access From Public ALB',
ipProtocol: 'tcp',
fromPort: 80,
toPort: 80,
groupId: targetFleetSg.ref
groupId: network.fleet.sg.ref
})

return { publicAlbSg, targetFleetSg }
}
12 changes: 9 additions & 3 deletions lib/constructs/vpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ import {
} from '@aws-cdk/aws-ec2'

import {
ConstructProps
ConstructProps,
NetworkConstructs
} from '../../types/index'

export default function ({ stack, scope, id, props }: ConstructProps): any {
export default function (
{ stack, scope, id, props }: ConstructProps,
network: NetworkConstructs
): void {
// VPC
const vpc = new CfnVPC(stack, 'VPC', {
cidrBlock: '10.0.0.0/16',
Expand Down Expand Up @@ -73,5 +77,7 @@ export default function ({ stack, scope, id, props }: ConstructProps): any {
Tag.add(construct, 'Application', id)
Tag.add(construct, 'Name', construct.node.id)
})
return { vpc, igw, publicSubnet1, publicSubnet2, publicRouteTable }

network.vpc = vpc
network.subnets.public.push(publicSubnet1, publicSubnet2)
}
20 changes: 20 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
import { CfnAutoScalingGroup } from '@aws-cdk/aws-autoscaling'
import { App, Stack, StackProps } from '@aws-cdk/core'
import { CfnSecurityGroup, CfnSubnet, CfnVPC } from '@aws-cdk/aws-ec2'
import { CfnLoadBalancer, CfnTargetGroup } from '@aws-cdk/aws-elasticloadbalancingv2'

export declare interface ConstructProps {
stack: Stack
scope: App
id: string
props?: StackProps
}

export declare interface NetworkConstructs {
vpc?: CfnVPC
subnets: {
public: CfnSubnet[]
}
loadBalancer: {
elb?: CfnLoadBalancer
sg?: CfnSecurityGroup
}
fleet: {
asg?: CfnAutoScalingGroup
sg?: CfnSecurityGroup
tg?: CfnTargetGroup
}
}