Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible/recommended to use `sam build` in AWS CodeBuild?

This question spun out of this one. Now that I have a better understanding of what was going wrong there, and a workable, if imperfect, solution, I'm submitting a more focused follow-up (I'm still something of a novice at StackOverflow - please let me know if this contravenes etiquette, and I should follow-up on the original).

This page suggests that "You use AWS CodeBuild to build, locally test, and package your serverless application". However, when I include a sam build command in my buildspec.yml, I get the following log output, suggesting that sam is not installed on CodeBuild images:

[Container] 2018/12/31 11:41:49 Running command sam build --use-container 
sh: 1: sam: not found 

[Container] 2018/12/31 11:41:49 Command did not exit successfully sam build --use-container exit status 127 
[Container] 2018/12/31 11:41:49 Phase complete: BUILD Success: false 
[Container] 2018/12/31 11:41:49 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: sam build --use-container. Reason: exit status 127 

Furthermore, if I install SAM with pip install aws-sam-cli, running sam build --use-container in CodeBuild gives an error. sam build itself succeeds, but since it doesn't install test dependencies, I'd still need to use pip install -r tests/requirements-test.txt -t . to be able to run tests in CodeBuild. Moreover, this suggests that --use-container is required for "packages that have natively compiled programs").

This makes me wonder whether I'm trying to do something wrong. What's the recommended way of building SAM services in a CI/CD workflow on AWS?

like image 385
scubbo Avatar asked Dec 31 '18 11:12

scubbo


1 Answers

2019_10_18 - Update (confirming @Spiff answer above):

Apparently Codebuild now work seamlessly with SAM, that's all I needed in buildspec.yml for a lambda using pandas and psycopg2-binary:

version: 0.2

phases:
  install:
    runtime-versions:
      python: 3.7

  pre_build:
    commands:
      - python -m unittest discover tests

  build:
    commands:
      - sam build

  post_build:
    commands:
      - sam package --output-template-file packaged.yaml --s3-bucket my-code-pipeline-bucketz

artifacts:
  type: zip
  files:
    - packaged.yaml

Cheers

like image 86
saza Avatar answered Nov 16 '22 03:11

saza