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...- ← Previous post: DynamoDB: key technical concepts & features
- → Next post: Tracking AWS Lambda errors with Sentry (and CloudFormation)
This blog is written by Marcel Krcah, an independent consultant for product-oriented software engineering. If you like what you read, sign up for my newsletter