Deploying AWS CDK in a CodePipeline and CodeBuild workflow

I am currently attempting to deploy an AWS CDK application on AWS CodePipeline using CodeBuild actions.

While the build and deploy processes run smoothly locally (as expected), encountering an issue when running on CodeBuild where the cdk command fails with:

Cannot find module './index'
Subprocess exited with error 1

Despite its likely trivial nature, I can't seem to pinpoint the exact cause!

The project structure is auto-generated using cdk init --language typescript

<>/cdk$ ls
README.md  app  cdk.context.json  cdk.json  cdk.out  jest.config.js  lib  node_modules  package.json  test  tsconfig.json  yarn.lock

The buildspec.yml for the Build stage consists of:

phases:
  build:
    commands:
      - cd ${CODEBUILD_SRC_DIR}/cdk
      - yarn install
      - yarn build
artifacts:
  base-directory: ${CODEBUILD_SRC_DIR}/cdk
  files:
    - '**/*'

The buildspec.yml for the Deploy stage (where the input directory is the artifact from the Build stage i.e. the cdk directory) looks like:

phases:
  install:
    commands:
      - npm install -g aws-cdk
      - cdk --version

  build:
    commands:
      - cd ${CODEBUILD_SRC_DIR} # this is the cdk directory
      - cdk ls
      - cdk deploy app

During the Deploy stage, the Cannot find module './index' error occurs at the cdk ls step. Since the local build/deploy steps work flawlessly (in a clean checkout), the issue seems related to copying artifacts from the Build to Deploy stages. However, I'm having trouble identifying the exact problem. Any suggestions for troubleshooting?

Answer №1

An issue has been identified with CodeBuild, causing all symlinks to break when the artifact is created =>

The error message "Cannot find module './index'" occurs due to a command in your cdk.json file that uses ts-node. When cdk attempts to run it from node-modules/.bin/ts-node, the symlink is broken.

To address this issue, it is recommended to compress the code yourself during the build process. Here is an example of how you can achieve this:

      - yarn build
      - tar -czf /tmp/mycode.tar.gz .
artifacts:
  files:
    - 'mycode.tar.gz'
  discard-paths: true
  base-directory: '/tmp'

Upon deployment, you can decompress the code using the following steps:

...
      - cd ${CODEBUILD_SRC_DIR} # This refers to the cdk directory
      - tar -zxvf mycode.tar.gz
      - cdk ls
      - cdk deploy app

Answer №2

I encountered a similar problem and managed to fix it by including enable-symlinks: yes in my buildspec.yml file.

artifacts:
  enable-symlinks: yes

Answer №3

Do you currently implement Lambda code in CDK? Have you verified the handler being used and its presence at the specified path?

import * as lambda from '@aws-cdk/aws-lambda';
import * as path from 'path';

