Bootstrap FreeKB - Amazon Web Services (AWS) - Build Serverless Application Model (SAM) app using the sam build command
Amazon Web Services (AWS) - Build Serverless Application Model (SAM) app using the sam build command

Updated:   |  Amazon Web Services (AWS) articles

This assumes you have already installed the AWS Serverless Application Model (SAM) CLI. If not, check out my article Download and Install the AWS Serverless Application Model (SAM) CLI on Linux.

This also assume you have already initialized your AWS Serverless Application Model (SAM) app using the sam init command. If not, check out my article Initialize Serverless Application Model (SAM) app using the sam init command. Let's say your SAM app was initialized in the /tmp/sam-hello-wolrd/sam-app directory.

]$ ll /tmp/sam-hello-wolrd/sam-app
total 20
drwxrwxr-x 2 ec2-user ec2-user   24 Nov 16 02:13 events
drwxrwxr-x 2 ec2-user ec2-user   63 Nov 16 02:13 hello_world
-rw-rw-r-- 1 ec2-user ec2-user    0 Nov 16 02:13 __init__.py
-rw-rw-r-- 1 ec2-user ec2-user 8329 Nov 16 02:13 README.md
-rw-rw-r-- 1 ec2-user ec2-user  679 Nov 16 02:13 samconfig.toml
-rw-rw-r-- 1 ec2-user ec2-user 1681 Nov 16 02:13 template.yaml
drwxrwxr-x 4 ec2-user ec2-user   80 Nov 16 02:13 tests

 

This example requires Python 3.9. Often, your default python3 installation will be some other version of Python.

]$ python3 --version
Python 3.7.15

 

If you do not have Python 3.9 installed on your system, check out my article Install Python on Linux. If you follow my article, you should end up with Python 3.9 at /usr/local/bin/python3.9.6/bin/python3.9.

~]$ /usr/local/bin/python3.9.6/bin/python3.9 --version
Python 3.9.6

 

Let's make sure /usr/local/bin is in your $PATH.

~]$ echo $PATH
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/ec2-user/.local/bin:/home/ec2-user/bin

 

If /usr/local/bin is not in your $PATH let's add /usr/local/bin to your $PATH.

export PATH=$PATH:/usr/local/bin/python3.9.6/bin/

 

Let's move into the sam-app directory.

cd /tmp/sam-hello-world/sam-app/

 

And run sam build.

]$ sam build
Starting Build use cache
Manifest file is changed (new hash: 3298f13049d19cffaa37ca931dd4d421) or dependency folder (.aws-sam/deps/61826041-56e6-405d-a032-fa521eb522af) is missing for (HelloWorldFunction), downloading dependencies and copying/building source
Building codeuri: /tmp/sam-hello-world/sam-app/hello_world runtime: python3.9 metadata: {} architecture: x86_64 functions: HelloWorldFunction
 Running PythonPipBuilder:CleanUp
 Running PythonPipBuilder:ResolveDependencies
 Running PythonPipBuilder:CopySource
 Running PythonPipBuilder:CopySource

Build Succeeded

Built Artifacts  : .aws-sam/build
Built Template   : .aws-sam/build/template.yaml

Commands you can use next
=========================
[*] Validate SAM template: sam validate
[*] Invoke Function: sam local invoke
[*] Test Function in the Cloud: sam sync --stack-name {{stack-name}} --watch
[*] Deploy: sam deploy --guided

 

The sam build command will create the .aws-sam directory, and there should be files and directories in the .aws-sam directory.

~]$ ls -lisa /tmp/sam-hello-world/sam-app/.aws-sam/
total 4
 5506085 0 drwxrwxr-x 5 ec2-user ec2-user  62 Nov 16 02:44 .
12596741 0 drwxrwxr-x 6 ec2-user ec2-user 165 Nov 16 02:44 ..
11067259 0 drwxr-xr-x 3 ec2-user ec2-user  53 Nov 16 02:45 build
 5506086 4 -rw-rw-r-- 1 ec2-user ec2-user 385 Nov 16 02:45 build.toml
14939178 0 drwxr-xr-x 2 ec2-user ec2-user   6 Nov 16 02:44 cache
  433415 0 drwxr-xr-x 3 ec2-user ec2-user  50 Nov 16 02:45 deps

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 96d42e in the box below so that we can be sure you are a human.