An independent guide to building modern software for serverless and native cloud

Defining Parameters & Outputs

This tutorial references the code in the aws-connectedcar-dotnet-serverless repository. If you're new to this course, see the introduction for information about setting up your workstation and getting the sample code.

CloudFormation templates can be structured to be reusable, somewhat like functions in a programming language. This is made possible by allowing you to define a set of parameters as inputs and a set of outputs that can be passed back, after execution, to calling scripts or templates. In this tutorial we’ll look in detail at how you can define these parameters and outputs in your CloudFormation templates.

Defining Template Parameters

To see some example parameters, let’s take a look at those found in the master.yaml template for the SAM version of the sample code, as shown below:

In this template, the BucketName example on lines 4-6 shows the simplest form for a parameter. In this case you see elements for a name, a type, and an optional description. You’re only going to see the string type in the sample code for this course, but there are other basic types you can use, such as Number, List<Number> and CommaDelimitedList, along with a range of AWS resource-specific types.

With the ServiceName parameter you also see an example of a default value, shown on line 10. With a default defined, specifying a value for this parameter in the create-stack or update-stack command is optional. With the EnvironmentName parameter, on lines 14-17, you see an example of a list of allowed values. Note that you also have the option to specify a regex-style value constraint.

Defining Template Outputs

Continuing to the bottom of the same master.yaml template, you can see an example set of outputs, as shown below:

Each output in this list has elements that define a name, an optional description, and a value. The UserPoolDomainName output, shown on lines 104-106, uses the !Ref function to return the value for the default attribute for the referenced element (we’ll cover the use of functions like this in the next tutorial). The UserPoolId output, shown on lines 107-111, uses the !GetAtt function to return a value for a specified resource (line 109) and named attribute for that resource (line 110).

Using Parameters & Outputs in Deployment Scripts

We’ll step through running the deployment scripts for the sample code in the labs, later in this section. But let’s take a quick look at how parameters and outputs are used in scripts. When creating or updating a CloudFormation stack, you specify values for the input parameters as shown below on lines 42-47 of the deploy.zsh script from the SAM deployment files:

Further down in the same script you can see a command that writes the outputs for the created or updated stack to the console, as shown on lines 88-91, in the code below:

In the labs we’ll also show you how to expand the “—query” argument shown on line 90 to filter for specific named stack outputs. For now, here’s what this command writes to the console when the script executes, shown below at the bottom right in the table with the "DescribeStacks" title: