AWSTemplateFormatVersion: '2010-09-09' Transform: 'AWS::Serverless-2016-10-31' Description: >- Greeting Amazon API Gateway. Parameters: LambdaAlias: Type: String Default: prod CanaryDeploymentType: Type: String Default: Canary10Percent5Minutes AllowedValues: - Canary10Percent30Minutes - Canary10Percent5Minutes - Canary10Percent10Minutes - Canary10Percent15Minutes - AllAtOnce - Linear10PercentEvery10Minutes - Linear10PercentEvery1Minute - Linear10PercentEvery2Minutes - Linear10PercentEvery3Minutes Resources: GreetingFunction: Type: 'AWS::Serverless::Function' Properties: Handler: org.aws.samples.compute.greeting.GreetingHandler::handleRequest Runtime: java8 CodeUri: ../../../microservices-greeting/target/greeting.zip MemorySize: 1024 Timeout: 20 Tracing: Active AutoPublishAlias: !Ref LambdaAlias Policies: - AWSLambdaBasicExecutionRole - AWSXrayWriteOnlyAccess - Version: "2012-10-17" Statement: - Effect: "Allow" Action: - "ssm:PutParameter" Resource: !Sub "arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/GreetingApiEndpoint" Events: greet: Type: Api Properties: Path: /resources/greeting Method: GET RestApiId: !Ref GreetingApi DeploymentPreference: Type: !Ref CanaryDeploymentType Alarms: - LambdaAliasErrorMetricGreaterThanZeroAlarm - LambdaLatestVersionErrorMetricGreaterThanZeroAlarm GreetingApi: Type: AWS::Serverless::Api Properties: StageName: prod DefinitionBody: swagger: "2.0" paths: "/resources/greeting": get: responses: "200": content: text/plain: scheme: type: string x-amazon-apigateway-integration: httpMethod: POST type: aws_proxy uri: !Sub "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GreetingFunction.Arn}:${LambdaAlias}/invocations" Parameter: Type: "AWS::SSM::Parameter" Properties: Name: GreetingApiEndpoint Description: Greeting API Endpoint Type: String Value: !Sub '${GreetingApi}.execute-api.${AWS::Region}.amazonaws.com' PreTrafficLambdaFunction: Type: AWS::Serverless::Function Properties: CodeUri: ./hooks/ Handler: preTrafficHook.handler Runtime: nodejs10.x FunctionName: CodeDeployHook_gretting_preTrafficHook DeploymentPreference: Enabled: false Policies: - Version: "2012-10-17" Statement: - Effect: "Allow" Action: - "codedeploy:PutLifecycleEventHookExecutionStatus" Resource: "*" - Version: "2012-10-17" Statement: - Effect: "Allow" Action: - "lambda:InvokeFunction" Resource: "*" PostTrafficLambdaFunction: Type: AWS::Serverless::Function Properties: CodeUri: ./hooks/ Handler: preTrafficHook.handler Runtime: nodejs10.x FunctionName: CodeDeployHook_gretting_postTrafficHook DeploymentPreference: Enabled: false Policies: - Version: "2012-10-17" Statement: - Effect: "Allow" Action: - "codedeploy:PutLifecycleEventHookExecutionStatus" Resource: "*" - Version: "2012-10-17" Statement: - Effect: "Allow" Action: - "lambda:InvokeFunction" Resource: "*" LambdaAliasErrorMetricGreaterThanZeroAlarm: Type: "AWS::CloudWatch::Alarm" Properties: AlarmDescription: Lambda Function Error > 0 ComparisonOperator: GreaterThanThreshold Dimensions: - Name: Resource Value: !Sub "${GreetingFunction}:prod" - Name: FunctionName Value: !Ref GreetingFunction EvaluationPeriods: 2 MetricName: Errors Namespace: AWS/Lambda Period: 60 Statistic: Sum Threshold: 0 LambdaLatestVersionErrorMetricGreaterThanZeroAlarm: Type: "AWS::CloudWatch::Alarm" Properties: AlarmDescription: Lambda Function Error > 0 ComparisonOperator: GreaterThanThreshold Dimensions: - Name: Resource Value: !Ref GreetingFunction.Version - Name: FunctionName Value: !Ref GreetingFunction EvaluationPeriods: 2 MetricName: Errors Namespace: AWS/Lambda Period: 60 Statistic: Sum Threshold: 0 Outputs: greetingApiEndpoint: Description: Greeting API URL for application Value: !Sub 'https://${GreetingApi}.execute-api.${AWS::Region}.amazonaws.com/prod/resources/greeting'