question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

sam build with --use container doesn't work if sam is running inside a container

See original GitHub issue

I am trying to build my lambda package inside a container with latest aws-sam-cli installed. Consider a scenario with a Jenkins or any CI server running inside containers.

Here are the reproduction steps:

  • Run a container in interactive mode with aws-sam-cli installed. docker run -v /var/run/docker.sock:/var/run/docker.sock -ti --entrypoint bash samcli:latest
  • Run sam Build command sam build -u -t template.yml
  • Expected: Sam build should be able to executed with --use-container option
  • Actual: Getting an error with Requirements file not found: /tmp/samcli/source/requirements.txt

root@d2da5d926823:/# sam build -u -t aws.cfn.app.yml Starting Build inside a container Building codeuri: / runtime: python3.8 metadata: {} functions: [‘KafkaLagLambdaFn’] Fetching public.ecr.aws/sam/build-python3.8:latest Docker container image… Mounting / as /tmp/samcli/source:ro,delegated inside runtime container Build Failed Running PythonPipBuilder:ResolveDependencies Error: PythonPipBuilder:ResolveDependencies - Requirements file not found: /tmp/samcli/source/requirements.txt

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
sd65commented, May 25, 2021

Hi all,

@aahung I got the same error with a nodejs Lambda :

$ sam build --use-container Starting Build inside a container Building codeuri: /builds/xxx/app runtime: nodejs14.x metadata: {} functions: [‘yyy’] Fetching public.ecr.aws/sam/build-nodejs14.x:latest Docker container image… Mounting /builds/xxx/app as /tmp/samcli/source:ro,delegated inside runtime container Build Failed Running NodejsNpmBuilder:NpmPack Error: NodejsNpmBuilder:NpmPack - NPM Failed: npm ERR! code ENOLOCAL npm ERR! Could not install from “…/source” as it does not contain a package.json file. npm ERR! A complete log of this run can be found in: npm ERR! /root/.npm/_logs/2021-05-25T11_23_12_715Z-debug.log

As you said sam is not really to blame here, it’s a docker-in-docker catch : the sam container is trying to bind mount the working dir. But the mount path has to be correct from the host perspective, and chances are that’s not possible without doing nasty things (like recreating the docker volume driver path/filesystem in the container).

One nice thing would be able to add additional docker volumes to a sam container (should we add a feature request ?). That’s almost already implemented but not available in the SAM build CLI interface. The nice thing about named docker volumes are that they don’t suffer from the host path perspective “problem”.

Finally, what I recommend to fix this issue if you are running sam in a CI/CD container runner : don’t use your usual Docker image for your CI/CD container runner docker executor and directly use AWS SAM lambda container images (the images ran if you specify --use-container). For GitLab, that’s the image keyword. They already contains sam and other tools to build and manage dependency in your lambda runtime choice. Find those images here. That way, if your function depends on packages have natively compiled dependencies, they will be built in AWS Lambda-like Docker container, as --use-container does. That way, you can remove this flag from your CI/CD script and now it works !

0reactions
mew1033commented, Jan 27, 2022

This is also a problem when building functions that use container images (https://docs.aws.amazon.com/lambda/latest/dg/images-create.html).

Read more comments on GitHub >

github_iconTop Results From Across the Web

sam build - AWS Serverless Application Model
Build an AWS SAM application using the sam build command from the AWS SAM CLI. ... build inside a Lambda-like Docker container $...
Read more >
AWS SAM DockerBuildArgs It does not add them when ...
I am trying to test a lambda function locally, the function is created from the public docker image from aws, however I want...
Read more >
Building applications - Amazon Serverless Application Model
Use the sam build command to build an Amazon SAM serverless application as a .zip file archive or a container image.
Read more >
SAM & Docker Builds-- How do they update (not working for ...
When I go to the Dockerfile and the app.py to make changes and expand the text analysis functions I am trying to use,...
Read more >
Let's Try Lambda Container Support with SAM CLI - hayao-k.dev
The AWS SAM CLI supports container images in v1.13.1. ... If you use your own base image, you can run tests by adding...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found