diff --git a/src/cdk-workshop-pipeline-stage.ts b/src/cdk-workshop-pipeline-stage.ts new file mode 100644 index 0000000..3c6195c --- /dev/null +++ b/src/cdk-workshop-pipeline-stage.ts @@ -0,0 +1,14 @@ +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); + + 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 new file mode 100644 index 0000000..035e8a5 --- /dev/null +++ b/src/cdk-workshop-pipeline.ts @@ -0,0 +1,37 @@ +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 CDKPipelineStack extends Stack { + constructor(scope: Construct, id: string) { + super(scope, id) + + 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"] + }) + }) + + 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 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, + }); } } diff --git a/src/cdk-workshop.ts b/src/cdk-workshop.ts index 535e876..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 { CdkWorkshopStack } from "./cdk-workshop-stack.js"; +import { CDKPipelineStack } from "./cdk-workshop-pipeline.js"; const app = new App(); -new CdkWorkshopStack(app, "CdkWorkshopStack"); +new CDKPipelineStack(app, "CdkWorkshopPipelineStack");