Last active
March 5, 2021 13:49
-
-
Save logemann/ee36639b6cfc6bcaadd36533e545d69c to your computer and use it in GitHub Desktop.
Revisions
-
Marc Logemann revised this gist
Jun 7, 2020 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -15,7 +15,7 @@ export class LambdaStack extends Stack { super(scope, id, props); let startStopFunc = this.createStartStopVpnFunction(); let vpnUrlFunction = this.createCNameChangeFunction(); this.createCNameChangeCron(route53Stack, vpnUrlFunction); @@ -68,7 +68,7 @@ export class LambdaStack extends Stack { return func; } private createCNameChangeFunction(): lambda.IFunction { var func = new lambda.Function(this, 'changeCName', { code: lambda.Code.fromAsset(path.join("content", "lambda", "cron")), handler: 'changeCName.handler', -
Marc Logemann created this gist
Jun 7, 2020 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,87 @@ import { Construct, Stack } from '@aws-cdk/core'; import { ExtendedStackProps } from '../model/ExtendedStackProps'; import * as lambda from '@aws-cdk/aws-lambda'; import * as iam from '@aws-cdk/aws-iam'; import * as path from 'path'; import * as eventTargets from '@aws-cdk/aws-events-targets'; import { Route53Stack } from './route53Stack'; import { Rule, Schedule, RuleTargetInput } from '@aws-cdk/aws-events'; import * as logs from '@aws-cdk/aws-logs'; export class LambdaStack extends Stack { constructor(scope: Construct, route53Stack: Route53Stack, id: string, props: ExtendedStackProps) { super(scope, id, props); let startStopFunc = this.createStartStopVpnFunction(); let vpnUrlFunction = this.createVpnUrlFunction(); this.createCNameChangeCron(route53Stack, vpnUrlFunction); this.createShutdownCronLambda(startStopFunc); this.createStartupCronLambda(startStopFunc); } private createShutdownCronLambda(func: lambda.IFunction) { new Rule(this, 'ShutdownVpnRule', { schedule: Schedule.cron({ minute: '00', hour: '18', weekDay: "MON-FRI" }), targets: [new eventTargets.LambdaFunction(func, { event: RuleTargetInput.fromText("shutdown") })], }); } private createStartupCronLambda(func: lambda.IFunction) { new Rule(this, 'StartupVpnRule', { schedule: Schedule.cron({ minute: '00', hour: '06', weekDay: "MON-FRI" }), targets: [new eventTargets.LambdaFunction(func, { event: RuleTargetInput.fromText("startup") })], }); } private createCNameChangeCron(route53Stack : Route53Stack, func: lambda.IFunction) { new Rule(this, 'cnameChangeRule', { schedule: Schedule.cron({ minute: '10/10', hour: '06', weekDay: "MON-FRI" }), targets: [new eventTargets.LambdaFunction(func, { event: RuleTargetInput.fromObject({ dnsZoneId: route53Stack.swypComZone.hostedZoneId, cname: "*.vpn." + route53Stack.swypComZone.zoneName, }) })], }); } private createStartStopVpnFunction(): lambda.IFunction { // lets create our startupVpn / shutdownVpn Function let func = new lambda.Function(this, 'startStopVpn', { code: lambda.Code.fromAsset(path.join("content", "lambda", "cron")), handler: 'startStopVpn.handler', runtime: lambda.Runtime.NODEJS_12_X, logRetention: logs.RetentionDays.ONE_WEEK, description: "Startup the VpnEndpoint", retryAttempts: 1 }); func.addToRolePolicy(new iam.PolicyStatement({ effect: iam.Effect.ALLOW, resources: ["*"], actions: ['ec2:*', "cloudformation:*", "cloudwatch:*", "logs:*"], })); return func; } private createVpnUrlFunction(): lambda.IFunction { var func = new lambda.Function(this, 'changeCName', { code: lambda.Code.fromAsset(path.join("content", "lambda", "cron")), handler: 'changeCName.handler', runtime: lambda.Runtime.NODEJS_12_X, logRetention: logs.RetentionDays.ONE_WEEK, retryAttempts: 1 }); func.addToRolePolicy(new iam.PolicyStatement({ effect: iam.Effect.ALLOW, resources: ["*"], actions: ['ec2:*', "route53:*"], })); return func; } }