Search⌘ K
AI Features

Initialising the Application

Explore how to initialize a serverless application using AWS Serverless Application Model (SAM). Understand using the SAM CLI to create a new project with Node.js runtime, configure Lambda functions, and manage infrastructure with CloudFormation templates for effective serverless deployment.

This chapter introduces the basic workflow of serverless deployments. You’ll learn how to manage infrastructure with the AWS Serverless Application Model (SAM) and CloudFormation, and how to use API Gateway to send HTTP requests to Lambda functions.

The SAM command-line tool #

SAM command-line tools can generate sample projects and events so that you can get started easily.

The sam init command #

Let’s dive right in and create a sample web service backed by a Lambda function. You will use the code directory to execute the commands. To create a simple SAM project in a new subdirectory, run the following command:

Shell
sam init --runtime nodejs24.x --name app --app-template hello-world

You will be asked for the X-Ray, CloudWatch, JSON formating for logs permission. Enter n as a response, as we will not be using these services.

Note: SAM CLI’s interactive mode may present additional options for runtime selection, package type (Zip or Image), and other configurations.

You should get a quick confirmation about the initialisation of a new project:

Cloning from https://github.com/aws/aws-sam-cli-app-templates (process may take a moment)              

    -----------------------
    Generating application:
    -----------------------
    Name: app
    Runtime: nodejs24.x
    Architectures: x86_64
    Dependency Manager: npm
    Application Template: hello-world
    Output Directory: .
    Configuration file: app/samconfig.toml

If this command printed an error on your local machine, there is some issue with the setup. You should look at the chapter where we have the information on how to set up SAM command-line tools and other prerequisites.

You can try the above commands in the terminal provided below:

Terminal 1
Terminal
Loading...

The --runtime argument

The --runtime argument tells SAM which programming language you’ll use to write the Lambda function, or more precisely which execution environment it is intended to run in. The --runtime argument is important for generating the sample project. You will learn how to add more functions to the same project later, and you can even mix functions that are executed in different languages.

To speed upscaling and operations, Lambda has pre-packaged execution environments, called runtimes, for many popular languages. The nodejs24.x runtime tells Lambda to run your function under Node.js 24 (the current LTS version), an execution engine for JavaScript. AWS Lambda currently supports Node.js 18, 20, 22 and 24 runtimes SAM can also create example projects in other languages. For example, use java25 for a Java function. AWS Lambda also supports java11, java17 and java21 runtimes. Check out the AWS Lambda runtimes page for a complete list.

The --name argument

The --name argument tells SAM how to call the application, or more precisely the name for the subdirectory where the application files are stored. In the previous example case, app was used, so SAM will create a new subdirectory called app and copy the sample files there.

Here are the key ones:

  • hello-world is a directory containing the JavaScript (Node) source code for your Lambda function.

  • hello-world/package.json is the standard Node.js package manifest, describing dependencies required by the function.

  • hello-world/app.mjs is the actual service request handler.

  • template.yaml describes the operational infrastructure required by the function.

The --app-template argument

The --app-template argument tells SAM which template to apply when initialising the application. You can see the list of standard templates in the aws-sam-cli-app-templates project on GitHub. SAM can also use your templates, which might be useful for teams that often create similar Lambda functions. For example, you could create a template for message queue handling, and then quickly apply it when creating a new payment processor that connects to your payments queue. Companies can also use templates to standardise project directory layouts. To specify your own template location, use the --location parameter and point to a GIT repository or a local directory.

Now let’s proceed to the next lesson where you will learn about the CloudFormation Template and the CloudFormation Stack.