AWS CDK and AWS CodePipeline

0

Hello, I'm creating an infrastructure in AWS with a Dynamo table and some lambdas for a "CRUD" functionality. The lambdas simply allow adding records, deleting, querying, etc. (I don't include all of them as they are not related to the issue and it makes the code easier to read).

However, when I add the code for AWS CodePipeline, I encounter the following error:

[Container] 2023/05/23 13:00:26 Command did not exit successfully npx cdk synth exit status 1
[Container] 2023/05/23 13:00:26 Phase complete: BUILD State: FAILED
[Container] 2023/05/23 13:00:26 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: npx cdk synth. Reason: exit status 1

How could I solve it? I'm new in AWS and I'm quite disoriented. The code in the lib folder I'm using is:

  const nuevoRegistroFunction = new lambda.Function(this, "NuevoRegistroFunction", {
      runtime: lambda.Runtime.NODEJS_14_X,
      handler: 'handler.nuevoRegistro', 
      code: lambda.Code.fromAsset(path.resolve(__dirname, 'lambda')),
      environment: {
        TABLA_REGISTROS: tablaRegistros.tableName,
      },
    });

    tablaRegistros.grantReadWriteData(nuevoRegistroFunction);

    const registrosAPI = new apigw.RestApi(this, "RegistrosApi");

    registrosAPI.root
      .resourceForPath("registros")
      .addMethod("PUT", new apigw.LambdaIntegration(nuevoRegistroFunction))
  
    new CodePipeline(this, 'Pipeline', {
      pipelineName: 'TestPipeline',
      synth: new ShellStep('Synth', {
        input: CodePipelineSource.gitHub('ivozmediano/microservicios', 'main'),
        commands: [
                    'npm ci',
                    'npm run build',
                    'npx cdk synth',
                  ]
      }),
    });
  }
}

Thanks a lot!

1 Answer
-1

Since a Docker container is used to build Lambda, it is necessary to set "dockerEnabledForSynth: true" to enable privileged mode during build.
https://docs.aws.amazon.com/cdk/api/v1/docs/@aws-cdk_pipelines.CodePipeline.html

So I thought this is how it would look as a code.
Note that "dockerEnabledForSelfMutation: true" must also be added if Docker image Assets are used on CDK Pipelines.

    new CodePipeline(this, 'Pipeline', {
      pipelineName: 'TestPipeline',
      synth: new ShellStep('Synth', {
        input: CodePipelineSource.gitHub('ivozmediano/microservicios', 'main'),
        commands: [
                    'npm ci',
                    'npm run build',
                    'npx cdk synth',
                  ]
      }),
      dockerEnabledForSynth: true
    });
  }
}
profile picture
EXPERT
answered a year ago
  • Hi Riku, I tried your solution but it's still not working, same error. Thanks a lot anyway!

  • By the way, if I clone CodePipelineSource.gitHub('ivozmediano/microservicios', 'main') locally and run "npm ci", "npm run build", "npx cdk synth" locally, I get error?

  • Probably, because of credentials

  • Are there any errors in the npm run build execution log when you run the CDK? You may also want to check if you have successfully cloned from GitHub.

  • There are no errors in npm run build locally. I tried a different node version and, locally, I can run npx cdk synth, but when I try to deploy code using CodePipeline, error again.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions