Initializing and Deploying an AWS SAM Application

Learn how to initialize an AWS SAM application with the SAM CLI and deploy it to AWS.

Initializing a SAM application

With the SAM CLI installed, we can create a SAM application as follows:

sam init --name api-app

Here, we are invoking the SAM CLI with the command init, which is used to initialize a SAM application, and we are also using the --name option to name the application "api-app". This will start the initialization routine, which will ask a number of questions on the command line as follows:

Which template source would you like to use?
1 - AWS Quick Start Templates
2 - Custom Template Location
Choice:

Here, we will choose the first option, which is to use “AWS Quick Start Templates”, and respond by typing 1.

The next question is, “Choose an AWS Quick Start application template.”

Choose an AWS Quick Start application template
	1 - Hello World Example
	2 - Data processing
	3 - Hello World Example with Powertools for AWS Lambda
...

Here, we can again choose option one to use “Hello World Example” as our template.

The next question we are presented with is to choose the runtime and package type, as follows:

Use the most popular runtime and package type? (Python and zip) [Y/N]

Here, we will choose option N, which is to This will actually show the other 21 different options available for the runtime, which were not shown here initially, meaning that the list of available languages with which to write AWS Lambdas is quite extensive.

Which runtime would you like to use?
	1 - aot.dotnet7 (provided.al2)
	2 - dotnet6
	3 - go1.x
	4 - go (provided.al2)
	5 - graalvm.java11 (provided.al2)
	6 - graalvm.java17 (provided.al2)
	7 - java17
	8 - java11
	9 - java8.al2
	10 - java8
	11 - nodejs18.x
	12 - nodejs16.x
	13 - nodejs14.x
	14 - nodejs12.x
	15 - python3.9
...

Here, we will select option 13, which is nodejs14.x. The next question is which package type to use:

What package type would you like to use?
	1 - Zip
	2 - Image
Package type:

Here, we can choose option 1 to use Zip artifacts uploaded via an Amazon S3 bucket.

The final question we will need to answer is which quick start template to use for our application, as follows:

Select your starter template
	1 - Hello World Example
	2 - Hello World Example TypeScript
Template:

Here, we will choose option one, which will give us a classic "Hello World" example.

The SAM CLI will now create an application for us with the following output:

-----------------------
 Generating application:
 -----------------------
 Name: api-app
 Runtime: nodejs14.x
 Dependency Manager: npm
 Application Template: hello-world
 Output Directory: .

 Next steps can be found in the README file at ./api-app/README.md

Here, we can see that we have generated a SAM application named api-app that uses the nodejs14.x runtime, uses the npm tool as a dependency manager, and uses the hello-world-typescript application template.

Generated structure

Let’s now take a quick look at the generated application structure that the SAM CLI has generated for us, based on the "Hello World" example, which we selected when initializing the application.

The files and folders that are created for us are as follows:


└── api-app
 ├── samconfig.toml
 ├── template.yaml
 ├── README.md
 ├── hello-world
 │ ├── tests
 │ │ ├── unit
 │ │ │ └── test-handler.js
 │ ├── package-lock.json
 │ ├── package.json
 │ └── app.js
 └── events
 └── event.json

Here, we can see that the SAM CLI has created a directory named api-app, which corresponds to the name that we used when initializing the application. Within this directory, we have a file named template.yaml and a README.md file. The template.yaml file is used to describe all of the AWS elements that we will need within our application.

This includes the endpoint names that Amazon API Gateway will use, the names of the JavaScript files that will be used as Lambda handler functions, and the IAM access control settings. We can also include elements that describe DynamoDB tables and assign different database access rights to each of our Lambda functions.

The template.yaml file is used when we deploy a SAM application to AWS and is also used to configure endpoints and Lambda handlers when we run a local copy of our API.

If we take a sneak peek at a section of the template.yaml file, we will find that it references files within the generated directory structure:

Get hands-on with 1200+ tech skills courses.