Defining resources conditionally based on environment

Here's an example that defines input for a scheduled Lambda event based on environment, using Conditions and Condition Functions.

Two important things:

  • During a stack update, you can't update conditions by themselves. You can update conditions only when you include changes that add, modify, or delete resources.
  • Where to put conditions: see How to use conditions overview
AWSTemplateFormatVersion: '2010-09-09'  
Transform: AWS::Serverless-2016-10-31

Parameters:  
  Environment:  
    Type: String  
    AllowedValues: 
      - acc  
      - prod

Conditions: # <-- define conditions here 
  IsProd: !Equals  
    - !Ref Environment  
    - prod
	
Resources:  
  Main:  
    Type: AWS::Serverless::Function  
	Events:  
	  PeriodicTrigger:  
		Type: Schedule  
		Properties:  
		  Schedule: rate(5 minutes)  
		  Input: !If [ IsProd, '"foo"', '"bar"' ]  #  🎉
	# ... CodeUri, etc...	  

Would you like to connect? Subscribe via email or RSS , or follow me on Twitter!