3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AWS CDKのスタックをGitHubからCodePipeline連携で自動更新する

Posted at

LambdaをGitHubの1つのリポジトリでまとめて管理する時などに、AWS CDKで管理して、
CodePipeline連携で自動更新できると便利だと思ったので作成しました。

環境

CDK CLI: 1.27.0

コード

CDKリポジトリのlibディレクトリ内に以下のtsファイルを作成します。

cdk-pipeline-stack.ts

import * as cdk from '@aws-cdk/core';
import * as codebuild from '@aws-cdk/aws-codebuild'
import * as codepipeline from '@aws-cdk/aws-codepipeline'
import * as codepipeline_actions from '@aws-cdk/aws-codepipeline-actions'
import * as iam from '@aws-cdk/aws-iam'

export class CdkPipelineStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props)

    const build = new codebuild.PipelineProject(this,'testBuild',{
      buildSpec: codebuild.BuildSpec.fromObject({
        version: '0.2',
        phases: {
          install: {
            commands: [
              'npm install -g aws-cdk@1.27.0',
              'npm install -g typescript'
            ]
          },
          pre_build: {
            commands: 'npm i'
          },
          post_build: {
            commands: [
              'cdk deploy --require-approval never'
            ]
          },
        },
      }),
      projectName: 'cdk-deploy'
    })
    build.addToRolePolicy(new iam.PolicyStatement({
      resources: ['*'],
      actions: ['*']
    }))
    const sourceOutput = new codepipeline.Artifact()
    const repositoryName = '管理したいGitHubのリポジトリ名'
    const owner = 'リポジトリの所有者名'
    const oauthToken = 'GitHubのトークン'
    const branch = 'master'

    const sourceAction = new codepipeline_actions.GitHubSourceAction ({
        actionName: 'Github_Source',
        owner: owner,
        repo: repositoryName,
        branch: branch,
        oauthToken: cdk.SecretValue.plainText(oauthToken),
        trigger: codepipeline_actions.GitHubTrigger.WEBHOOK,
        output: sourceOutput
    })

    const buildOutput = new codepipeline.Artifact()
    const buildAction = new codepipeline_actions.CodeBuildAction({
      actionName: 'CodeBuild',
      project: build,
      input: sourceOutput,
      outputs: [buildOutput]
    })

    new codepipeline.Pipeline(this, 'pipeline', {
      pipelineName: 'cdk-deploy-pipeline',
      stages: [
        {
          stageName: 'Source',
          actions: [
            sourceAction
          ],
        },
        {
          stageName: 'Build',
          actions: [
            buildAction
          ],
        }
      ]
    })
  }
}

このスタックをデプロイすると、CodePipelineが作成されます。
対象となるGitHubのリポジトリのmasterブランチの更新を契機に、
cdk deployが実行されて、対象リポジトリのスタックが更新されます。

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?