通过aws codebuild为nodejs lambda创建zip文件的问题

7 amazon-web-services node.js aws-lambda aws-codebuild

我想通过aws codebuild进程为我的nodejs lambda创建一个zip工件 - 这样lambda函数可以在S3中使用这个zip文件作为源代码,我们在codebuild中使用git commit id进行管理的部署"证明"

我在github-repo中的文件结构是

folder1
   - myfile.js
   - otherfile.js
folder2
   - otherfiles.js
package.json
Run Code Online (Sandbox Code Playgroud)

现在对于nodejs lambda项目我想要没有zip文件夹的zip文件(我们需要lambda中的nodejs项目)所以zip应该直接包含以下文件

- myfile.js
- node_module ==> folder from codebuild via npm install command 
Run Code Online (Sandbox Code Playgroud)

问题:

1)S3中的输出zip包含在文件夹即.zip-> rootfolder-> myfile.js而不是我们需要.zip-> myfiles.js这对于lambda是不可用的,因为对于nodejs它应该有root zip文件而不是里面他们(文件夹内没有相对路径)

2)路径 - 你可以看到myfile.js在一个文件夹里面我想要相对路径被省略 - 我试过丢弃路径但问题是所有的node_module文件也在文件夹而不是在文件夹中,因为丢弃路径适用于两者 - 我可以只为myfile.js而不是为node_module文件夹设置discard路径吗?我目前的yaml文件:

artifacts:
  files:
    - folder/myfile.js
    - node_modules/**/*
  discard-paths: yes 
Run Code Online (Sandbox Code Playgroud)

如果有人可以为此提供解决方案,那会很棒吗?

如果解决方案不包含更改github-repo文件夹结构并且我想在该repo中为其他文件重复此操作以创建其他lambda函数,那将是很好的.

编辑:

我使用下面的yaml文件,@awsnitin回答后一切正常

version: 0.2

phases:
  build:
    commands:
      - echo Build started on `date`
      - npm install
  post_build:
    commands:
      - echo Running post_build commands
      - mkdir build-output
      - cp -R folder1/myfile.js build-output
      - mkdir -p build-output/node_modules
      - cp -R node_modules/* build-output/node_modules
      - cd build-output/
      - zip -qr build-output.zip ./*
      - mv build-output.zip ../
      - echo Build completed on `date`
artifacts:
  files:
    - build-output.zip
Run Code Online (Sandbox Code Playgroud)

aws*_*tin 8

不幸的是丢弃路径在这种情况下不起作用.最佳选择是将必要的文件作为构建逻辑(buildspec.yml)的一部分复制到新文件夹,并在工件部分中指定该文件夹.这是一个示例buildspec文件

post_build:
    commands:
      - mkdir build-output
      - cp -R folder/myfile.js node_modules/ build-output
artifacts:
  files:
    - build-output/**/*
Run Code Online (Sandbox Code Playgroud)