issue creating zip file through aws codebuild for nodejs lambda
Asked Answered
S

2

8

I want to create a zip artifact for my nodejs lambda through aws codebuild process - so that lambda function can use this zip file as source in S3 and we have a deployment "proof" for management with git commit id in codebuild

my file structure in github-repo is

folder1
   - myfile.js
   - otherfile.js
folder2
   - otherfiles.js
package.json

now for nodejs lambda project I want zip file without the folder in zip (we need that for nodejs project in lambda) so zip should directly contain following files

- myfile.js
- node_module ==> folder from codebuild via npm install command 

issues:

1) output zip in S3 contains in folder i.e .zip->rootfolder->myfile.js rather than we require .zip->myfiles.js this is not usable by lambda as for nodejs it should have files in root zip and not inside them (no relative path inside folder)

2) paths - as you can see myfile.js is inside a folder I want to relative path to be omitted - I tried discard path but problem with that is all the node_module files are also in folder rather than in folder as discard path applies to both of them - Can I set discard path only for myfile.js and not for node_module folder ?? my current yaml file:

artifacts:
  files:
    - folder/myfile.js
    - node_modules/**/*
  discard-paths: yes 

It would be great if someone can provide solution to this?

It would be great if solution does not contain changing github-repo folder structure and I want to repeat this for other files too in that repo for creating other lambda functions.

Edit:

I used below yaml file and everything is working fine after @awsnitin answer

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
Shipwreck answered 30/6, 2017 at 19:41 Comment(0)
T
9

Unfortunately discard-paths would not work in this case. Best option is to copy the necessary files to a new folder as part of your build logic (buildspec.yml) and specify that folder in the artifacts section. Here's a sample buildspec file

post_build:
    commands:
      - mkdir build-output
      - cp -R folder/myfile.js node_modules/ build-output
artifacts:
  files:
    - build-output/**/*
Tonsil answered 30/6, 2017 at 20:23 Comment(2)
doing this the build-output folder will be put inside the root folder of the S3. Anyway to extract the content of the build-output folder so the files will be in the root? Adding discard-paths will just remove all the sub-folders paths.Isaacisaacs
Use base-directory: 'build-output' and remove 'build-output' from the "files:" portion of your code to remove the parent 'build-output' directory when deploying to S3Judaea
Q
0

I just struggled with this issue with Python, but I think my solution applies the to Node.js since both relies on .zip files. Following is my buildspec.yml:

artifacts:
  files:
    - '**/*'
  base-directory: target

The code above zips the files under the target directory (where my .py and packages were pip-installed or, in the case of Node.js, all the .js and node_modules) and save it to the S3 bucket. No need to zip anything before, neither to list it as an artifact file. I used CodeBuild from within CodeFile so the https://s3bucket/.../random-part is, indeed, a zip file generated from the buildspec.yml/artifacts/files/base-directory declaration.

Quantum answered 17/10, 2021 at 2:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.