Skip to content

Instantly share code, notes, and snippets.

@logemann
Last active March 5, 2021 13:49
Show Gist options
  • Select an option

  • Save logemann/ee36639b6cfc6bcaadd36533e545d69c to your computer and use it in GitHub Desktop.

Select an option

Save logemann/ee36639b6cfc6bcaadd36533e545d69c to your computer and use it in GitHub Desktop.

Revisions

  1. Marc Logemann revised this gist Jun 7, 2020. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions lambdaStack.ts
    Original 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.createVpnUrlFunction();
    let vpnUrlFunction = this.createCNameChangeFunction();

    this.createCNameChangeCron(route53Stack, vpnUrlFunction);

    @@ -68,7 +68,7 @@ export class LambdaStack extends Stack {
    return func;
    }

    private createVpnUrlFunction(): lambda.IFunction {
    private createCNameChangeFunction(): lambda.IFunction {
    var func = new lambda.Function(this, 'changeCName', {
    code: lambda.Code.fromAsset(path.join("content", "lambda", "cron")),
    handler: 'changeCName.handler',
  2. Marc Logemann created this gist Jun 7, 2020.
    87 changes: 87 additions & 0 deletions lambdaStack.ts
    Original 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;
    }
    }