A developer maintains an AWS SAM template with twelve AWS::Serverless::Function resources.
Every function repeats the same Runtime and Timeout, and the same LOG_LEVEL environment variable. One function needs a longer Timeout than the others. The developer wants to declare the shared settings once and still allow that exception.
What should the developer do?
Choose one answer.
AA. Move the shared settings to a Globals section under the Function key, and set the longer Timeout on that one function.
BB. Move the shared settings to the Parameters section and reference them from each function with Ref.
CC. Move the shared settings to the Mappings section and read them in every function with Fn::FindInMap.
DD. Move the shared settings to the Outputs section so that each function can import them with Fn::ImportValue.
Show the answer and explanation
Answer: A
A. Right: Correct. Globals settings are inherited by every function of that type, and a value set on a resource replaces the global one for scalar properties such as Timeout. Environment variables are merged, so LOG_LEVEL stays shared. Learn more: SAM Globals section (opens in a new tab)
B. Wrong: Parameters hold values supplied at deploy time, but each function would still have to reference them, so the repetition stays. They are not a way to apply properties to many resources at once.
C. Wrong: Mappings are lookup tables, and every function would still need its own Fn::FindInMap lines. This reduces the values repeated but not the repeated declarations.
D. Wrong: Outputs publish values from a stack, usually for other stacks. They do not apply properties to resources in the same template.
Explanation
The SAM Globals section declares properties once for all resources of a type. A property set on a single resource overrides the global value for scalars like Timeout, and maps like environment variables are merged. Parameters, Mappings and Outputs still need a reference in every function.
Want more on AWS SAM, CloudFormation and CDK? The 5 full-length practice sets have 15 more AWS SAM, CloudFormation and CDK questions, 390 in all, none of them in the free exam. One payment of $14.99, no subscription. See the practice sets →