Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Customizable task cpu and memory settings #207

Merged
merged 1 commit into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ new ContainerService(scope: Construct, id: string, props: ContainerServiceProps)
* **privateSubnets** (<code>[aws_ec2.SubnetSelection](#aws-cdk-lib-aws-ec2-subnetselection)</code>) VPC subnets for keycloak service. __*Optional*__
* **publicSubnets** (<code>[aws_ec2.SubnetSelection](#aws-cdk-lib-aws-ec2-subnetselection)</code>) VPC public subnets for ALB. __*Optional*__
* **stickinessCookieDuration** (<code>[Duration](#aws-cdk-lib-duration)</code>) The sticky session duration for the keycloak workload with ALB. __*Default*__: one day
* **taskCpu** (<code>number</code>) The number of cpu units used by the keycloak task. __*Default*__: 4096
* **taskMemory** (<code>number</code>) The amount (in MiB) of memory used by the keycloak task. __*Default*__: 8192



Expand Down Expand Up @@ -159,6 +161,8 @@ new KeyCloak(scope: Construct, id: string, props: KeyCloakProps)
* **publicSubnets** (<code>[aws_ec2.SubnetSelection](#aws-cdk-lib-aws-ec2-subnetselection)</code>) VPC public subnets for ALB. __*Default*__: VPC public subnets
* **singleDbInstance** (<code>boolean</code>) Whether to use single RDS instance rather than RDS cluster. __*Default*__: false
* **stickinessCookieDuration** (<code>[Duration](#aws-cdk-lib-duration)</code>) The sticky session duration for the keycloak workload with ALB. __*Default*__: one day
* **taskCpu** (<code>number</code>) The number of cpu units used by the keycloak task. __*Default*__: 4096
* **taskMemory** (<code>number</code>) The amount (in MiB) of memory used by the keycloak task. __*Default*__: 8192
* **vpc** (<code>[aws_ec2.IVpc](#aws-cdk-lib-aws-ec2-ivpc)</code>) VPC for the workload. __*Optional*__


Expand Down Expand Up @@ -226,6 +230,8 @@ addKeyCloakContainerService(props: ContainerServiceProps): ContainerService
* **privateSubnets** (<code>[aws_ec2.SubnetSelection](#aws-cdk-lib-aws-ec2-subnetselection)</code>) VPC subnets for keycloak service. __*Optional*__
* **publicSubnets** (<code>[aws_ec2.SubnetSelection](#aws-cdk-lib-aws-ec2-subnetselection)</code>) VPC public subnets for ALB. __*Optional*__
* **stickinessCookieDuration** (<code>[Duration](#aws-cdk-lib-duration)</code>) The sticky session duration for the keycloak workload with ALB. __*Default*__: one day
* **taskCpu** (<code>number</code>) The number of cpu units used by the keycloak task. __*Default*__: 4096
* **taskMemory** (<code>number</code>) The amount (in MiB) of memory used by the keycloak task. __*Default*__: 8192

__Returns__:
* <code>[ContainerService](#cdk-keycloak-containerservice)</code>
Expand Down Expand Up @@ -314,6 +320,8 @@ Name | Type | Description
**privateSubnets**? | <code>[aws_ec2.SubnetSelection](#aws-cdk-lib-aws-ec2-subnetselection)</code> | VPC subnets for keycloak service.<br/>__*Optional*__
**publicSubnets**? | <code>[aws_ec2.SubnetSelection](#aws-cdk-lib-aws-ec2-subnetselection)</code> | VPC public subnets for ALB.<br/>__*Optional*__
**stickinessCookieDuration**? | <code>[Duration](#aws-cdk-lib-duration)</code> | The sticky session duration for the keycloak workload with ALB.<br/>__*Default*__: one day
**taskCpu**? | <code>number</code> | The number of cpu units used by the keycloak task.<br/>__*Default*__: 4096
**taskMemory**? | <code>number</code> | The amount (in MiB) of memory used by the keycloak task.<br/>__*Default*__: 8192



Expand Down Expand Up @@ -389,6 +397,8 @@ Name | Type | Description
**publicSubnets**? | <code>[aws_ec2.SubnetSelection](#aws-cdk-lib-aws-ec2-subnetselection)</code> | VPC public subnets for ALB.<br/>__*Default*__: VPC public subnets
**singleDbInstance**? | <code>boolean</code> | Whether to use single RDS instance rather than RDS cluster.<br/>__*Default*__: false
**stickinessCookieDuration**? | <code>[Duration](#aws-cdk-lib-duration)</code> | The sticky session duration for the keycloak workload with ALB.<br/>__*Default*__: one day
**taskCpu**? | <code>number</code> | The number of cpu units used by the keycloak task.<br/>__*Default*__: 4096
**taskMemory**? | <code>number</code> | The amount (in MiB) of memory used by the keycloak task.<br/>__*Default*__: 8192
**vpc**? | <code>[aws_ec2.IVpc](#aws-cdk-lib-aws-ec2-ivpc)</code> | VPC for the workload.<br/>__*Optional*__


Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,21 @@ new KeyCloak(stack, 'KeyCloak', {

```


# Customize fargate task settings

Define `taskCpu` or `taskMemory` for overriding the defaults for the ecs service task.
Could be useful for development environments. For example:

```ts
new KeyCloak(stack, 'KeyCloak', {
nodeCount: 1,
taskCpu: 512,
taskMemory: 2048,
});

```

# Deploy in existing Vpc Subnets

You can deploy the workload in the existing Vpc and subnets. The `publicSubnets` are for the ALB, `privateSubnets` for the keycloak container tasks and `databaseSubnets` for the database.
Expand Down
41 changes: 37 additions & 4 deletions src/keycloak.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,29 @@ export interface KeyCloakProps {
*/
readonly databaseRemovalPolicy?: cdk.RemovalPolicy;


/**
* Overrides the default image
*
* @default quay.io/keycloak/keycloak:${KEYCLOAK_VERSION}
*/
readonly containerImage?: ecs.ContainerImage;

/**
* The number of cpu units used by the keycloak task.
*
* @default 4096
* @see FargateTaskDefinitionProps
*/
readonly taskCpu?: number;

/**
* The amount (in MiB) of memory used by the keycloak task.
*
* @default 8192
* @see FargateTaskDefinitionProps
*/
readonly taskMemory?: number;

}

export class KeyCloak extends Construct {
Expand Down Expand Up @@ -329,6 +345,8 @@ export class KeyCloak extends Construct {
internetFacing: props.internetFacing ?? true,
hostname: props.hostname,
containerImage: props.containerImage,
taskCpu: props.taskCpu,
taskMemory: props.taskMemory,
});

this.applicationLoadBalancer = keycloakContainerService.applicationLoadBalancer;
Expand Down Expand Up @@ -671,13 +689,28 @@ export interface ContainerServiceProps {
*/
readonly hostname?: string;


/**
* Overrides the default image
*
* @default quay.io/keycloak/keycloak:${KEYCLOAK_VERSION}
*/
readonly containerImage?: ecs.ContainerImage;

/**
* The number of cpu units used by the keycloak task.
*
* @default 4096
* @see FargateTaskDefinitionProps
*/
readonly taskCpu?: number;

/**
* The amount (in MiB) of memory used by the keycloak task.
*
* @default 8192
* @see FargateTaskDefinitionProps
*/
readonly taskMemory?: number;
}

export class ContainerService extends Construct {
Expand Down Expand Up @@ -764,8 +797,8 @@ export class ContainerService extends Construct {
),
});
const taskDefinition = new ecs.FargateTaskDefinition(this, 'TaskDef', {
cpu: 4096,
memoryLimitMiB: 8192,
cpu: props.taskCpu ?? 4096,
memoryLimitMiB: props.taskMemory ?? 8192,
executionRole,
});

Expand Down
28 changes: 28 additions & 0 deletions test/cluster-quarkus.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -529,3 +529,31 @@ test('with env', () => {
},
});
});


test('with customized task settings', () => {
// GIVEN
const app = new App();
const stack = new Stack(app, 'testing-stack');

// WHEN
new kc.KeyCloak(stack, 'KeyCloak', {
keycloakVersion: KeycloakVersion.V21_0_1,
certificateArn: 'MOCK_ARN',
hostname: 'keycloak.test',
taskCpu: 512,
taskMemory: 1024,
});

// THEN
const t = assertions.Template.fromStack(stack);
t.hasResourceProperties('AWS::ECS::TaskDefinition', {
ContainerDefinitions: [
{
Name: 'keycloak',
},
],
Cpu: '512',
Memory: '1024',
});
});
Loading