-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #302 from PRX/feat/analytics_dynamodb_lambda
analytics-dynamodb-lambda
- Loading branch information
Showing
4 changed files
with
248 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters