Is there a global properties for cloudformation Resources section?

9.7k Views Asked by At

I have a cloudformation template for my lambda:

Resources:
  Resource1:
    Type: AWS::Res
    Properties:
      StreamArn: 
         "Fn::Sub": "${var1}-${var2}"
  Resource2:
    Type: AWS::Res
    Properties:
      StreamArn: 
         "Fn::Sub": "${var1}-${var2}"

Is it possible to move these properties somewhere to Properties field of Resources section or any other place to avoid duplication?

Resources:
   Properties:
       StreamArn: 
           "Fn::Sub": "${var1}-${var2}"

I've tried to do it, but it doesn't work.

4

There are 4 best solutions below

0
John Rotenstein On

You can use a Parameters entry with a default value to create the equivalent to a Constant Variable, but it can't accept any values from the Resources section (since they haven't been created at that point).

Otherwise, no -- you'll need to duplicate the values. (As at the time of writing this answer.)

0
AudioBubble On

If you're using AWS::Serverless::Function, you can use Globals section to have common properties in a stack in one place. So, you can put the resources you mentioned in a stack for them and define a Globals section that has StreamArn

See docs

0
castlesnake On

If you are using SAM and the right resources then you can use the globals sections for this:

  • AWS::Serverless::Function
  • AWS::Serverless::Api, and
  • AWS::Serverless::SimpleTable

The Globals section is unique to AWS SAM. It defines properties that are common to all your serverless functions and APIs. All the AWS::Serverless::Function AWS::Serverless::Api, and AWS::Serverless::SimpleTable resources inherit the properties that are defined in the Globals section. For more information about the Globals section, see Globals Section of the Template in the AWS Serverless Application Model Developer Guide.

Documentation

0
Orest On

The most convenient way that I found so far is to use mapping like:

Mappings:
  ParametersMap:
    Var1:
      Value: "A"
    Var2:
      Value: "B"

and then put line !FindInMap: [ "ParametersMap", "Var1", "Value" ] in all the places were you need Var1 param