From e5e488d50091bdc082743f118613699b24627e0c Mon Sep 17 00:00:00 2001 From: Francisco Morillo Date: Fri, 30 Jun 2023 09:39:39 +0200 Subject: [PATCH 1/2] fixing git ignore --- ScheduledScaling/.gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/ScheduledScaling/.gitignore b/ScheduledScaling/.gitignore index d9798bb..8043422 100644 --- a/ScheduledScaling/.gitignore +++ b/ScheduledScaling/.gitignore @@ -23,7 +23,6 @@ dist/ downloads/ eggs/ .eggs/ -lib/ lib64/ parts/ sdist/ From 4e5185a05868e6583ea89db1730d03b544510a03 Mon Sep 17 00:00:00 2001 From: Francisco Morillo Date: Fri, 30 Jun 2023 10:10:51 +0200 Subject: [PATCH 2/2] fixing git ignore --- .../scheduled-scaling-cdk/.gitignore | 1 - .../lib/scheduled-scaling-cdk-stack.ts | 97 +++++++++++++++++++ 2 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 ScheduledScaling/scheduled-scaling-cdk/lib/scheduled-scaling-cdk-stack.ts diff --git a/ScheduledScaling/scheduled-scaling-cdk/.gitignore b/ScheduledScaling/scheduled-scaling-cdk/.gitignore index d9798bb..8043422 100644 --- a/ScheduledScaling/scheduled-scaling-cdk/.gitignore +++ b/ScheduledScaling/scheduled-scaling-cdk/.gitignore @@ -23,7 +23,6 @@ dist/ downloads/ eggs/ .eggs/ -lib/ lib64/ parts/ sdist/ diff --git a/ScheduledScaling/scheduled-scaling-cdk/lib/scheduled-scaling-cdk-stack.ts b/ScheduledScaling/scheduled-scaling-cdk/lib/scheduled-scaling-cdk-stack.ts new file mode 100644 index 0000000..30aee0a --- /dev/null +++ b/ScheduledScaling/scheduled-scaling-cdk/lib/scheduled-scaling-cdk-stack.ts @@ -0,0 +1,97 @@ +import * as cdk from 'aws-cdk-lib'; +import { Construct } from 'constructs'; +import * as iam from 'aws-cdk-lib/aws-iam'; +import * as lambda from 'aws-cdk-lib/aws-lambda'; +import * as path from 'path'; +import {CfnParameter} from "aws-cdk-lib"; +import * as events from "aws-cdk-lib/aws-events"; +import * as targets from "aws-cdk-lib/aws-events-targets"; + + +export class ScheduledScalingCdkStack extends cdk.Stack { + constructor(scope: Construct, id: string, props?: cdk.StackProps) { + super(scope, id, props); + + const kdaAppName = new CfnParameter(this, "kdaAppName", { + type: "String", + description: "The name of the Kinesis Data Analytics Application that you want to scale based on schedule."}); + + const scaleUpHour = new CfnParameter(this, "scaleUpHour", { + type: "Number", + description: "Hour of the day when you want your KDA Application to Scale Up"}); + + const scaleDownHour = new CfnParameter(this, "scaleDownHour", { + type: "Number", + description: "Hour of the day when you want your KDA Application to Scale down"}); + + const lowKPU = new CfnParameter(this, "lowKPU", { + type: "Number", + description: "KPU you want for your KDA application during normal hours"}); + + const highKPU = new CfnParameter(this, "highKPU", { + type: "Number", + description: "KPU you want for your KDA application during peak hours"}); + + // our KDA app needs access to describe kinesisanalytics + const kdaAccessPolicy = new iam.PolicyDocument({ + statements: [ + new iam.PolicyStatement({ + resources: ['*'], + actions: ['kinesisanalytics:DescribeApplication','kinesisanalytics:UpdateApplication'] + }), + ], + }); + const lambdaRole = new iam.Role(this, 'lambda-scheduler-role', { + assumedBy: new iam.ServicePrincipal('lambda.amazonaws.com'), + description: 'Lambda Scheduler role', + inlinePolicies: { + KdaAccessPolicy: kdaAccessPolicy, + }, + }); + + const fn = new lambda.Function(this, 'kda-scheduled-scaler', { + runtime: lambda.Runtime.PYTHON_3_9, + handler: 'kda-scaler-lambda.handler', + code: lambda.Code.fromAsset('../kda-scaler-lambda/'), + role: lambdaRole, + environment: { + "REGION": this.region, + "KDA_APP_NAME": kdaAppName.valueAsString, + 'SCALE_UP_HOUR': scaleUpHour.valueAsString, + 'SCALE_DOWN_HOUR': scaleDownHour.valueAsString, + 'LOW_KPU': lowKPU.valueAsString, + 'HIGH_KPU': highKPU.valueAsString, + } + }); + + const ScalingUpRule=new events.Rule(this, 'ScalingUpRule', { + schedule: events.Schedule.cron({minute:"0", hour:scaleUpHour.valueAsString}) + }); + + + // add the Lambda function as a target for the Event Rule + ScalingUpRule.addTarget( + new targets.LambdaFunction(fn, { + event: events.RuleTargetInput.fromObject({ message: "Triggering Scheduled Scaling" }), + }) + ); + + // allow the Event Rule to invoke the Lambda function + targets.addLambdaPermission(ScalingUpRule, fn); + + const ScalingDownRule=new events.Rule(this, 'ScalingDownRule', { + schedule: events.Schedule.cron({minute:"0", hour:scaleDownHour.valueAsString}) + }); + + + // add the Lambda function as a target for the Event Rule + ScalingDownRule.addTarget( + new targets.LambdaFunction(fn, { + event: events.RuleTargetInput.fromObject({ message: "Triggering Scheduled Scaling" }), + }) + ); + + // allow the Event Rule to invoke the Lambda function + targets.addLambdaPermission(ScalingDownRule, fn); + } +}