AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Description: Chat bot for reInvent trivia game Parameters: TriviaBackendEndpoint: Type: String Default: 'https://api.reinvent-trivia.com' Resources: BotFunction: Type: AWS::Serverless::Function Properties: CodeUri: ./bot Handler: bot.handler Runtime: nodejs14.x Timeout: 60 Environment: Variables: API_ENDPOINT: !Ref TriviaBackendEndpoint Events: Canary: Type: Schedule Properties: Schedule: rate(1 minute) Input: >- { "bot": { "name": "TriviaGame", "alias": "$LATEST", "version": "$LATEST" }, "currentIntent": { "name": "LetsPlay", "slots": { } } } AutoPublishAlias: live DeploymentPreference: Enabled: true Type: Canary10Percent5Minutes Alarms: - !Ref BotLatestVersionErrorMetricGreaterThanZeroAlarm - !Ref BotAliasErrorMetricGreaterThanZeroAlarm Hooks: PreTraffic: !Ref PreTrafficHook BotAliasErrorMetricGreaterThanZeroAlarm: Type: "AWS::CloudWatch::Alarm" Properties: AlarmName: !Sub ${AWS::StackName}-BotAliasErrors AlarmDescription: Lambda Function Error > 0 ComparisonOperator: GreaterThanThreshold Dimensions: - Name: Resource Value: !Sub "${BotFunction}:live" - Name: FunctionName Value: !Ref BotFunction EvaluationPeriods: 2 MetricName: Errors Namespace: AWS/Lambda Period: 60 Statistic: Sum Threshold: 0 BotLatestVersionErrorMetricGreaterThanZeroAlarm: Type: "AWS::CloudWatch::Alarm" Properties: AlarmName: !Sub ${AWS::StackName}-BotLatestVersionErrors AlarmDescription: Lambda Function Error > 0 ComparisonOperator: GreaterThanThreshold Dimensions: - Name: Resource Value: !Sub "${BotFunction}:live" - Name: FunctionName Value: !Ref BotFunction - Name: ExecutedVersion Value: !GetAtt BotFunction.Version.Version EvaluationPeriods: 2 MetricName: Errors Namespace: AWS/Lambda Period: 60 Statistic: Sum Threshold: 0 PreTrafficHook: Type: AWS::Serverless::Function Properties: FunctionName: !Join - '-' - - 'CodeDeployHook_' - !Ref "AWS::StackName" - 'pre-traffic-hook' CodeUri: ./hook Timeout: 300 Handler: pre-traffic-hook.handler Policies: - Version: "2012-10-17" Statement: - Effect: "Allow" Action: - "codedeploy:PutLifecycleEventHookExecutionStatus" Resource: !Sub 'arn:aws:codedeploy:${AWS::Region}:${AWS::AccountId}:deploymentgroup:${ServerlessDeploymentApplication}/*' - Version: "2012-10-17" Statement: - Effect: "Allow" Action: - "lambda:InvokeFunction" Resource: !Sub "${BotFunction.Arn}:*" Runtime: nodejs14.x DeploymentPreference: Enabled: false Role: "" Environment: Variables: CurrentVersion: !Ref BotFunction.Version LexPermission: Type: AWS::Lambda::Permission Properties: FunctionName: !Ref BotFunction.Alias Action: "lambda:invokeFunction" Principal: lex.amazonaws.com SourceArn: !Join - ':' - - 'arn' - !Ref "AWS::Partition" - 'lex' - !Ref "AWS::Region" - !Ref "AWS::AccountId" - 'intent:LetsPlay:*'