Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export PATH to "sam build" command?

I'm developing AWS Lambda function on PyCharm. When I do "Run" button, Following error message.

/usr/local/bin/sam build MyFunction --template /Users/miyashiiii/Works/myapp/myapp/template.yaml --build-dir /Users/miyashiiii/Works/myapp/myapp/.aws-sam/build
Building codeuri: myapp/ runtime: python3.7 metadata: {} functions: ['MyFunction']

Build Failed
Error: PythonPipBuilder:Validation - Binary validation failed for python, searched for python in following locations  : ['/usr/bin/python'] which did not satisfy constraints for runtime: python3.7. Do you have python for runtime: python3.7 on your PATH?

python3.7 is on my PATH, and other some locations is on my PATH, but locations list includes only '/usr/bin/python'.

And when I do same command (/usr/local/bin/sam build ~~) on terminal, It succeed.

How to export PATH to "sam build" command?

like image 789
miyashiiii Avatar asked Dec 07 '20 11:12

miyashiiii


People also ask

What does Sam build command do?

The sam build command processes your AWS SAM template file, application code, and any applicable language-specific files and dependencies. The command also copies build artifacts in the format and location expected for subsequent steps in your workflow.

Does AWS CLI include Sam?

AWS SAM provides you with a command line tool, the AWS SAM CLI, that makes it easy for you to create and manage serverless applications. You need to install and configure a few things in order to use the AWS SAM CLI. To use the Amazon Web Services Documentation, Javascript must be enabled.

What is Makefile in Sam?

Your makefile is responsible for compiling the custom runtime if necessary, and copying the build artifacts into the proper location required for subsequent steps in your workflow. The location of the makefile is specified by the CodeUri property of the function resource, and must be named Makefile .


2 Answers

I solved the problem for python3.8 (same for python3.7 I suppose) by linking executable into /usr/local/bin

ln -sf /usr/local/opt/[email protected]/bin/python3.8 /usr/local/bin/

[EDIT Nov2021] it's /opt/homebrew/opt/[email protected]/bin/python3 nowadays [/EDIT]

To find your python directory if installed by homebrew: brew info [email protected]

Also, you'd probably prefer running SAM inside a docker container (option sam build --use-container with CLI)

In pycharm: Activate the build function inside a container

like image 179
Vincent J Avatar answered Oct 18 '22 11:10

Vincent J


SAM expects your version of Python do be on PATH, as seen here

To find your python directory if installed by homebrew: brew info [email protected]

In my case, this yielded /usr/local/opt/[email protected]/libexec/bin

Then add that to PATH with:

export PATH=$PATH:<Your Python version's path here>
export PATH=$PATH:/usr/local/opt/[email protected]/libexec/bin

You can confirm that it has been appended by running echo $PATH, and you should see your new path at the end of the output.

sam build should then find your Python version

like image 38
Lee James Avatar answered Oct 18 '22 12:10

Lee James