const fn = new lambda.Function(this, 'MyFunction', {
  runtime: lambda.Runtime.NODEJS_10_X,
  handler: 'dist/index.handler',  <======= Please ensure index.js file is located within the dist directory
  code: lambda.Code.fromAsset(path.join(__dirname, 'lambda-handler')),
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The attribute 'selectionStart' is not a valid property for the type 'EventTarget'

I'm currently utilizing the selectionStart and selectionEnd properties to determine the beginning and ending points of a text selection. Check out the code here: https://codesandbox.io/s/busy-gareth-mr04o Nevertheless, I am facing difficulties in id ...

Using TypeScript to incorporate JS web assembly into your project

I have been attempting to incorporate wasm-clingo into my TypeScript React project. I tried creating my own .d.ts file for the project: // wasm-clingo.d.ts declare module 'wasm-clingo' { export const Module: any; } and importing it like this: ...

Observable task queuing

Here's the scenario: In my application, the user can tap a button to trigger a task that takes 2 seconds to complete. I want to set up a queue to run these tasks one after another, in sequence. I am working with Ionic 3 and TypeScript. What would be ...

Different Approaches for Handling User Interactions in Angular Instead of Using the Deferred (Anti-?)Pattern

In the process of developing a game using Angular, I have implemented the following mechanics: An Angular service checks the game state and prompts a necessary user interaction. A mediator service creates this prompt and sends it to the relevant Angular c ...

The function "AAA" is utilizing the React Hook "useState" in a context that does not fit the requirements for either a React function component or a custom React Hook function

Upon reviewing this code snippet, I encountered an error. const AB= () => { const [A, setA] = useState<AT| null>(null); const [B, setB] = useState<string>('0px'); ..more} ...

An easy method to define argument types for every function type in TypeScript

How can I assign argument types to each function type in TypeScript? Each function has a parameter associated with it. [Example] type F1 = (arg: number) => void; type F2 = (arg: string) => void; const caller = (f: F1 | F2) => (n: number | strin ...

Angular version 10 does not allow for intentionally causing errors using the HttpClient delete() method

Currently, I'm working through an Angular tutorial and facing a challenge in the error-handling section. Despite my efforts to trigger an error with incorrect data in a HttpClient.delete()-request, I am unable to force one. To provide some context, I ...

Utilizing a GLTF asset as the main Scene element in a Three.js project

I'm struggling with incorporating a gltf model as the main scene in Three.js. Specifically, I have a gltf model of an apartment that I want to load from inside and not outside the apartment. I also need the controls to work seamlessly within the apart ...

Utilizing ES6 JavaScript for Creating Static Methods and Angular 2 Services

During the development of an Angular 2 app involving multiple calculation services, I encountered some interesting questions: Is it beneficial to use static in an Angular service provided on the application level? Or is it unnecessary? How does a static ...

Utilizing GitHub Actions and Python CDK to seamlessly deploy to AWS

My current project repository has the following structure: . ├── .github │ └── workflows/ ├── .gitignore ├── README.md ├── backend/ ├── cdk │ ├── .env │ ├── .gitignore │ ├── README.md ...

Attempting to integrate WebdriverIO into an Angular Electron application

Context: Currently, I am in the process of implementing the fundamental WebdriverIO example within an Angular Electron App. My application is built on the foundation of the Angular Electron Boilerplate. To set up, I have installed webdriverio and @types/we ...

Issue TS1112: It is not possible to declare a class member as optional

I'm currently working on creating a movie catalog using Angular and Ionic. Within the Movie class, I have properties for id, title, image, and plot. On the initial page of the app, only the id, title, and image are displayed, while the plot is omitte ...

Creating a new list by grouping elements from an existing list

I have successfully received data from my API in the following format: [ {grade: "Grade A", id: 1, ifsGrade: "A1XX", ifsType: "01XX", points: 22, type: "Type_1"}, {grade: "Grade B", id: 2, ifsGrade: &quo ...

Effortlessly glide through entire pages using the mouse wheel for seamless scrolling

I provide a seamless full-page scrolling experience using the mouse wheel. However, the scrollIntoView function does not seem to function within the @HostListener('wheel', ['$event']). Here is a snippet from app.component.html file: & ...

What is the best way to structure a nested object model in Angular?

Issue occurred when trying to assign the this.model.teamMembersDto.roleDto to teamMembersDto. The error message states that the property roleDto does not exist on type TeamMembersDropdownDto[], even though it is nested under teamMembersDto. If you look at ...

What is the best way to create a case-insensitive sorting key in ag-grid?

While working with grids, I've noticed that the sorting is case-sensitive. Is there a way to change this behavior? Here's a snippet of my code: columnDefs = [ { headerName: 'Id', field: 'id', sort: 'asc', sortabl ...

How to style Angular Material Dropdowns: Trimming the Left and Right Sides using CSS

Seeking to customize Angular Material Select to resemble a basic dropdown. Even after applying the disableOptionCentering, the dropdown list options still expand from the left and right sides (refer to Current picture below). The desired look would involve ...

Bovine without Redis to oversee queue operations

Can Bull (used for job management) be implemented without utilizing Redis? Here is a segment of my code: @Injectable() export class MailService { private queue: Bull.Queue; private readonly queueName = 'mail'; constructor() { ...

Storing information upon refresh in Angular 8

When it comes to inter-component communication in my Angular project, I am utilizing BehaviourSubject from RXJS. Currently, I have a setup with 3 components: Inquiry Form Where users enter an ID number to check for summon-related information. This data ...

Create a fresh type by dynamically adjusting/filtering its attributes

Suppose we have a type defined as follows: type PromiseFunc = () => Promise<unknown>; type A = { key1: string; key2: string; key3: PromiseFunc; key4: string; key5: PromiseFunc; key6: SomeOtherType1[]; key7: SomeOtherType2[]; key8: ...