DEV Community

Cover image for Bootstrapping AWS CDK Automation With Amazon CodeCatalyst

Bootstrapping AWS CDK Automation With Amazon CodeCatalyst

Abstract

  • A step-by-step guide on establishing an AWS CDK setup alongside Amazon CodeCatalyst from the ground up, enabling the creation of a comprehensive CI/CD pipeline for your infrastructure.
  • AWS CDK is fantastic for overseeing your entire infrastructure as code, but when multiple developers are involved in modifying the infrastructure, the situation can become chaotic without a proper mechanism like a CI/CD pipeline. Absence of such a system makes coordinating and communicating changes to the infrastructure a challenging task, and this challenge amplifies as more individuals participate in the modification process.
  • This tutorial will guide you through setting up a CI/CD pipeline using Amazon CodeCatalyst and AWS CDK for building To-Do web application

Table Of Contents


๐Ÿš€ Setting up a CodeCatalyst Project, Repo, and Environment

  1. Login to CodeCatalyst and go to your Space (Create one if you don't have)
  2. Create a project from scratch

  1. Create repository to store code and workflows of the project

  1. Create CICD Environments which associates to AWS account for deploying our infrastructure.

  1. Create IAM role for codecatalyst to consume during running workflows. It should be already created while you create the Space or you can customize the others

๐Ÿš€ Design workflows

  • Workflows directory
  .codecatalyst
  โ””โ”€โ”€ workflows
      โ””โ”€โ”€ main_fullstack_workflow.yaml
Enter fullscreen mode Exit fullscreen mode
  • Workflows is triggered by PUSH of branch main and includes following Actions
  Triggers:
    - Branches:
        - main
      Type: PUSH
Enter fullscreen mode Exit fullscreen mode
  1. FrontendBuildAndPackage build react app, target build which is shared to cross-actions by Artifacts of Outputs

    FrontendBuildAndPackage:
      Identifier: aws/build@v1
      Inputs:
        Sources:
          - WorkflowSource
      Outputs:
        Artifacts:
          - Name: frontend
            Files:
              - "**/*"
      Configuration:
        Steps:
          - Run: cd static-assets/frontend
          - Run: npm install
          - Run: echo "REACT_APP_SERVICE_URL=/api/todos" > ".env"
          - Run: npm run build
    
  2. FrontendTest Test frontend code

    FrontendTest:
        Identifier: aws/managed-test@v1
        Inputs:
          Sources:
            - WorkflowSource
        Outputs:
          AutoDiscoverReports:
            IncludePaths:
              - frontend/**/*.xml
            ExcludePaths:
              - frontend/node_modules/**/*
            ReportNamePrefix: AutoDiscovered
            Enabled: true
            SuccessCriteria:
              PassRate: 100
        Configuration:
          Steps:
            - Run: cd static-assets/frontend
            - Run: npm install
            - Run: npm test -- --coverage --watchAll=false;
    
  3. CDKBootstrapAction Run cdk bootstrap for the region of the account with latest CDK version. This action depends on FrontendTest and FrontendBuildAndPackage

    CDKBootstrapAction:
      Identifier: aws/cdk-bootstrap@v1
      Configuration:
        Region: us-east-1
        CdkCliVersion: latest
      Environment:
        Name: default_environment
        Connections:
          - Name: "123456789012"
            Role: CodeCatalystWorkflowDevelopmentRole-simflexcloud
      DependsOn:
        - FrontendTest
        - FrontendBuildAndPackage
    
  4. CDKDeploy Download build target of FrontendBuildAndPackage and trigger cdk deploy, this action depends on CDKBootstrapAction. Here I don't use the defined action aws/cdk-deploy@v1 of CodeCatalyst because I'd like to use projen and pnmp in CDK and handle copying frontend target build

    CDKDeploy:
      Identifier: aws/build@v1
      Inputs:
        Artifacts:
          - frontend
      Outputs:
        AutoDiscoverReports:
          IncludePaths:
            - "**/*"
          ExcludePaths:
            - "*/.codecatalyst/workflows/*"
          ReportNamePrefix: AutoDiscovered
          Enabled: true
      Configuration:
        Steps:
          - Run: cp -r static-assets/frontend/build static-assets/cdkStack/src/lib/frontend/
          - Run: cd static-assets/cdkStack
          - Run: npm install -g pnpm
          - Run: pnpm i --no-frozen-lockfile
          - Run: export CDK_DEPLOY_ACCOUNT=123456789012
          - Run: export CDK_DEPLOY_REGION=us-east-1
          - Run: pnpm dlx projen deploy --all --require-approval never
      Environment:
        Name: default_environment
        Connections:
          - Name: "123456789012"
            Role: CodeCatalystWorkflowDevelopmentRole-simflexcloud
      DependsOn:
        - FrontendTest
        - FrontendBuildAndPackage
    
  • Use EC2 compute type for CodeCatalyst workflows
  Compute:
    Type: EC2
    Fleet: Linux.x86-64.Large
Enter fullscreen mode Exit fullscreen mode

๐Ÿš€ Source code and CDK stacks

  • Structure
    • cdkStack Define CDK stacks and use projen for configuration management as well as pnpm
    • frontend Frontend react app
  static-assets
  โ”œโ”€โ”€ cdkStack
  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ LICENSE
  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ README.md
  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ cdk.json
  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ package.json
  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ src
  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ bin
  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ main.ts
  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ lib
  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ backend
  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ lambda
  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ CorsAPIGatewayProxyResult.ts
  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ Todo.ts
  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ addTodo.ts
  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ deleteTodo.ts
  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ getTodo.ts
  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ getTodos.ts
  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ updateTodo.ts
  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ todo-api-stack.ts
  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ frontend
  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย      โ”œโ”€โ”€ build
  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย      โ”œโ”€โ”€ constants.ts
  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย      โ””โ”€โ”€ frontend-stack.ts
  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ main.ts
  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ test
  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ todo-api.test.ts
  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ tsconfig.dev.json
  โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ tsconfig.json
  โ”‚ย ย  โ””โ”€โ”€ frontend
  โ”‚ย ย      โ”œโ”€โ”€ README.md
  โ”‚ย ย      โ”œโ”€โ”€ babel.config.js
  โ”‚ย ย      โ”œโ”€โ”€ jest.config.js
  โ”‚ย ย      โ”œโ”€โ”€ package.json
  โ”‚ย ย      โ”œโ”€โ”€ public
  โ”‚ย ย      โ”‚ย ย  โ”œโ”€โ”€ index.html
  โ”‚ย ย      โ”‚ย ย  โ”œโ”€โ”€ manifest.json
  โ”‚ย ย      โ”‚ย ย  โ””โ”€โ”€ robots.txt
  โ”‚ย ย      โ”œโ”€โ”€ src
  โ”‚ย ย      โ”‚ย ย  โ”œโ”€โ”€ App.css
  โ”‚ย ย      โ”‚ย ย  โ”œโ”€โ”€ App.test.tsx
  โ”‚ย ย      โ”‚ย ย  โ”œโ”€โ”€ App.tsx
  โ”‚ย ย      โ”‚ย ย  โ”œโ”€โ”€ index.css
  โ”‚ย ย      โ”‚ย ย  โ”œโ”€โ”€ index.tsx
  โ”‚ย ย      โ”‚ย ย  โ”œโ”€โ”€ react-app-env.d.ts
  โ”‚ย ย      โ”‚ย ย  โ”œโ”€โ”€ reportWebVitals.ts
  โ”‚ย ย      โ”‚ย ย  โ”œโ”€โ”€ setupTests.ts
  โ”‚ย ย      โ”‚ย ย  โ”œโ”€โ”€ to-do.api.ts
  โ”‚ย ย      โ”‚ย ย  โ””โ”€โ”€ to-do.types.ts
  โ”‚ย ย      โ””โ”€โ”€ tsconfig.json
  โ”œโ”€โ”€ tsconfig.dev.json
  โ”œโ”€โ”€ tsconfig.json
  โ””โ”€โ”€ yarn.lock
Enter fullscreen mode Exit fullscreen mode

๐Ÿš€ Push source code to repo

  • Init the repo and add repo URL which is created from the above as origin


  โžœ  git init
  Initialized empty Git repository in /Users/vudao/workspace/codecatalyst/cdk-todo-web-app/.git/
  โžœ  git remote add origin https://vumdao@git.us-west-2.codecatalyst.aws/v1/simflexcloud/cdk-todo-web-app/cdk-todo-web-app
  โžœ  git branch --set-upstream-to=origin/main main
  branch 'main' set up to track 'origin/main' by rebasing.
  โžœ  git pull
  โžœ  git add .
  โžœ  git commit -m "Init commit"
  โžœ  git push origin main
Enter fullscreen mode Exit fullscreen mode

๐Ÿš€ Workflows Runs

  • When the commit is pushed to the main branch, CodeCatalyst CI/CD triggers the workflows

  • The CDKDeploy triggers cloudformation to create AWS resources

  • After the workflows done, we now have the To-Do Web app UI

๐Ÿš€ Conclusion

Congratulations! You've successfully bootstrapped and initialized AWS CDK with CodeCatalyst, and you can now deploy infrastructure changes or update frontend/backend using a pull request workflow.


๐ŸŒ  Blog ยท Github ยท stackoverflow ยท Linkedin ยท Group ยท Page ยท Twitter ๐ŸŒ 

Top comments (0)