From 8c26f4a1459c729ab5bd980c0c85998de1857363 Mon Sep 17 00:00:00 2001 From: Henadzi Shutko Date: Sun, 5 Nov 2023 19:42:47 +0300 Subject: [PATCH 1/7] feat: add pipeline stack to deploy application --- src/cdk-workshop-pipeline.ts | 13 +++++++++++++ src/cdk-workshop.ts | 4 ++-- 2 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 src/cdk-workshop-pipeline.ts diff --git a/src/cdk-workshop-pipeline.ts b/src/cdk-workshop-pipeline.ts new file mode 100644 index 0000000..ad84219 --- /dev/null +++ b/src/cdk-workshop-pipeline.ts @@ -0,0 +1,13 @@ +import { Stack } from "aws-cdk-lib"; +import { Repository } from "aws-cdk-lib/aws-codecommit"; +import { Construct } from "constructs"; + +export class PipelineStack extends Stack { + constructor(scope: Construct, id: string) { + super(scope, id) + + const repo = new Repository(this, "CDKWorkshopRepository", { + repositoryName: "CDKWorkshopRepository" + }) + } +} \ No newline at end of file diff --git a/src/cdk-workshop.ts b/src/cdk-workshop.ts index 535e876..2dccfcb 100644 --- a/src/cdk-workshop.ts +++ b/src/cdk-workshop.ts @@ -1,6 +1,6 @@ #!/usr/bin/env node import { App } from "aws-cdk-lib"; -import { CdkWorkshopStack } from "./cdk-workshop-stack.js"; +import { PipelineStack } from "./cdk-workshop-pipeline.js"; const app = new App(); -new CdkWorkshopStack(app, "CdkWorkshopStack"); +new PipelineStack(app, "CdkWorkshopPipelineStack"); From e548923648f33611cb8195dcf448bb9a0050a3ea Mon Sep 17 00:00:00 2001 From: Henadzi Shutko Date: Sun, 5 Nov 2023 20:11:15 +0300 Subject: [PATCH 2/7] feat: add pipeline --- src/cdk-workshop-pipeline.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/cdk-workshop-pipeline.ts b/src/cdk-workshop-pipeline.ts index ad84219..14b34be 100644 --- a/src/cdk-workshop-pipeline.ts +++ b/src/cdk-workshop-pipeline.ts @@ -1,5 +1,6 @@ import { Stack } from "aws-cdk-lib"; import { Repository } from "aws-cdk-lib/aws-codecommit"; +import { CodeBuildStep, CodePipeline, CodePipelineSource } from "aws-cdk-lib/pipelines"; import { Construct } from "constructs"; export class PipelineStack extends Stack { @@ -9,5 +10,14 @@ export class PipelineStack extends Stack { const repo = new Repository(this, "CDKWorkshopRepository", { repositoryName: "CDKWorkshopRepository" }) + + const pipeline = new CodePipeline(this, "CDKWorkshopPipeline", { + pipelineName: "CDKWorkshopPipeline", + synth: new CodeBuildStep("Step1", { + input: CodePipelineSource.codeCommit(repo, "feature-pipeline"), + installCommands: ["npm install -g aws-cdk"], + commands: ["npm ci", "npm run build", "npx cdk synth"] + }) + }) } } \ No newline at end of file From 6aacf3a86c4529148a9d4e311eec12a23f41ef0e Mon Sep 17 00:00:00 2001 From: Henadzi Shutko Date: Sun, 5 Nov 2023 20:31:57 +0300 Subject: [PATCH 3/7] feat: add deploy stage to pipeline --- src/cdk-workshop-pipeline-stage.ts | 11 +++++++++++ src/cdk-workshop-pipeline.ts | 6 +++++- src/cdk-workshop.ts | 4 ++-- 3 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 src/cdk-workshop-pipeline-stage.ts diff --git a/src/cdk-workshop-pipeline-stage.ts b/src/cdk-workshop-pipeline-stage.ts new file mode 100644 index 0000000..a3cbe08 --- /dev/null +++ b/src/cdk-workshop-pipeline-stage.ts @@ -0,0 +1,11 @@ +import { Stage } from "aws-cdk-lib"; +import { Construct } from "constructs"; +import { CdkWorkshopStack } from "./cdk-workshop-stack"; + +export class CDKPipelineStage extends Stage { + constructor(scope: Construct, id: string) { + super(scope, id); + + const stack = new CdkWorkshopStack(scope, "CDKWorkshopStack"); + } +} \ No newline at end of file diff --git a/src/cdk-workshop-pipeline.ts b/src/cdk-workshop-pipeline.ts index 14b34be..cbbebb2 100644 --- a/src/cdk-workshop-pipeline.ts +++ b/src/cdk-workshop-pipeline.ts @@ -2,8 +2,9 @@ import { Stack } from "aws-cdk-lib"; import { Repository } from "aws-cdk-lib/aws-codecommit"; import { CodeBuildStep, CodePipeline, CodePipelineSource } from "aws-cdk-lib/pipelines"; import { Construct } from "constructs"; +import { CDKPipelineStage } from "./cdk-workshop-pipeline-stage"; -export class PipelineStack extends Stack { +export class CDKPipelineStack extends Stack { constructor(scope: Construct, id: string) { super(scope, id) @@ -19,5 +20,8 @@ export class PipelineStack extends Stack { commands: ["npm ci", "npm run build", "npx cdk synth"] }) }) + + const stage = new CDKPipelineStage(this, "Stage1"); + pipeline.addStage(stage); } } \ No newline at end of file diff --git a/src/cdk-workshop.ts b/src/cdk-workshop.ts index 2dccfcb..75c2335 100644 --- a/src/cdk-workshop.ts +++ b/src/cdk-workshop.ts @@ -1,6 +1,6 @@ #!/usr/bin/env node import { App } from "aws-cdk-lib"; -import { PipelineStack } from "./cdk-workshop-pipeline.js"; +import { CDKPipelineStack } from "./cdk-workshop-pipeline.js"; const app = new App(); -new PipelineStack(app, "CdkWorkshopPipelineStack"); +new CDKPipelineStack(app, "CdkWorkshopPipelineStack"); From 6ae3a815a9c3d071bee064881d9f69f346b15387 Mon Sep 17 00:00:00 2001 From: Henadzi Shutko Date: Sun, 5 Nov 2023 20:41:09 +0300 Subject: [PATCH 4/7] fix: remove redundant const declaration --- src/cdk-workshop-pipeline-stage.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cdk-workshop-pipeline-stage.ts b/src/cdk-workshop-pipeline-stage.ts index a3cbe08..11baee1 100644 --- a/src/cdk-workshop-pipeline-stage.ts +++ b/src/cdk-workshop-pipeline-stage.ts @@ -6,6 +6,6 @@ export class CDKPipelineStage extends Stage { constructor(scope: Construct, id: string) { super(scope, id); - const stack = new CdkWorkshopStack(scope, "CDKWorkshopStack"); + new CdkWorkshopStack(scope, "CDKWorkshopStack"); } } \ No newline at end of file From df975790a9febd3dbc124b917730f95c5a599f14 Mon Sep 17 00:00:00 2001 From: Henadzi Shutko Date: Sun, 5 Nov 2023 20:46:37 +0300 Subject: [PATCH 5/7] fix: pass stage as a main stack's scope --- src/cdk-workshop-pipeline-stage.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cdk-workshop-pipeline-stage.ts b/src/cdk-workshop-pipeline-stage.ts index 11baee1..ec2d577 100644 --- a/src/cdk-workshop-pipeline-stage.ts +++ b/src/cdk-workshop-pipeline-stage.ts @@ -6,6 +6,6 @@ export class CDKPipelineStage extends Stage { constructor(scope: Construct, id: string) { super(scope, id); - new CdkWorkshopStack(scope, "CDKWorkshopStack"); + new CdkWorkshopStack(this, "CDKWorkshopStack"); } } \ No newline at end of file From 84c3e1d76c4010549d7c00bc964455c3b9418ea2 Mon Sep 17 00:00:00 2001 From: Henadzi Shutko Date: Sun, 5 Nov 2023 20:58:17 +0300 Subject: [PATCH 6/7] feat: add gateway public url to outputs --- src/cdk-workshop-stack.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/cdk-workshop-stack.ts b/src/cdk-workshop-stack.ts index 5c5777b..cae292b 100644 --- a/src/cdk-workshop-stack.ts +++ b/src/cdk-workshop-stack.ts @@ -1,10 +1,12 @@ -import { Stack, StackProps } from "aws-cdk-lib"; +import { CfnOutput, Stack, StackProps } from "aws-cdk-lib"; import { LambdaRestApi } from "aws-cdk-lib/aws-apigateway"; import { Construct } from "constructs"; import { HitCounter } from "./constructs/hitcounter.js"; import { Hello } from "./constructs/hello.js"; export class CdkWorkshopStack extends Stack { + public readonly endpointOutput: CfnOutput; + constructor(scope: Construct, id: string, props?: StackProps) { super(scope, id, props); @@ -14,8 +16,12 @@ export class CdkWorkshopStack extends Stack { downstream: hello.handler, }); - new LambdaRestApi(this, "Endpoint", { + const gateway = new LambdaRestApi(this, "Endpoint", { handler: hitCounter.handler, }); + + this.endpointOutput = new CfnOutput(this, "GatewayUrl", { + value: gateway.url, + }); } } From 4544445141d11d2c616e4893b8a955649439fb4f Mon Sep 17 00:00:00 2001 From: Henadzi Shutko Date: Sun, 5 Nov 2023 21:22:07 +0300 Subject: [PATCH 7/7] test: add integration test after deploy stage of the pipeline --- src/cdk-workshop-pipeline-stage.ts | 7 +++++-- src/cdk-workshop-pipeline.ts | 14 ++++++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/cdk-workshop-pipeline-stage.ts b/src/cdk-workshop-pipeline-stage.ts index ec2d577..3c6195c 100644 --- a/src/cdk-workshop-pipeline-stage.ts +++ b/src/cdk-workshop-pipeline-stage.ts @@ -1,11 +1,14 @@ -import { Stage } from "aws-cdk-lib"; +import { CfnOutput, Stage } from "aws-cdk-lib"; import { Construct } from "constructs"; import { CdkWorkshopStack } from "./cdk-workshop-stack"; export class CDKPipelineStage extends Stage { + public readonly gateWayUrlOutput: CfnOutput; + constructor(scope: Construct, id: string) { super(scope, id); - new CdkWorkshopStack(this, "CDKWorkshopStack"); + const application = new CdkWorkshopStack(this, "CDKWorkshopStack"); + this.gateWayUrlOutput = application.endpointOutput; } } \ No newline at end of file diff --git a/src/cdk-workshop-pipeline.ts b/src/cdk-workshop-pipeline.ts index cbbebb2..035e8a5 100644 --- a/src/cdk-workshop-pipeline.ts +++ b/src/cdk-workshop-pipeline.ts @@ -21,7 +21,17 @@ export class CDKPipelineStack extends Stack { }) }) - const stage = new CDKPipelineStage(this, "Stage1"); - pipeline.addStage(stage); + const deploy = new CDKPipelineStage(this, "Stage1"); + const deployStage = pipeline.addStage(deploy); + + deployStage.addPost( + new CodeBuildStep("TestGatewayEndpoint", { + projectName: "TestGatewayEndpoint", + envFromCfnOutputs: { + ENDPOINT_URL: deploy.gateWayUrlOutput + }, + commands: ["curl -Ssf $ENDPOINT_URL", "curl -Ssf $ENDPOINT_URL/hello", "curl -Ssf $ENDPOINT_URL/users"] + }) + ) } } \ No newline at end of file