Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save filipeandre/55db664a0c48f911d76a7182a4208442 to your computer and use it in GitHub Desktop.
Save filipeandre/55db664a0c48f911d76a7182a4208442 to your computer and use it in GitHub Desktop.
Working Alarm template with cloudformation Fn::ForEach
AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::LanguageExtensions
Parameters:
AlarmLambdaPairs:
Type: CommaDelimitedList
Description: |
Comma-separated "prefix:lambdaName" pairs. Example: "PrefixA:MyLambdaA,PrefixB:MyLambdaB"
Default: PrefixA:MyLambdaA,PrefixB:MyLambdaB
Resources:
# This special resource logical ID "Fn::ForEach::CreateAlarms"
# triggers the loop expansion in Language Extensions.
Fn::ForEach::CreateAlarms:
# The array below must have exactly three items:
# 1) The iteration variable name (Pair),
# 2) The list to iterate over (AlarmLambdaPairs),
# 3) A map of resource definitions keyed by a name that references the iteration variable.
- Pair
- !Ref AlarmLambdaPairs
# The &{} syntax allows non-alphanumeric characters in the Collection to be used in OutputKey parameter
- MyAlarm&{Pair}:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmName: !Sub
- ${Value0}-${Value1}
- Value0: !Select [0, !Split [':', !Ref Pair]]
Value1: !Select [1, !Split [':', !Ref Pair]]
MetricName: Errors
Namespace: AWS/Lambda
Dimensions:
- Name: FunctionName
Value: !Select [1, !Split [':', !Ref Pair]]
Statistic: Sum
Period: 60
EvaluationPeriods: 1
Threshold: 1
ComparisonOperator: GreaterThanOrEqualToThreshold
AlarmDescription: !Sub
- 'Alarm for Lambda: ${LambdaName}'
- LambdaName: !Select [1, !Split [':', !Ref Pair]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment