You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS Lambda Docker容器文件写入失败问题求助

AWS Lambda Docker容器写入文件失败问题

我构建了一个用于AWS Lambda的Docker容器,功能为从S3下载图片到本地临时目录、生成3个标记文件后再上传回S3。函数执行到downloadImageFromS3步骤时,在fs.writeFileSync(downloadPath, data.Body)处失败。曾尝试设置WORKDIR /app,但此时lambda.js无法找到npm安装的依赖包;改用${LAMBDA_TASK_ROOT}后仍出现文件写入错误,请求解决。

Dockerfile内容

# Use AWS Lambda's base image for Node.js with Alpine Linux
FROM public.ecr.aws/lambda/nodejs:20

# Set the working directory in the container
#WORKDIR /app

# Copy package.json and package-lock.json
COPY package*.json ${LAMBDA_TASK_ROOT}

# Install production dependencies
RUN npm install

# Install aws-sdk
RUN npm install aws-sdk

# Copy the rest of the application code to the container
COPY . ${LAMBDA_TASK_ROOT}
#COPY lambda.js ${LAMBDA_TASK_ROOT}

# Command to run the app when invoked in AWS Lambda
CMD ["lambda.handler"]

lambda.js文件内容

const AWS = require('aws-sdk');
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');

// Configure S3 client
const s3 = new AWS.S3();

async function downloadImageFromS3(bucket, key, downloadPath) {
    const params = { Bucket: bucket, Key: key };
    console.log('getting s3 object with params: ', params);
    const data = await s3.getObject(params).promise();
    console.log('writing file to download path: ', downloadPath);
    fs.writeFileSync(downloadPath, data.Body);
}

async function uploadMarkerToS3(bucket, key, filePath) {
    console.log('reading file content from key: ', key);
    const fileContent = fs.readFileSync(filePath);
    const params = {
        Bucket: bucket,
        Key: key,
        Body: fileContent
    };
    console.log('saving s3 object with params: ', params);
    await s3.putObject(params).promise();
}

async function generateMarker() {
    const imagePath = '/app/images/sample.png';  // Image path inside container
    const outputDir = '/app/output';  // Output directory inside container

    // Run marker generation
    const command = `node app.js -i ${imagePath}`;
    console.log('generating marker with command: ', command);
    execSync(command, { encoding: 'utf-8' });
}

exports.handler = async function(event) {
    console.log('event is: ', event);

    // Get environment variables passed from Lambda
    const bucket = event.S3_BUCKET;
    const key = event.S3_KEY;
    const outputPrefix = event.OUTPUT_PREFIX || 'markers';

    console.log('config: bucket - ', bucket);
    console.log('config: key - ', key);
    console.log('config: outputPrefix - ', outputPrefix);

    console.log('list: ', execSync(`ls -la`, {encoding: 'utf-8'}));
    console.log('env...: ', process.env.LAMBDA_TASK_ROOT);

    const localImagePath = `${process.env.LAMBDA_TASK_ROOT}/images/sample.png`;
    const localOutputDir = `${process.env.LAMBDA_TASK_ROOT}/output`;

    // 1. Download the image from S3
    console.log('downloading from s3 to local path')
    await downloadImageFromS3(bucket, key, localImagePath);
    console.log('s3 download complete')

    // 2. Run marker generation
    console.log('generating marker')
    await generateMarker();
    console.log('marker generation completed')

    // 3. Upload generated marker files to S3
    console.log('reading files from output directory: ', localOutputDir);
    const outputFiles = fs.readdirSync(localOutputDir);
    console.log('output files are: ', outputFiles);
    for (const file of outputFiles) {
        console.log('file object: ', file);
        const outputFilePath = path.join(localOutputDir, file);
        console.log('file path: ', outputFilePath);
        const s3Key = `${outputPrefix}/${file}`;
        console.log('s3 file key: ', s3Key);
        console.log('uploading file to s3')
        await uploadMarkerToS3(bucket, s3Key, outputFilePath);
        console.log('file uploaded to s3');
    }

    console.log('Marker generation and upload completed successfully.');
};

错误日志

