example_self_tested_lambda/cloudformation/lambda-sam-template.yaml

104 lines
2.7 KiB
YAML

---
AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
Description: An Lambda function with a monitoring alarm, DLQ & PreTraffic Test
Metadata:
License: magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3-or-Later
cfn-lint:
config:
ignore_checks:
- W3011 # UpdatePolicy generated by SAM
Parameters:
NotificationEmail:
Type: String
TestData:
Type: String
TestExpected:
Type: String
Resources:
Function:
Type: AWS::Serverless::Function
Properties:
Handler: lambda_function.handler
Runtime: python3.7
CodeUri: src/lambda_function.py
DeadLetterQueue:
TargetArn: !GetAtt DeadLetterQueue.Arn
Type: SQS
AutoPublishAlias: live
DeploymentPreference:
Type: CodeDeployDefault.LambdaAllAtOnce
Alarms:
- !Ref Alarm
Hooks:
PreTraffic: !Ref TestFunction
Alarm:
Type: AWS::CloudWatch::Alarm
Properties:
ActionsEnabled: true
AlarmActions:
- !Ref NotificationTopic
AlarmDescription: Notify on failures of cloud-interest-rate-calculator function
AlarmName: !Sub ${AWS::StackName}-errors
MetricName: Errors
Namespace: AWS/Lambda
Statistic: Sum
Dimensions:
- Name: FunctionName
Value: !Select [6, !Split [':', !GetAtt Function.Arn]]
- Name: Resource
Value: !Select [6, !Split [':', !GetAtt Function.Arn]]
Period: 300
EvaluationPeriods: 1
DatapointsToAlarm: 1
Threshold: 0
ComparisonOperator: GreaterThanThreshold
NotificationTopic:
Type: AWS::SNS::Topic
Properties:
DisplayName: !Sub ${AWS::StackName}-errors
Subscription:
- Endpoint: !Ref NotificationEmail
Protocol: email
Tags:
- Key: Type
Value: Monitoring
DeadLetterQueue:
Type: AWS::SQS::Queue
Properties:
Tags:
- Key: Type
Value: DeadLetterQueue
TestFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub CodeDeployHook_PreHook_${AWS::StackName}
Handler: lambda_verification.handler
Runtime: python3.7
CodeUri: src/lambda_verification.py
Environment:
Variables:
CurrentVersion: !Ref Function.Version
TestData: !Ref TestData
TestExpected: !Ref TestExpected
Policies:
- Version: 2012-10-17
Statement:
- Effect: Allow
Action: codedeploy:PutLifecycleEventHookExecutionStatus
Resource: "*"
- Effect: Allow
Action: lambda:InvokeFunction
Resource: !Sub "${Function.Arn}:*"
Outputs:
LambdaArn:
Value: !GetAtt Function.Arn