Home > database >  CDK pipeline to deploy to S3 bucket from GitHub
CDK pipeline to deploy to S3 bucket from GitHub

Time:01-12

I am trying to write cdk pipeline to setup s3 website whenever I commit to my github. I was able to setup the static website using CDK. however I am not sure how to progress with cdk pipeline to copy github repo contents to s3 bucket whenever there is a commit.

I was wondering if anyone can provide some guidance on the following

  1. How to setup "Start the pipeline on source code change"

  2. How to deploy (copy) the repo contents to S3 bucket


    import * as cdk from "aws-cdk-lib";
    import * as codecommit from "aws-cdk-lib/aws-codecommit";
    import * as pipelines from "aws-cdk-lib/pipelines";
    import { CodePipeline, CodePipelineSource } from "aws-cdk-lib/pipelines";
    
    import { Construct } from "constructs";
    
    export class WorkshopPipeLineStack extends cdk.Stack {
      constructor(scope: Construct, id: string, props?: cdk.StackProps) {
        super(scope, id, props);
    
        const source = pipelines.CodePipelineSource.gitHub(
          "kasukur/s3-website",
          "main"
        );
    
        const pipeline = new pipelines.CodePipeline(scope, "MyPipeline", {
          synth: new pipelines.ShellStep("Synth", {
            input: source,
            commands: [],
            env: {
              COMMIT_ID: source.sourceAttribute("CommitId"),
            },
          }),
        });
      }
    }

CodePudding user response:

TL;DR Use a codepipeline.Pipeline construct with a S3 Deploy Action.


You've got the wrong tool for the job. The pipelines.CodePipeline construct (aka CDK Pipeline) is a specialist pipeline construct for deploying CDK apps. That's not what you want1. Instead, use the codepipeline.Pipeline construct, a more general-purpose tool2. A pipelines.CodePipeline consists of stages. Stages have actions:

Source Action: GitHubSourceAction using your GitHub OAuth token in Secrets Manager or the newer CodeStarConnectionAction that uses a CodeStar connection (= GitHub app) to connect to your repo.

[Build Actions, Test Actions]

Deploy Action: a S3DeployAction outputs your artefact to S3


  1. Each CDK Pipeline stage must contain at least one CDK stack or will fail on synth.

  2. If you just need a simple copy-paste from GitHub to S3 on a commit, a standalone CodeBuild project configured for Github can do the job without a pipeline.

  • Related