module-2/2-kinesis-analytics-module-update.yaml [891:1085]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  MetricTable:
    Type: AWS::DynamoDB::Table
    Properties:
      AttributeDefinitions:
        - AttributeName: MetricType
          AttributeType: S
      KeySchema:
        - AttributeName: MetricType
          KeyType: HASH
      ProvisionedThroughput:
        ReadCapacityUnits: 1
        WriteCapacityUnits: 1
      TableName: !Sub '${AWS::StackName}-Metrics'

  MetricDetailsTable:
    Type: AWS::DynamoDB::Table
    Properties:
      AttributeDefinitions:
        - AttributeName: MetricType
          AttributeType: S
        - AttributeName: EventTimestamp
          AttributeType: N
      KeySchema:
        - AttributeName: MetricType
          KeyType: HASH
        - AttributeName: EventTimestamp
          KeyType: RANGE
      ProvisionedThroughput:
        ReadCapacityUnits: !FindInMap [DDB, Scaling, ReadCapacityMin]
        WriteCapacityUnits: !FindInMap [DDB, Scaling, ReadCapacityMin]
      TableName: !Sub '${AWS::StackName}-MetricDetails'
      TimeToLiveSpecification:
        AttributeName: ExpireTime
        Enabled: true
      StreamSpecification:
        StreamViewType: NEW_IMAGE

  DDBScalingRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - application-autoscaling.amazonaws.com
            Action:
              - sts:AssumeRole
      Path: "/"
      Policies:
        - PolicyName: "root"
          PolicyDocument:
            Version: "2012-10-17"
            Statement:
              - Effect: "Allow"
                Action:
                  - "cloudwatch:PutMetricAlarm"
                  - "cloudwatch:DescribeAlarms"
                  - "cloudwatch:GetMetricStatistics"
                  - "cloudwatch:SetAlarmState"
                  - "cloudwatch:DeleteAlarms"
                Resource: "*"
              - Effect: "Allow"
                Action:
                  - "dynamodb:DescribeTable"
                  - "dynamodb:UpdateTable"
                Resource:
                  - !GetAtt MetricDetailsTable.Arn

  WriteCapacityScalableTarget:
    Type: AWS::ApplicationAutoScaling::ScalableTarget
    Properties:
      MaxCapacity: !FindInMap [DDB, Scaling, WriteCapacityMax]
      MinCapacity: !FindInMap [DDB, Scaling, WriteCapacityMin]
      ResourceId: !Sub "table/${MetricDetailsTable}"
      RoleARN: !GetAtt DDBScalingRole.Arn
      ScalableDimension: dynamodb:table:WriteCapacityUnits
      ServiceNamespace: dynamodb

  WriteScalingPolicy:
    Type: AWS::ApplicationAutoScaling::ScalingPolicy
    Properties:
      PolicyName: WriteAutoScalingPolicy
      PolicyType: TargetTrackingScaling
      ScalingTargetId: !Ref WriteCapacityScalableTarget
      TargetTrackingScalingPolicyConfiguration:
        TargetValue: !FindInMap [DDB, Scaling, WriteTargetUtilization]
        ScaleInCooldown: 300
        ScaleOutCooldown: 60
        PredefinedMetricSpecification:
          PredefinedMetricType: DynamoDBWriteCapacityUtilization

  ReadCapacityScalableTarget:
    Type: "AWS::ApplicationAutoScaling::ScalableTarget"
    Properties:
      MaxCapacity: !FindInMap [DDB, Scaling, ReadCapacityMax]
      MinCapacity: !FindInMap [DDB, Scaling, ReadCapacityMin]
      ResourceId: !Sub "table/${MetricDetailsTable}"
      RoleARN: !GetAtt DDBScalingRole.Arn
      ScalableDimension: dynamodb:table:ReadCapacityUnits
      ServiceNamespace: dynamodb

  ReadScalingPolicy:
    Type: "AWS::ApplicationAutoScaling::ScalingPolicy"
    Properties:
      PolicyName: ReadAutoScalingPolicy
      PolicyType: TargetTrackingScaling
      ScalingTargetId: !Ref ReadCapacityScalableTarget
      TargetTrackingScalingPolicyConfiguration:
        TargetValue: !FindInMap [DDB, Scaling, ReadTargetUtilization]
        ScaleInCooldown: 300
        ScaleOutCooldown: 60
        PredefinedMetricSpecification:
          PredefinedMetricType: DynamoDBReadCapacityUtilization

  SeedDynamoTable:
    Type: Custom::LoadLambda
    Properties:
      ServiceToken: !GetAtt CustomResourceHelper.Arn
      Region: !Ref AWS::Region
      TableName: !Ref MetricTable
      CustomResourceAction: SeedDynamoTable
      UUID: !GetAtt GenerateUUID.UUID

# Lambda Processing Function
  ProcessMetricsLambdaRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - lambda.amazonaws.com
            Action:
              - sts:AssumeRole
      Policies:
      - PolicyName: ProcessMetricsLambdaPolicy
        PolicyDocument:
          Version: '2012-10-17'
          Statement:
          - Sid: DynamoDBAccess
            Effect: Allow
            Action:
            - dynamodb:BatchGetItem
            - dynamodb:BatchWriteItem
            - dynamodb:GetItem
            - dynamodb:GetRecords
            - dynamodb:PutItem
            - dynamodb:Query
            - dynamodb:Scan
            - dynamodb:UpdateItem
            Resource:
            - !GetAtt MetricTable.Arn
            - !GetAtt MetricDetailsTable.Arn
          - Sid: CWLogs
            Effect: Allow
            Action:
            - logs:CreateLogGroup
            - logs:CreateLogStream
            - logs:PutDestination
            - logs:PutLogEvents
            Resource:
            - !Join ["", ["arn:aws:logs:", Ref: "AWS::Region", ":", Ref: "AWS::AccountId", ":log-group:/aws/lambda/*"]]

  ProcessMetricsFunction:
    Type: AWS::Lambda::Function
    Properties:
      Code:
        S3Bucket: !Sub
          - ${Param1}-${AWS::Region}
          - Param1: !FindInMap
              - SourceCode
              - General
              - S3Bucket
        S3Key: !Sub
          - ${Param1}/wasa.zip
          - Param1: !FindInMap
              - SourceCode
              - General
              - KeyPrefix
      Environment:
        Variables:
          METRIC_TABLE: !Ref MetricTable
          METRIC_DETAILS_TABLE: !Ref MetricDetailsTable
          PULL_FROM_STREAMS: 'N'

      Description: Function for processing records coming from Kinesis Data Analytics and storing in DynamoDB
      Handler: index.handler
      MemorySize: 1600
      Role: !GetAtt ProcessMetricsLambdaRole.Arn
      Runtime: nodejs12.x
      Timeout: 300
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



module-3/3-publish-metrics-update.yaml [910:1105]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  MetricTable:
    Type: AWS::DynamoDB::Table
    Properties:
      AttributeDefinitions:
        - AttributeName: MetricType
          AttributeType: S
      KeySchema:
        - AttributeName: MetricType
          KeyType: HASH
      ProvisionedThroughput:
        ReadCapacityUnits: 1
        WriteCapacityUnits: 1
      TableName: !Sub '${AWS::StackName}-Metrics'

  MetricDetailsTable:
    Type: AWS::DynamoDB::Table
    Properties:
      AttributeDefinitions:
        - AttributeName: MetricType
          AttributeType: S
        - AttributeName: EventTimestamp
          AttributeType: N
      KeySchema:
        - AttributeName: MetricType
          KeyType: HASH
        - AttributeName: EventTimestamp
          KeyType: RANGE
      ProvisionedThroughput:
        ReadCapacityUnits: !FindInMap [DDB, Scaling, ReadCapacityMin]
        WriteCapacityUnits: !FindInMap [DDB, Scaling, ReadCapacityMin]
      TableName: !Sub '${AWS::StackName}-MetricDetails'
      TimeToLiveSpecification:
        AttributeName: ExpireTime
        Enabled: true
      StreamSpecification:
        StreamViewType: NEW_IMAGE

  DDBScalingRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - application-autoscaling.amazonaws.com
            Action:
              - sts:AssumeRole
      Path: "/"
      Policies:
        - PolicyName: "root"
          PolicyDocument:
            Version: "2012-10-17"
            Statement:
              - Effect: "Allow"
                Action:
                  - "cloudwatch:PutMetricAlarm"
                  - "cloudwatch:DescribeAlarms"
                  - "cloudwatch:GetMetricStatistics"
                  - "cloudwatch:SetAlarmState"
                  - "cloudwatch:DeleteAlarms"
                Resource: "*"
              - Effect: "Allow"
                Action:
                  - "dynamodb:DescribeTable"
                  - "dynamodb:UpdateTable"
                Resource:
                  - !GetAtt MetricDetailsTable.Arn

  WriteCapacityScalableTarget:
    Type: AWS::ApplicationAutoScaling::ScalableTarget
    Properties:
      MaxCapacity: !FindInMap [DDB, Scaling, WriteCapacityMax]
      MinCapacity: !FindInMap [DDB, Scaling, WriteCapacityMin]
      ResourceId: !Sub "table/${MetricDetailsTable}"
      RoleARN: !GetAtt DDBScalingRole.Arn
      ScalableDimension: dynamodb:table:WriteCapacityUnits
      ServiceNamespace: dynamodb

  WriteScalingPolicy:
    Type: AWS::ApplicationAutoScaling::ScalingPolicy
    Properties:
      PolicyName: WriteAutoScalingPolicy
      PolicyType: TargetTrackingScaling
      ScalingTargetId: !Ref WriteCapacityScalableTarget
      TargetTrackingScalingPolicyConfiguration:
        TargetValue: !FindInMap [DDB, Scaling, WriteTargetUtilization]
        ScaleInCooldown: 300
        ScaleOutCooldown: 60
        PredefinedMetricSpecification:
          PredefinedMetricType: DynamoDBWriteCapacityUtilization

  ReadCapacityScalableTarget:
    Type: "AWS::ApplicationAutoScaling::ScalableTarget"
    Properties:
      MaxCapacity: !FindInMap [DDB, Scaling, ReadCapacityMax]
      MinCapacity: !FindInMap [DDB, Scaling, ReadCapacityMin]
      ResourceId: !Sub "table/${MetricDetailsTable}"
      RoleARN: !GetAtt DDBScalingRole.Arn
      ScalableDimension: dynamodb:table:ReadCapacityUnits
      ServiceNamespace: dynamodb

  ReadScalingPolicy:
    Type: "AWS::ApplicationAutoScaling::ScalingPolicy"
    Properties:
      PolicyName: ReadAutoScalingPolicy
      PolicyType: TargetTrackingScaling
      ScalingTargetId: !Ref ReadCapacityScalableTarget
      TargetTrackingScalingPolicyConfiguration:
        TargetValue: !FindInMap [DDB, Scaling, ReadTargetUtilization]
        ScaleInCooldown: 300
        ScaleOutCooldown: 60
        PredefinedMetricSpecification:
          PredefinedMetricType: DynamoDBReadCapacityUtilization

  SeedDynamoTable:
    Type: Custom::LoadLambda
    Properties:
      ServiceToken: !GetAtt CustomResourceHelper.Arn
      Region: !Ref AWS::Region
      TableName: !Ref MetricTable
      CustomResourceAction: SeedDynamoTable
      UUID: !GetAtt GenerateUUID.UUID

# Lambda Processing Function
  ProcessMetricsLambdaRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - lambda.amazonaws.com
            Action:
              - sts:AssumeRole
      Policies:
        - PolicyName: ProcessMetricsLambdaPolicy
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
              - Sid: DynamoDBAccess
                Effect: Allow
                Action:
                  - dynamodb:BatchGetItem
                  - dynamodb:BatchWriteItem
                  - dynamodb:GetItem
                  - dynamodb:GetRecords
                  - dynamodb:PutItem
                  - dynamodb:Query
                  - dynamodb:Scan
                  - dynamodb:UpdateItem
                Resource:
                  - !GetAtt MetricTable.Arn
                  - !GetAtt MetricDetailsTable.Arn
              - Sid: CWLogs
                Effect: Allow
                Action:
                  - logs:CreateLogGroup
                  - logs:CreateLogStream
                  - logs:PutDestination
                  - logs:PutLogEvents
                Resource:
                  - !Join ["", ["arn:aws:logs:", Ref: "AWS::Region", ":", Ref: "AWS::AccountId", ":log-group:/aws/lambda/*"]]


  ProcessMetricsFunction:
    Type: AWS::Lambda::Function
    Properties:
      Code:
        S3Bucket: !Sub
          - ${Param1}-${AWS::Region}
          - Param1: !FindInMap
              - SourceCode
              - General
              - S3Bucket
        S3Key: !Sub
          - ${Param1}/wasa.zip
          - Param1: !FindInMap
              - SourceCode
              - General
              - KeyPrefix
      Environment:
        Variables:
          METRIC_TABLE: !Ref MetricTable
          METRIC_DETAILS_TABLE: !Ref MetricDetailsTable
          PULL_FROM_STREAMS: 'N'

      Description: Function for processing records coming from Kinesis Data Analytics and storing in DynamoDB
      Handler: index.handler
      MemorySize: 1600
      Role: !GetAtt ProcessMetricsLambdaRole.Arn
      Runtime: nodejs12.x
      Timeout: 300
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



