sam-local inside CodeBuild Docker container
See original GitHub issueI have a fairly simple use-case. I want to run aws-sam-local as part of ci/cd. I use aws codebuild for building my backend. I would like run sam-local in the background as part of a post build process. Therefore, I can run integration tests against the local server and possibly fail the build before deploying.
I don’t have any issue running the NPM installed sam-local CLI in the foreground simply with:
sam local start-api --env-vars ./src/config/env/test.json
But when I try to use something like nohup per https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-background-tasks.html with the CLI, I get Connection refused
I also don’t have any issues running the https://github.com/cnadiminti/docker-aws-sam-local container in the foreground with the privileged flag set in my CodeBuild container. However, if I use the -d
parameter of Docker I get Connection reset by peer
Buildspec.yaml example:
- docker run -d -t
-v /var/run/docker.sock:/var/run/docker.sock
-v $(pwd):/var/opt
-p 3000:3000
cnadiminti/aws-sam-local
local start-api --env-vars ./config/env/local.json --docker-volume-basedir $(pwd) --host 0.0.0.0
Maybe I’m thinking about this the wrong way but it seems like running aws-sam-local in a container within my CodeBuild process and hitting the local server with my integration test suite would be suitable. I’m aware of the drawbacks of using Docker in Docker.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:4
- Comments:5 (2 by maintainers)
Not so long ago I created localci to run CodeBuild locally using official AWS Docker images to help me inspect some problems during AWS CodeBuild runtime. You may try it or just run CodeBuild’s Docker images locally and try out to resolve your issue - they are well-documented and easy to play with.
Personally, after 4 months of using
sam-local
I cannot recommend it to rely on it during some functional/postman tests instart-api
mode. I’d rather go with mocking event payload (from API Gateway) and invidually call all methods which requires such functional testing. In my humble opinnion, there is still a lot of differences if you do acurl
request to real deployment on API Gateway with AWS Lambda and toaws-sam-local
which mimics them (i.e. retrieving resources after gzip/deflate compression). During functional testsaws-sam-local
might be an extra PoF which may cause false-positive/negative test results.Closing. Docker in Docker is not something we currently support. Please run same outside of docker to use sam local or sam build --use-container.