START RequestId: 98428165-89de-4e11-b2bf-32c27d6e6c67 Version: $LATEST
2024-10-22T05:16:39.251Z    98428165-89de-4e11-b2bf-32c27d6e6c67    INFO    event is:  {
  S3_BUCKET: 'tapxreality-media',
  S3_KEY: 'media/uploads/422909c0-b46d-45e2-9692-8020b8324a58/2024/10/20241002_258276_46PM.png',
  OUTPUT_PREFIX: 'value3'
}
2024-10-22T05:16:39.305Z    98428165-89de-4e11-b2bf-32c27d6e6c67    INFO    config: bucket -  tapxreality-media
2024-10-22T05:16:39.305Z    98428165-89de-4e11-b2bf-32c27d6e6c67    INFO    config: key -  media/uploads/422909c0-b46d-45e2-9692-8020b8324a58/2024/10/20241002_258276_46PM.png
2024-10-22T05:16:39.305Z    98428165-89de-4e11-b2bf-32c27d6e6c67    INFO    config: outputPrefix -  value3
2024-10-22T05:16:39.468Z    98428165-89de-4e11-b2bf-32c27d6e6c67    INFO    list:  total 73
drwxr-xr-x   8 root root  1024 Oct 22 05:15 .
drwxr-xr-x   1 root root  4096 Oct 22 04:41 ..
-rw-r--r--   1 root root   458 Oct 21 13:27 Dockerfile
-rw-r--r--   1 root root  3023 Oct 21 13:18 README.md
-rw-r--r--   1 root root 19253 Oct 20 18:47 app.js
drwxr-xr-x   3 root root    60 Oct 20 12:10 demo
drwxr-xr-x   2 root root    60 Oct 20 12:25 input
-rw-r--r--   1 root root   530 Oct 22 02:46 lambda.Dockerfile
-rw-r--r--   1 root root  3462 Oct 22 05:15 lambda.js
drwxr-xr-x   2 root root  1024 Oct 20 12:10 libs
drwxr-xr-x 107 root root  3072 Oct 22 02:47 node_modules
drwxr-xr-x   2 root root  1024 Oct 20 12:44 output
-rw-r--r--   1 root root 33822 Oct 20 12:18 package-lock.json
-rw-r--r--   1 root root   426 Oct 21 17:55 package.json
drwxr-xr-x   2 root root    60 Oct 20 12:38 sample
2024-10-22T05:16:39.468Z    98428165-89de-4e11-b2bf-32c27d6e6c67    INFO    env...:  /var/task
2024-10-22T05:16:39.468Z    98428165-89de-4e11-b2bf-32c27d6e6c67    INFO    downloading from s3 to local path
2024-10-22T05:16:39.468Z    98428165-89de-4e11-b2bf-32c27d6e6c67    INFO    getting s3 object with params:  {
  Bucket: 'tapxreality-media',
  Key: 'media/uploads/422909c0-b46d-45e2-9692-8020b8324a58/2024/10/20241002_258276_46PM.png'
}
2024-10-22T05:16:40.929Z    98428165-89de-4e11-b2bf-32c27d6e6c67    INFO    writing file to download path:  /var/task/images/sample.png
2024-10-22T05:16:41.109Z    98428165-89de-4e11-b2bf-32c27d6e6c67    ERROR   Invoke Error    {"errorType":"Error","errorMessage":"ENOENT: no such file or directory, open '/var/task/images/sample.png'","code":"ENOENT","errno":-2,"syscall":"open","path":"/var/task/images/sample.png","stack":["Error: ENOENT: no such file or directory, open '/var/task/images/sample.png'","    at Object.openSync (node:fs:573:18)","    at Object.writeFileSync (node:fs:2380:35)","    at downloadImageFromS3 (/var/task/lambda.js:14:8)","    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)","    at async exports.handler (/var/task/lambda.js:65:9)"]}
END RequestId: 98428165-89de-4e11-b2bf-32c27d6e6c67
REPORT RequestId: 98428165-89de-4e11-b2bf-32c27d6e6c67  Duration: 2019.29 ms    Billed Duration: 3245 ms    Memory Size: 128 MB Max Memory Used: 99 MB  Init Duration: 1225.61 ms   

内容的提问来源于stack exchange,提问作者Anuj TBE

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 01:02:02