From a29a16d647dac900292bb8999cda4054746dd560 Mon Sep 17 00:00:00 2001 From: Frank Cao Date: Fri, 3 Feb 2023 17:15:17 +1100 Subject: [PATCH] feat: migrate to CDK v2 --- .../bin/amazon-msk-java-app-cdk.ts | 2 +- amazon-msk-java-app-cdk/cdk.json | 30 ++++++++++++++----- amazon-msk-java-app-cdk/lib/dynamodb-stack.ts | 10 +++---- amazon-msk-java-app-cdk/lib/fargate-stack.ts | 19 ++++++------ amazon-msk-java-app-cdk/lib/kafka-stack.ts | 11 +++---- .../lib/kafka-topic-stack.ts | 17 ++++++----- amazon-msk-java-app-cdk/lib/lambda-stack.ts | 13 ++++---- amazon-msk-java-app-cdk/lib/vpc-stack.ts | 7 +++-- amazon-msk-java-app-cdk/package.json | 27 +++++++---------- amazon-msk-java-app-cdk/tsconfig.json | 3 +- scripts/deploy.sh | 0 scripts/destroy.sh | 0 12 files changed, 78 insertions(+), 61 deletions(-) mode change 100644 => 100755 scripts/deploy.sh mode change 100644 => 100755 scripts/destroy.sh diff --git a/amazon-msk-java-app-cdk/bin/amazon-msk-java-app-cdk.ts b/amazon-msk-java-app-cdk/bin/amazon-msk-java-app-cdk.ts index a66d296..ac179b9 100644 --- a/amazon-msk-java-app-cdk/bin/amazon-msk-java-app-cdk.ts +++ b/amazon-msk-java-app-cdk/bin/amazon-msk-java-app-cdk.ts @@ -17,7 +17,7 @@ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ import 'source-map-support/register'; -import * as cdk from '@aws-cdk/core'; +import * as cdk from 'aws-cdk-lib'; import {KafkaStack} from '../lib/kafka-stack'; import {VpcStack} from "../lib/vpc-stack"; import {DynamoDbStack} from "../lib/dynamodb-stack"; diff --git a/amazon-msk-java-app-cdk/cdk.json b/amazon-msk-java-app-cdk/cdk.json index ff13a3d..76b9401 100644 --- a/amazon-msk-java-app-cdk/cdk.json +++ b/amazon-msk-java-app-cdk/cdk.json @@ -1,12 +1,28 @@ { "app": "npx ts-node --prefer-ts-exts bin/amazon-msk-java-app-cdk.ts", "context": { - "@aws-cdk/core:enableStackNameDuplicates": "true", - "aws-cdk:enableDiffNoFail": "true", - "@aws-cdk/core:stackRelativeExports": "true", - "@aws-cdk/aws-ecr-assets:dockerIgnoreSupport": true, - "@aws-cdk/aws-secretsmanager:parseOwnedSecretName": true, - "@aws-cdk/aws-kms:defaultKeyPolicies": true, - "@aws-cdk/aws-s3:grantWriteWithoutAcl": true + "@aws-cdk/aws-lambda:recognizeLayerVersion": true, + "@aws-cdk/core:checkSecretUsage": true, + "@aws-cdk/core:target-partitions": [ + "aws", + "aws-cn" + ], + "@aws-cdk-containers/ecs-service-extensions:enableDefaultLogDriver": true, + "@aws-cdk/aws-ec2:uniqueImdsv2TemplateName": true, + "@aws-cdk/aws-ecs:arnFormatIncludesClusterName": true, + "@aws-cdk/aws-iam:minimizePolicies": true, + "@aws-cdk/core:validateSnapshotRemovalPolicy": true, + "@aws-cdk/aws-codepipeline:crossAccountKeyAliasStackSafeResourceName": true, + "@aws-cdk/aws-s3:createDefaultLoggingPolicy": true, + "@aws-cdk/aws-sns-subscriptions:restrictSqsDescryption": true, + "@aws-cdk/aws-apigateway:disableCloudWatchRole": true, + "@aws-cdk/core:enablePartitionLiterals": true, + "@aws-cdk/aws-events:eventsTargetQueueSameAccount": true, + "@aws-cdk/aws-iam:standardizedServicePrincipals": true, + "@aws-cdk/aws-ecs:disableExplicitDeploymentControllerForCircuitBreaker": true, + "@aws-cdk/aws-iam:importedRoleStackSafeDefaultPolicyName": true, + "@aws-cdk/aws-s3:serverAccessLogsUseBucketPolicy": true, + "@aws-cdk/aws-route53-patters:useCertificate": true, + "@aws-cdk/customresources:installLatestAwsSdkDefault": false } } diff --git a/amazon-msk-java-app-cdk/lib/dynamodb-stack.ts b/amazon-msk-java-app-cdk/lib/dynamodb-stack.ts index debd9a5..4404680 100644 --- a/amazon-msk-java-app-cdk/lib/dynamodb-stack.ts +++ b/amazon-msk-java-app-cdk/lib/dynamodb-stack.ts @@ -15,19 +15,19 @@ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -import * as dynamodb from "@aws-cdk/aws-dynamodb"; -import {AttributeType} from "@aws-cdk/aws-dynamodb"; -import * as cdk from "@aws-cdk/core"; +import * as dynamodb from "aws-cdk-lib/aws-dynamodb"; +import * as cdk from 'aws-cdk-lib'; +import { Construct } from "constructs"; export class DynamoDbStack extends cdk.Stack { private tableName = "Accounts"; - constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) { + constructor(scope: Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); const table = new dynamodb.Table(this, "dynamoDbTable", { tableName: this.tableName, - partitionKey: {name: "id", type: AttributeType.STRING} + partitionKey: {name: "id", type: dynamodb.AttributeType.STRING} }); } } diff --git a/amazon-msk-java-app-cdk/lib/fargate-stack.ts b/amazon-msk-java-app-cdk/lib/fargate-stack.ts index 8b2ed00..6e03f9b 100644 --- a/amazon-msk-java-app-cdk/lib/fargate-stack.ts +++ b/amazon-msk-java-app-cdk/lib/fargate-stack.ts @@ -15,21 +15,22 @@ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -import * as cdk from "@aws-cdk/core"; -import {CfnParameter} from "@aws-cdk/core"; -import * as ecs from "@aws-cdk/aws-ecs"; -import * as ec2 from "@aws-cdk/aws-ec2"; -import {InstanceClass, InstanceSize} from "@aws-cdk/aws-ec2"; +import * as cdk from 'aws-cdk-lib'; +import {CfnParameter} from "aws-cdk-lib"; +import * as ecs from "aws-cdk-lib/aws-ecs"; +import * as ec2 from "aws-cdk-lib/aws-ec2"; +import {InstanceClass, InstanceSize} from "aws-cdk-lib/aws-ec2"; import {VpcStack} from "./vpc-stack"; -import * as assets from "@aws-cdk/aws-ecr-assets"; -import * as iam from "@aws-cdk/aws-iam"; -import {Effect} from "@aws-cdk/aws-iam"; +import * as assets from "aws-cdk-lib/aws-ecr-assets"; +import * as iam from "aws-cdk-lib/aws-iam"; +import {Effect} from "aws-cdk-lib/aws-iam"; +import { Construct } from 'constructs'; export class FargateStack extends cdk.Stack { private tableName = "Accounts"; private groupId = "transaction-consumers"; - constructor(vpcStack: VpcStack, scope: cdk.Construct, id: string, props?: cdk.StackProps) { + constructor(vpcStack: VpcStack, scope: Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); let bootstrapAddress = new CfnParameter(this, "bootstrapAddress", { diff --git a/amazon-msk-java-app-cdk/lib/kafka-stack.ts b/amazon-msk-java-app-cdk/lib/kafka-stack.ts index 1d6dfa2..f4f825c 100644 --- a/amazon-msk-java-app-cdk/lib/kafka-stack.ts +++ b/amazon-msk-java-app-cdk/lib/kafka-stack.ts @@ -15,22 +15,23 @@ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -import * as cdk from '@aws-cdk/core'; -import * as msk from '@aws-cdk/aws-msk'; -import * as ec2 from '@aws-cdk/aws-ec2' +import * as cdk from 'aws-cdk-lib'; +import * as msk from 'aws-cdk-lib/aws-msk'; +import * as ec2 from 'aws-cdk-lib/aws-ec2'; import {VpcStack} from "./vpc-stack"; +import { Construct } from 'constructs'; export class KafkaStack extends cdk.Stack { public kafkaCluster: msk.CfnCluster; - constructor(vpcStack: VpcStack, scope: cdk.Construct, id: string, props?: cdk.StackProps) { + constructor(vpcStack: VpcStack, scope: Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); this.kafkaCluster = new msk.CfnCluster(this, "kafkaCluster", { brokerNodeGroupInfo: { securityGroups: [vpcStack.kafkaSecurityGroup.securityGroupId], clientSubnets: [...vpcStack.vpc.selectSubnets({ - subnetType: ec2.SubnetType.PRIVATE + subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS }).subnetIds], instanceType: "kafka.t3.small", storageInfo: { diff --git a/amazon-msk-java-app-cdk/lib/kafka-topic-stack.ts b/amazon-msk-java-app-cdk/lib/kafka-topic-stack.ts index cfd6c04..000b692 100644 --- a/amazon-msk-java-app-cdk/lib/kafka-topic-stack.ts +++ b/amazon-msk-java-app-cdk/lib/kafka-topic-stack.ts @@ -15,19 +15,20 @@ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -import * as cdk from "@aws-cdk/core"; -import {CfnParameter, CustomResource, Duration} from "@aws-cdk/core"; +import * as cdk from 'aws-cdk-lib'; +import {CfnParameter, CustomResource, Duration} from "aws-cdk-lib"; import {VpcStack} from "./vpc-stack"; import {KafkaStack} from "./kafka-stack"; -import {NodejsFunction} from "@aws-cdk/aws-lambda-nodejs"; -import {Runtime} from "@aws-cdk/aws-lambda"; -import {Effect, PolicyStatement} from "@aws-cdk/aws-iam"; -import {Provider} from "@aws-cdk/custom-resources"; -import {RetentionDays} from "@aws-cdk/aws-logs"; +import {NodejsFunction} from "aws-cdk-lib/aws-lambda-nodejs"; +import {Runtime} from "aws-cdk-lib/aws-lambda"; +import {Effect, PolicyStatement} from "aws-cdk-lib/aws-iam"; +import {Provider} from "aws-cdk-lib/custom-resources"; +import {RetentionDays} from "aws-cdk-lib/aws-logs"; +import { Construct } from 'constructs'; export class KafkaTopicStack extends cdk.Stack { - constructor(vpcStack: VpcStack, kafkaStack: KafkaStack, scope: cdk.Construct, id: string, props?: cdk.StackProps) { + constructor(vpcStack: VpcStack, kafkaStack: KafkaStack, scope: Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); const bootstrapAddress = new CfnParameter(this, "bootstrapAddress", { diff --git a/amazon-msk-java-app-cdk/lib/lambda-stack.ts b/amazon-msk-java-app-cdk/lib/lambda-stack.ts index 33b6d3b..df3db9f 100644 --- a/amazon-msk-java-app-cdk/lib/lambda-stack.ts +++ b/amazon-msk-java-app-cdk/lib/lambda-stack.ts @@ -15,16 +15,17 @@ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -import * as cdk from "@aws-cdk/core"; -import {CfnParameter, Duration} from "@aws-cdk/core"; +import * as cdk from "aws-cdk-lib"; +import {CfnParameter, Duration} from "aws-cdk-lib"; import {VpcStack} from "./vpc-stack"; -import {NodejsFunction} from "@aws-cdk/aws-lambda-nodejs"; -import {Effect, PolicyStatement} from "@aws-cdk/aws-iam"; +import {NodejsFunction} from "aws-cdk-lib/aws-lambda-nodejs"; +import {Effect, PolicyStatement} from "aws-cdk-lib/aws-iam"; import {KafkaStack} from "./kafka-stack"; -import {Runtime} from "@aws-cdk/aws-lambda" +import {Runtime} from "aws-cdk-lib/aws-lambda" +import { Construct } from "constructs"; export class LambdaStack extends cdk.Stack { - constructor(vpcStack: VpcStack, kafkaStack: KafkaStack, scope: cdk.Construct, id: string, props?: cdk.StackProps) { + constructor(vpcStack: VpcStack, kafkaStack: KafkaStack, scope: Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); let bootstrapAddress = new CfnParameter(this, "bootstrapAddress", { diff --git a/amazon-msk-java-app-cdk/lib/vpc-stack.ts b/amazon-msk-java-app-cdk/lib/vpc-stack.ts index 542da0a..9cd883b 100644 --- a/amazon-msk-java-app-cdk/lib/vpc-stack.ts +++ b/amazon-msk-java-app-cdk/lib/vpc-stack.ts @@ -15,8 +15,9 @@ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -import * as cdk from "@aws-cdk/core"; -import * as ec2 from "@aws-cdk/aws-ec2"; +import * as cdk from "aws-cdk-lib"; +import * as ec2 from "aws-cdk-lib/aws-ec2"; +import { Construct } from "constructs"; export class VpcStack extends cdk.Stack { public vpc: ec2.Vpc; @@ -24,7 +25,7 @@ export class VpcStack extends cdk.Stack { public fargateSercurityGroup: ec2.SecurityGroup; public lambdaSecurityGroup: ec2.SecurityGroup; - constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) { + constructor(scope: Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); this.vpc = new ec2.Vpc(this, 'vpc'); diff --git a/amazon-msk-java-app-cdk/package.json b/amazon-msk-java-app-cdk/package.json index d42f090..ec7066e 100644 --- a/amazon-msk-java-app-cdk/package.json +++ b/amazon-msk-java-app-cdk/package.json @@ -11,23 +11,18 @@ "cdk": "cdk" }, "devDependencies": { - "@aws-cdk/assert": "1.96.0", - "@types/jest": "^26.0.10", - "@types/node": "10.17.27", - "aws-cdk": "1.96.0", - "jest": "^26.4.2", - "ts-jest": "^26.2.0", - "ts-node": "^9.0.0", - "typescript": "~3.9.7" + "@aws-cdk/assert": "2.63.0", + "@types/jest": "^29.4.0", + "@types/node": "18.11.18", + "jest": "^29.4.1", + "ts-jest": "^29.0.5", + "aws-cdk": "2.63.0", + "ts-node": "^10.9.1", + "typescript": "~4.9.4" }, "dependencies": { - "@aws-cdk/aws-dynamodb": "1.96.0", - "@aws-cdk/aws-ec2": "1.96.0", - "@aws-cdk/aws-ecs": "1.96.0", - "@aws-cdk/aws-iam": "1.96.0", - "@aws-cdk/aws-lambda-nodejs": "1.96.0", - "@aws-cdk/aws-msk": "1.96.0", - "@aws-cdk/core": "1.96.0", - "source-map-support": "0.5.19" + "aws-cdk-lib": "2.63.0", + "constructs": "^10.0.0", + "source-map-support": "^0.5.21" } } diff --git a/amazon-msk-java-app-cdk/tsconfig.json b/amazon-msk-java-app-cdk/tsconfig.json index ec75123..20b19f9 100644 --- a/amazon-msk-java-app-cdk/tsconfig.json +++ b/amazon-msk-java-app-cdk/tsconfig.json @@ -1,5 +1,6 @@ { "compilerOptions": { + "outDir": "./dist", "target": "ES2018", "module": "commonjs", "lib": ["es2018"], @@ -19,5 +20,5 @@ "strictPropertyInitialization": false, "typeRoots": ["./node_modules/@types"] }, - "exclude": ["cdk.out"] + "exclude": ["cdk.out", "node_modules", "dist"] } diff --git a/scripts/deploy.sh b/scripts/deploy.sh old mode 100644 new mode 100755 diff --git a/scripts/destroy.sh b/scripts/destroy.sh old mode 100644 new mode 100755