Building a SAM Application
Explore the process of building serverless applications using AWS SAM CLI. Understand how to install and bundle dependencies, compile code when needed, and prepare your app for deployment. This lesson covers using SAM build commands effectively across supported programming languages to optimize Lambda function startup.
We'll cover the following...
Step 1: Build
Most modern software programming languages make it easy to reference third-party libraries and packages. For example, Node.js comes with NPM, a package manager that installs the libraries listed in the package.json project manifest. Python works with pip. Java has maven. In order for Lambda functions to start as quickly as possible, you need to install and bundle dependencies before uploading it to AWS. CloudFormation doesn’t know how to do that. However, the SAM command line tools know how to install and bundle dependencies for many common package managers. For compiled languages, such as Java and Go, SAM knows how to turn source files into executable code.
To prepare the function source for uploading to AWS, execute the following command in the main project directory (app). This will be the one that contains the CloudFormation template (template.yaml):
sam build
Note: Please add “C.UTF-8” in the LANG and LC_ALL field in the below terminal.
You should see a report that SAM built the HelloWorldFunction using npm:
$ sam build
...
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 copies project source files into a temporary subdirectory and runs the required packager to install all production dependencies for functions. It knows how to ignore test code, resources, and avoids bundling development dependencies. This means that you can safely install development tools in your source directory; SAM will ignore them when building the functions.
If your build process needs to compile binary executables, pass the --use-container option to sam build. This will execute the build process inside a Docker container that matches the Lambda runtime. For JavaScript, this is normally not needed. On the other hand, many Python libraries try to compile native dependencies and building inside a Lambda-like container is very useful for those cases.
SAM creates a temporary directory for build artefacts, defaulting to a subdirectory inside your project called .aws-sam. You can make it write the build results to a different location by using the --build-dir option. Check out the SAM Build documentation for more information on this and other options.
Building packages for other languages
SAM command-line tools support building projects in many languages, including JavaScript/TypeScript, Python, Ruby, Java, Go, Rust, and .NET. SAM also supports custom runtimes through the
Makefilebuild method, allowing you to define custom build steps for any language or framework. For the latest list of supported runtimes and build methods, check the SAM CLI documentation.If your chosen packaging system is not yet supported, you will have to somehow bundle dependencies and remove development tools before uploading the code to CloudFormation. In this case, you can skip the
sam buildstep.
In the next lesson, you’ll look at the remaining steps required to deploy SAM applications.