* Build a Lambda-compatible Docker image from an existing image * @param {Object} params - Build parameters * @param {string} params.imageUri - The name of the existing Docker image * @param {number} [params.port=8080] - The port to expose in the Lambda image * @param {boolean} [params.l
({ imageUri, port = 8080, local = false })
| 482 | * @returns {Promise<string>} The URI of the built Lambda Docker image |
| 483 | */ |
| 484 | async buildImageAwsLambda({ imageUri, port = 8080, local = false }) { |
| 485 | const buildOutput = [] |
| 486 | |
| 487 | // Remove -lambda suffix for base image if present |
| 488 | const baseImageUri = imageUri.replace('-lambda', '') |
| 489 | this.logger.debug(`Building AWS Lambda Docker image: ${imageUri}`) |
| 490 | |
| 491 | let dockerfileContent = `FROM ${baseImageUri} |
| 492 | COPY --from=public.ecr.aws/lambda/nodejs:20 /usr/local/bin/aws-lambda-rie /aws-lambda-rie |
| 493 | COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.8.4 /lambda-adapter /opt/extensions/lambda-adapter |
| 494 | ENV AWS_LWA_PORT=${port}` |
| 495 | |
| 496 | if (local) { |
| 497 | dockerfileContent = `${dockerfileContent} |
| 498 | ENTRYPOINT ["/aws-lambda-rie", "--runtime-interface-emulator-address", "0.0.0.0:9000", "/cnb/process/web"]` |
| 499 | } |
| 500 | |
| 501 | // Only append -lambda if it's not already present |
| 502 | const lambdaImageUri = imageUri.endsWith('-lambda') |
| 503 | ? imageUri |
| 504 | : imageUri.includes(':') |
| 505 | ? `${imageUri}-lambda` |
| 506 | : `${imageUri}:lambda` |
| 507 | |
| 508 | return await new Promise((resolve, reject) => { |
| 509 | const dockerProcess = spawn('docker', [ |
| 510 | 'build', |
| 511 | '--provenance=false', |
| 512 | '--platform=linux/amd64', |
| 513 | '--load', |
| 514 | '-t', |
| 515 | lambdaImageUri, |
| 516 | '-f-', |
| 517 | '.', |
| 518 | ]) |
| 519 | |
| 520 | dockerProcess.stdin.write(dockerfileContent) |
| 521 | dockerProcess.stdin.end() |
| 522 | |
| 523 | dockerProcess.stdout.on('data', (data) => |
| 524 | this.#handleBuildOutput({ buildOutput, data }), |
| 525 | ) |
| 526 | |
| 527 | dockerProcess.stderr.on('data', (data) => |
| 528 | this.#handleBuildOutput({ buildOutput, data }), |
| 529 | ) |
| 530 | |
| 531 | dockerProcess.on('error', (error) => { |
| 532 | const buildError = this.#handleBuildFailure({ |
| 533 | containerName: lambdaImageUri, |
| 534 | buildOutput, |
| 535 | error, |
| 536 | }) |
| 537 | reject(buildError) |
| 538 | }) |
| 539 | |
| 540 | dockerProcess.on('close', (code) => { |
| 541 | if (code === 0) { |
no test coverage detected