Skip to content

Instantly share code, notes, and snippets.

@logemann
Last active March 5, 2021 13:49
Show Gist options
  • Save logemann/ee36639b6cfc6bcaadd36533e545d69c to your computer and use it in GitHub Desktop.
Save logemann/ee36639b6cfc6bcaadd36533e545d69c to your computer and use it in GitHub Desktop.
LambdaStack as typescript code for CDK project
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.createCNameChangeFunction();
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 createCNameChangeFunction(): 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;
}
}
@jamesenki
Copy link

Unable to find ExtendedStackProps! Any guidance would be appreciated

@logemann
Copy link
Author

logemann commented Mar 5, 2021

you can just replace it with core.StackProps from import * as core from '@aws-cdk/core';

@jamesenki
Copy link

jamesenki commented Mar 5, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment