Skip to content

Commit

Permalink
Merge pull request #302 from PRX/feat/analytics_dynamodb_lambda
Browse files Browse the repository at this point in the history
analytics-dynamodb-lambda
  • Loading branch information
farski authored Feb 25, 2019
2 parents b57019e + 6862daa commit a8fbed0
Show file tree
Hide file tree
Showing 4 changed files with 248 additions and 0 deletions.
1 change: 1 addition & 0 deletions stacks/root.yml
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ Resources:
DovetailCountsLambdaCodeS3ObjectVersion: !Ref DovetailCountsLambdaCodeS3ObjectVersion
CmsAudioLambdaCodeS3ObjectVersion: !Ref CmsAudioLambdaCodeS3ObjectVersion
CmsImageLambdaCodeS3ObjectVersion: !Ref CmsImageLambdaCodeS3ObjectVersion
AnalyticsIngestLambdaCodeS3ObjectVersion: !Ref AnalyticsIngestLambdaCodeS3ObjectVersion
Tags:
- Key: "prx:cloudformation:stack-name"
Value: !Ref AWS::StackName
Expand Down
89 changes: 89 additions & 0 deletions stacks/serverless/analytics-ingest-alarms.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# stacks/serverless/analytics-ingest-alarms.yml
AWSTemplateFormatVersion: "2010-09-09"
Description: The many, many alarms on analytics-ingest-lambda functions
Conditions:
CreateProductionResources: !Equals [!Ref EnvironmentType, Production]
Parameters:
OpsWarnMessagesSnsTopicArn:
Type: String
OpsErrorMessagesSnsTopicArn:
Type: String
OpsFatalMessagesSnsTopicArn:
Type: String
EnvironmentType:
Type: String
DynamodbFunctionArn:
Type: String
Resources:
AnalyticsDynamodbErrorAlarm:
Type: "AWS::CloudWatch::Alarm"
Properties:
ActionsEnabled: true
AlarmName: !Sub "[AnalyticsLambda][DynamoDB][Errors] ${EnvironmentType} > 1"
AlarmActions:
- !If [CreateProductionResources, !Ref OpsErrorMessagesSnsTopicArn, !Ref OpsWarnMessagesSnsTopicArn]
InsufficientDataActions:
- !If [CreateProductionResources, !Ref OpsErrorMessagesSnsTopicArn, !Ref OpsWarnMessagesSnsTopicArn]
OKActions:
- !If [CreateProductionResources, !Ref OpsErrorMessagesSnsTopicArn, !Ref OpsWarnMessagesSnsTopicArn]
AlarmDescription:
Too many analytics dynamodb lambda errors
ComparisonOperator: GreaterThanThreshold
EvaluationPeriods: 1
MetricName: Errors
Namespace: AWS/Lambda
Period: 300
Statistic: Sum
Threshold: 1
TreatMissingData: notBreaching
Dimensions:
- Name: FunctionName
Value: !Ref DynamodbFunctionArn
AnalyticsDynamodbIteratorAgeWarnAlarm:
Type: "AWS::CloudWatch::Alarm"
Properties:
ActionsEnabled: true
AlarmName: !Sub "[AnalyticsLambda][DynamoDB][IteratorAge] ${EnvironmentType} > 15 minutes"
AlarmActions:
- !Ref OpsWarnMessagesSnsTopicArn
InsufficientDataActions:
- !Ref OpsWarnMessagesSnsTopicArn
OKActions:
- !Ref OpsWarnMessagesSnsTopicArn
AlarmDescription:
Processing is slightly behind
ComparisonOperator: GreaterThanThreshold
EvaluationPeriods: 1
MetricName: IteratorAge
Namespace: AWS/Lambda
Period: 15
Statistic: Maximum
Threshold: 900000
TreatMissingData: notBreaching
Dimensions:
- Name: FunctionName
Value: !Ref DynamodbFunctionArn
AnalyticsDynamodbIteratorAgeFatalAlarm:
Type: "AWS::CloudWatch::Alarm"
Properties:
ActionsEnabled: true
AlarmName: !Sub "[AnalyticsLambda][DynamoDB][IteratorAge] ${EnvironmentType} > 1 Hour"
AlarmActions:
- !If [CreateProductionResources, !Ref OpsFatalMessagesSnsTopicArn, !Ref OpsWarnMessagesSnsTopicArn]
InsufficientDataActions:
- !If [CreateProductionResources, !Ref OpsFatalMessagesSnsTopicArn, !Ref OpsWarnMessagesSnsTopicArn]
OKActions:
- !If [CreateProductionResources, !Ref OpsFatalMessagesSnsTopicArn, !Ref OpsWarnMessagesSnsTopicArn]
AlarmDescription:
Processing is very behind
ComparisonOperator: GreaterThanThreshold
EvaluationPeriods: 1
MetricName: IteratorAge
Namespace: AWS/Lambda
Period: 60
Statistic: Maximum
Threshold: 3600000
TreatMissingData: notBreaching
Dimensions:
- Name: FunctionName
Value: !Ref DynamodbFunctionArn
107 changes: 107 additions & 0 deletions stacks/serverless/analytics-ingest-lambdas.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# stacks/serverless/analytics-ingest-lambda.yml
# This stack creates 4 lambda functions subscribed to kinesis streams:
# 1) TODO: move analytics-bigquery
# 2) TODO: move analytics-pingbacks
# 3) TODO: move analytics-redis
# 4) analytics-dynamodb - IAB 2.0 compliant downloads
# a) store temporary 'antebytes' records from dovetail.prx.org in ddb
# b) process 'bytes'/'segmentbytes' records from dovetail-counts-lambda,
# lookup the records in ddb, change them to type 'postbytes' and place
# back onto kinesis to be process by functions 1/2/3.
AWSTemplateFormatVersion: "2010-09-09"
Description: Analytics ingest lambda functions
Parameters:
CodeS3Bucket:
Type: String
CodeS3ObjectVersion:
Type: String
EnvironmentType:
Type: String
MetricsKinesisStream:
Type: AWS::SSM::Parameter::Value<String>
DynamodbKinesisStream:
Type: AWS::SSM::Parameter::Value<String>
DynamodbTableName:
Type: AWS::SSM::Parameter::Value<String>
DynamodbAccessRole:
Type: AWS::SSM::Parameter::Value<String>
DynamodbTTL:
Type: AWS::SSM::Parameter::Value<String>
Resources:
# IAM Roles
AnalyticsDynamodbExecutionIAMRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service:
- "lambda.amazonaws.com"
Action:
- "sts:AssumeRole"
Path: "/"
Policies:
- PolicyName: KinesisWritePolicy
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- "kinesis:DescribeStream"
- "kinesis:PutRecord"
- "kinesis:PutRecords"
Resource:
- !Ref MetricsKinesisStream
- PolicyName: DynamodbAssumeRolePolicy
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action: "sts:AssumeRole"
Resource: !Ref DynamodbAccessRole
ManagedPolicyArns:
- "arn:aws:iam::aws:policy/service-role/AWSLambdaKinesisExecutionRole"
# Lambda Functions
AnalyticsDynamodbLambdaFunction:
Type: "AWS::Lambda::Function"
Properties:
Code:
S3Bucket: !Ref CodeS3Bucket
S3Key: lambda/PRX-analytics-ingest-lambda.zip
S3ObjectVersion: !Ref CodeS3ObjectVersion
Description: Dovetail analytics to dynamodb
Environment:
Variables:
DYNAMODB: "true" # set function mode = dynamodb
DDB_TABLE: !Ref DynamodbTableName
DDB_ROLE: !Ref DynamodbAccessRole
DDB_TTL: !Ref DynamodbTTL
KINESIS_STREAM: !Ref MetricsKinesisStream
Handler: index.handler
MemorySize: 256
Role: !GetAtt AnalyticsDynamodbExecutionIAMRole.Arn
Runtime: nodejs8.10
Tags:
- Key: Project
Value: Dovetail
- Key: Environment
Value: !Ref EnvironmentType
- Key: "prx:cloudformation:stack-name"
Value: !Ref AWS::StackName
- Key: "prx:cloudformation:stack-id"
Value: !Ref AWS::StackId
Timeout: 30
# Lambda Triggers
AnalyticsDynamodbKinesisTrigger:
Type: "AWS::Lambda::EventSourceMapping"
Properties:
BatchSize: 100
Enabled: true
EventSourceArn: !Ref DynamodbKinesisStream
FunctionName: !Ref AnalyticsDynamodbLambdaFunction
StartingPosition: "LATEST"
Outputs:
DynamodbFunctionArn:
Value: !GetAtt AnalyticsDynamodbLambdaFunction.Arn
51 changes: 51 additions & 0 deletions stacks/serverless/root.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ Parameters:
Type: String
CmsImageLambdaCodeS3ObjectVersion:
Type: String
AnalyticsIngestLambdaCodeS3ObjectVersion:
Type: String
Resources:
UploadLambdaStack:
Type: "AWS::CloudFormation::Stack"
Expand Down Expand Up @@ -201,3 +203,52 @@ Resources:
Value: !Ref AWS::StackId
TemplateURL: !Join ["", [!Ref TemplateUrlPrefix, "cms-image-lambda.yml"]]
TimeoutInMinutes: 5
AnalyticsIngestLambdasStack:
Type: "AWS::CloudFormation::Stack"
Properties:
NotificationARNs:
- Fn::ImportValue:
!Sub "${InfrastructureNotificationsStackName}-CloudFormationNotificationSnsTopic"
Parameters:
CodeS3Bucket:
Fn::ImportValue:
!Sub "${InfrastructureStorageStackName}-InfrastructureApplicationCodeBucket"
CodeS3ObjectVersion: !Ref AnalyticsIngestLambdaCodeS3ObjectVersion
EnvironmentType: !Ref EnvironmentType
MetricsKinesisStream: !Sub "/prx/${EnvironmentTypeAbbreviation}/analytics-ingest-lambda/METRICS_KINESIS_STREAM"
DynamodbKinesisStream: !Sub "/prx/${EnvironmentTypeAbbreviation}/analytics-ingest-lambda/DYNAMODB_KINESIS_STREAM"
DynamodbTableName: !Sub "/prx/${EnvironmentTypeAbbreviation}/analytics-ingest-lambda/DYNAMODB_TABLE_NAME"
DynamodbAccessRole: !Sub "/prx/${EnvironmentTypeAbbreviation}/analytics-ingest-lambda/DYNAMODB_ACCESS_ROLE"
DynamodbTTL: !Sub "/prx/${EnvironmentTypeAbbreviation}/analytics-ingest-lambda/DYNAMODB_TTL"
Tags:
- Key: "prx:cloudformation:stack-name"
Value: !Ref AWS::StackName
- Key: "prx:cloudformation:stack-id"
Value: !Ref AWS::StackId
TemplateURL: !Join ["", [!Ref TemplateUrlPrefix, "analytics-ingest-lambdas.yml"]]
TimeoutInMinutes: 5
AnalyticsIngestAlarmsStack:
Type: "AWS::CloudFormation::Stack"
Properties:
NotificationARNs:
- Fn::ImportValue:
!Sub "${InfrastructureNotificationsStackName}-CloudFormationNotificationSnsTopic"
Parameters:
OpsWarnMessagesSnsTopicArn:
Fn::ImportValue:
!Sub "${InfrastructureNotificationsStackName}-OpsWarnMessagesSnsTopicArn"
OpsErrorMessagesSnsTopicArn:
Fn::ImportValue:
!Sub "${InfrastructureNotificationsStackName}-OpsErrorMessagesSnsTopicArn"
OpsFatalMessagesSnsTopicArn:
Fn::ImportValue:
!Sub "${InfrastructureNotificationsStackName}-OpsFatalMessagesSnsTopicArn"
EnvironmentType: !Ref EnvironmentType
DynamodbFunctionArn: !GetAtt AnalyticsIngestLambdasStack.Outputs.DynamodbFunctionArn
Tags:
- Key: "prx:cloudformation:stack-name"
Value: !Ref AWS::StackName
- Key: "prx:cloudformation:stack-id"
Value: !Ref AWS::StackId
TemplateURL: !Join ["", [!Ref TemplateUrlPrefix, "analytics-ingest-alarms.yml"]]
TimeoutInMinutes: 5

0 comments on commit a8fbed0

Please sign in to comment.