Last active
March 5, 2021 13:49
-
-
Save logemann/ee36639b6cfc6bcaadd36533e545d69c to your computer and use it in GitHub Desktop.
LambdaStack as typescript code for CDK project
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 characters
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; | |
} | |
} |
you can just replace it with core.StackProps from import * as core from '@aws-cdk/core';
Thank you!
…On Fri, Mar 5, 2021, 8:49 AM Marc Logemann ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
you can just replace it with core.StackProps from import * as core from
***@***.***/core';
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<https://gist.github.com/ee36639b6cfc6bcaadd36533e545d69c#gistcomment-3654814>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AHQF3V2GUE3FOMJI5Y537M3TCDON3ANCNFSM4YN7VXAA>
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Unable to find ExtendedStackProps! Any guidance would be appreciated