-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CDK stack now includes optional lambda functions for monitoring serve…
…r status, and starting and stopping the server
- Loading branch information
DaveB93
authored and
DaveB93
committed
Mar 26, 2021
1 parent
c72395e
commit 72bee85
Showing
10 changed files
with
2,013 additions
and
547 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import * as core from "@aws-cdk/core"; | ||
import * as apigateway from "@aws-cdk/aws-apigateway"; | ||
import * as lambda from "@aws-cdk/aws-lambda"; | ||
import * as lambdanodejs from "@aws-cdk/aws-lambda-nodejs"; | ||
import { Policy, PolicyStatement, PolicyProps, Effect } from "@aws-cdk/aws-iam" | ||
import { Stack, Arn } from "@aws-cdk/core"; | ||
import { EndpointType } from "@aws-cdk/aws-apigateway"; | ||
|
||
export interface LambdaEcsFargateUpDownServiceOptions { | ||
region: string; | ||
serviceArn: Arn; | ||
clusterArn: Arn; | ||
startStopPassword: string; | ||
} | ||
|
||
export class LambdaEcsFargateUpDownService extends core.Construct { | ||
constructor(scope: core.Construct, id: string, props: LambdaEcsFargateUpDownServiceOptions) { | ||
super(scope, id); | ||
|
||
const serverStatusHandler = new lambdanodejs.NodejsFunction(this, "serverStatus", { | ||
runtime: lambda.Runtime.NODEJS_10_X, // So we can use async | ||
entry: 'resources/serverstatus.ts', | ||
handler: "handler", | ||
bundling: { | ||
nodeModules: ['@aws-sdk/client-ecs', '@aws-sdk/client-ec2'], | ||
}, | ||
environment: { | ||
REGION: props.region, | ||
SERVICE_ARN: props.serviceArn as string, | ||
CLUSTER_ARN: props.clusterArn as string | ||
} | ||
}); | ||
|
||
const ecsStatusPolicy = new Policy(this, "ecsStatusPolicy", { | ||
statements: [ | ||
new PolicyStatement({ | ||
resources: ['*'], | ||
effect: Effect.ALLOW, | ||
actions: [ | ||
"ecs:ListTasks", | ||
"ecs:DescribeTasks", | ||
"ec2:DescribeNetworkInterfaces" | ||
] | ||
}) | ||
] | ||
}); | ||
serverStatusHandler.role?.attachInlinePolicy(ecsStatusPolicy); | ||
|
||
const startStopHandler = new lambdanodejs.NodejsFunction(this, "startstop", { | ||
runtime: lambda.Runtime.NODEJS_10_X, // So we can use async | ||
entry: 'resources/startstopserver.ts', | ||
handler: "handler", | ||
bundling: { | ||
nodeModules: ['@aws-sdk/client-ecs'], | ||
}, | ||
environment: { | ||
REGION: props.region, | ||
SERVICE_NAME: props.serviceArn as string, | ||
CLUSTER_ARN: props.clusterArn as string, | ||
PASSWORD: props.startStopPassword, | ||
} | ||
}); | ||
const ecsStartStopPolicy = new Policy(this, "ecsStartStopPolicy", { | ||
statements: [ | ||
new PolicyStatement({ | ||
resources: ['*'], | ||
effect: Effect.ALLOW, | ||
actions: [ | ||
"ecs:UpdateService", | ||
] | ||
}) | ||
] | ||
}); | ||
startStopHandler.role?.attachInlinePolicy(ecsStartStopPolicy); | ||
|
||
|
||
const api = new apigateway.RestApi(this, "startstopserver-api", { | ||
restApiName: "Start Stop Status for ECS service", | ||
description: "This service allows you to start / stop and get the status of an ECS task.", | ||
endpointTypes: [ EndpointType.REGIONAL ] | ||
}); | ||
|
||
const startStopResource = api.root.addResource("startstop"); | ||
const serverStatusResource = api.root.addResource("serverstatus"); | ||
|
||
const serverStatusIntegration = new apigateway.LambdaIntegration(serverStatusHandler, { | ||
}); | ||
|
||
const startStopIntegration = new apigateway.LambdaIntegration(startStopHandler, { | ||
}); | ||
|
||
serverStatusResource.addMethod("ANY", serverStatusIntegration); // GET / | ||
startStopResource.addMethod("ANY", startStopIntegration); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import * as cdk from '@aws-cdk/core'; | ||
import { Arn, Stack } from '@aws-cdk/core'; | ||
import * as lambda_service from '../lib/lambda-ecs-fargate-updownstatus-service'; | ||
|
||
interface MultiStackProps extends cdk.StackProps { | ||
serviceArn: Arn; | ||
clusterArn: Arn; | ||
startStopPassword: string; | ||
} | ||
|
||
export class LambdaEcsFargateUpdownstatusStack extends cdk.Stack { | ||
constructor(scope: cdk.Construct, id: string, props: MultiStackProps) { | ||
super(scope, id, props); | ||
|
||
new lambda_service.LambdaEcsFargateUpDownService(this, 'Status', { | ||
region: cdk.Stack.of(this).region, | ||
serviceArn: props.serviceArn, | ||
clusterArn: props.clusterArn, | ||
startStopPassword: props.startStopPassword, | ||
}); | ||
} | ||
} |
This file contains 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
Oops, something went wrong.