Initialising the Application
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.
SAM command-line tools can generate sample projects and events so that you can get started easily.
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:
sam init --runtime nodejs12.x --name app --app-template hello-world
You will be asked for the following permission:
Allow SAM CLI to download AWS-provided quick start templates from Github [Y/n]:
y as a response. You should get a quick confirmation about the initialisation of a new project:
Cloning app templates from https://github.com/awslabs/aws-sam-cli-app-templates.git
Dependency Manager: npm
Application Template: hello-world
Output Directory: .
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.
--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
java8 for a Java function. Check out the AWS Lambda runtimes page for a complete list.
--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/package.jsonis the standard Node.js package manifest, describing dependencies required by the function.
hello-world/app.jsis the actual service request handler.
template.yamldescribes the operational infrastructure required by the function.
--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.