Skip to content

Commit

Permalink
Merge pull request #5 from nib-health-funds/v2
Browse files Browse the repository at this point in the history
Update to cdkv2
  • Loading branch information
thomas0087 authored Sep 18, 2023
2 parents 23eafea + 64022c5 commit 84fd4a2
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 46 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
node_modules
package-lock.json
*.js
*.js
.DS_Store
cdk.out
31 changes: 13 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

{
"name": "cdk-alb",
"version": "1.0.0",
Expand All @@ -8,7 +7,7 @@
"build": "tsc",
"watch": "tsc -w",
"cdk": "cdk",
"test": "jest",
"test": "jest --verbose",
"test-update": "jest -u",
"lint": "eslint 'src/**/*.ts' --quiet",
"lint-fix": "eslint 'src/**/*.ts' --quiet --fix"
Expand All @@ -20,23 +19,19 @@
},
"license": "Apache-2.0",
"devDependencies": {
"@types/jest": "^24.0.18",
"@types/node": "^10.17.0",
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"eslint": "^8.0.1",
"jest": "^26.4.2",
"ts-jest": "^26.4.0",
"ts-node": "^9.0.0",
"prettier": "^2.4.1",
"typescript": "^4.1.2"
"@types/jest": "^29.5.5",
"@types/node": "^20.6.2",
"@typescript-eslint/eslint-plugin": "^6.7.0",
"@typescript-eslint/parser": "^6.7.0",
"eslint": "^8.49.0",
"jest": "^29.7.0",
"prettier": "^3.0.3",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
},
"dependencies": {
"@aws-cdk/assert": "^1.140.0",
"@aws-cdk/aws-autoscaling": "^1.140.0",
"@aws-cdk/aws-ec2": "^1.140.0",
"@aws-cdk/aws-elasticloadbalancingv2": "^1.140.0",
"@aws-cdk/aws-iam": "^1.140.0",
"@aws-cdk/core": "^1.140.0"
"aws-cdk": "^2.96.2",
"aws-cdk-lib": "^2.96.2"
}
}
43 changes: 22 additions & 21 deletions src/stack.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect as expectCDK, haveResource, haveResourceLike, arrayWith, objectLike, stringLike } from '@aws-cdk/assert'
import { App, Stack } from '@aws-cdk/core'
// import { expect as expectCDK, haveResource, haveResourceLike, arrayWith, objectLike, stringLike } from '@aws-cdk/assert'
import { Template, Match } from 'aws-cdk-lib/assertions'
import { App, Stack } from 'aws-cdk-lib/core'
import { LoadBalancerStack } from './stack'

describe('LoadBalancerStack', () => {
Expand All @@ -12,64 +13,64 @@ describe('LoadBalancerStack', () => {
})

it('should create an autoscaling group', () => {
expectCDK(testStack).to(haveResource('AWS::AutoScaling::AutoScalingGroup', {
Template.fromStack(testStack).hasResourceProperties('AWS::AutoScaling::AutoScalingGroup', {
MaxSize: '1',
MinSize: '1'
}))
})
})

it('should use the correct instance type', () => {
expectCDK(testStack).to(haveResource('AWS::AutoScaling::LaunchConfiguration', {
Template.fromStack(testStack).hasResourceProperties('AWS::AutoScaling::LaunchConfiguration', {
InstanceType: 't3a.micro'
}))
})
})

it('have the required outbound ports open', () => {
expectCDK(testStack).to(haveResource('AWS::EC2::SecurityGroup', {
Template.fromStack(testStack).hasResourceProperties('AWS::EC2::SecurityGroup', {
SecurityGroupEgress: [
objectLike({
Match.objectLike({
ToPort: 443
}),
objectLike({
Match.objectLike({
ToPort: 5432
}),
objectLike({
Match.objectLike({
ToPort: 6379
}),
objectLike({
Match.objectLike({
ToPort: 12001
})
]
}))
})
})

it('should autoscale on CPU load', () => {
expectCDK(testStack).to(haveResource('AWS::AutoScaling::ScalingPolicy', {
Template.fromStack(testStack).hasResourceProperties('AWS::AutoScaling::ScalingPolicy', {
TargetTrackingConfiguration: {
PredefinedMetricSpecification: {
PredefinedMetricType: 'ASGAverageCPUUtilization'
},
TargetValue: 40
}
}))
})
})

it('should have an IAM role to read from s3', () => {
expectCDK(testStack).to(haveResourceLike('AWS::IAM::Policy', {
Template.fromStack(testStack).hasResourceProperties('AWS::IAM::Policy', {
PolicyDocument: {
Statement: arrayWith(objectLike({
Statement: [Match.objectLike({
Action: 's3:GetObject',
Resource: 'arn:aws:s3:::cdk-alb-test/*',
}))
})]
}
}))
})
})

it('should have a userdata script', () => {
expectCDK(testStack).to(haveResource('AWS::AutoScaling::LaunchConfiguration', {
Template.fromStack(testStack).hasResourceProperties('AWS::AutoScaling::LaunchConfiguration', {
UserData: {
"Fn::Base64": stringLike('*/testFile.txt*')
"Fn::Base64": Match.stringLikeRegexp('.*/testFile.txt.*')
}
}))
})
})
})
12 changes: 6 additions & 6 deletions src/stack.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env node
import autoscaling = require('@aws-cdk/aws-autoscaling')
import ec2 = require('@aws-cdk/aws-ec2')
import { ApplicationLoadBalancer } from '@aws-cdk/aws-elasticloadbalancingv2'
import { App, Stack } from '@aws-cdk/core'
import autoscaling = require('aws-cdk-lib/aws-autoscaling')
import ec2 = require('aws-cdk-lib/aws-ec2')
import { ApplicationLoadBalancer } from 'aws-cdk-lib/aws-elasticloadbalancingv2'
import { App, Stack } from 'aws-cdk-lib/core'

export class LoadBalancerStack extends Stack {
constructor(app: App, id: string) {
Expand Down Expand Up @@ -41,7 +41,7 @@ export class LoadBalancerStack extends Stack {
listener.connections.allowDefaultPortFromAnyIpv4('Open to the world')

asg.scaleOnRequestCount('AModestLoad', {
targetRequestsPerSecond: 1
targetRequestsPerMinute: 1
})
}
}
}

0 comments on commit 84fd4a2

Please sign in to comment.