Skip to content

Commit

Permalink
feat(cdk-docker-cluster): container environment and secrets
Browse files Browse the repository at this point in the history
  • Loading branch information
thijsdaniels committed Jun 30, 2024
1 parent 8206b36 commit 4bbe16d
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 113 deletions.
5 changes: 5 additions & 0 deletions .changeset/lemon-ladybugs-unite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@codedazur/cdk-docker-cluster": minor
---

Container environment and secrets are now supported.
5 changes: 5 additions & 0 deletions .changeset/smart-lies-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@codedazur/cdk-next-app": patch
---

Props were updated to match DockerCluster.
105 changes: 26 additions & 79 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 15 additions & 7 deletions packages/cdk-docker-cluster/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ The minimum needed to get a DockerCluster up and running is a path to the source
```ts
new DockerCluster(this, "DockerCluster", {
source: "../path/to/source",
port: 3000,
service: {
port: 3000,
},
});
```

Expand Down Expand Up @@ -94,9 +96,12 @@ The `DockerCluster` construct supports both vertical and horizontal scaling.
```ts
new DockerCluster(this, "DockerCluster", {
// ...
cpu: 1024, // 1vCPU
memory: 4096, // 4GB
tasks: 3,
service: {
// ...
cpu: 1024, // 1vCPU
memory: 4096, // 4GB
tasks: 3,
},
});
```

Expand All @@ -105,9 +110,12 @@ Horizontal auto-scaling is also supported.
```ts
new DockerCluster(this, "DockerCluster", {
// ...
tasks: {
minimum: 1,
maximum: 5,
service: {
// ...
tasks: {
minimum: 1,
maximum: 5,
},
},
});
```
49 changes: 30 additions & 19 deletions packages/cdk-docker-cluster/src/constructs/DockerCluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import { App } from "aws-cdk-lib";
import { OriginProtocolPolicy } from "aws-cdk-lib/aws-cloudfront";
import { LoadBalancerV2Origin } from "aws-cdk-lib/aws-cloudfront-origins";
import { Platform } from "aws-cdk-lib/aws-ecr-assets";
import { AssetImageProps, Cluster, ContainerImage } from "aws-cdk-lib/aws-ecs";
import {
AssetImageProps,
Cluster,
ContainerImage,
Secret,
} from "aws-cdk-lib/aws-ecs";
import {
ApplicationLoadBalancedFargateService,
ApplicationLoadBalancedFargateServiceProps,
Expand All @@ -15,15 +20,19 @@ import { Construct } from "constructs";

export interface DockerClusterProps {
source: string | SourceProps | ContainerImage;
port?: number;
tasks?:
| number
| {
minimum: number;
maximum: number;
};
cpu?: ApplicationLoadBalancedFargateServiceProps["cpu"];
memory?: ApplicationLoadBalancedFargateServiceProps["memoryLimitMiB"];
service?: {
port?: number;
tasks?:
| number
| {
minimum: number;
maximum: number;
};
cpu?: ApplicationLoadBalancedFargateServiceProps["cpu"];
memory?: ApplicationLoadBalancedFargateServiceProps["memoryLimitMiB"];
environment?: Record<string, string>;
secrets?: Record<string, Secret>;
};
distribution?: Omit<SiteDistributionProps, "origin">;
}

Expand Down Expand Up @@ -81,18 +90,20 @@ export class DockerCluster extends Construct {

protected createService() {
const desiredTasks =
typeof this.props.tasks === "number"
? this.props.tasks
: this.props.tasks?.minimum;
typeof this.props.service?.tasks === "number"
? this.props.service?.tasks
: this.props.service?.tasks?.minimum;

const service = new ApplicationLoadBalancedFargateService(this, "Service", {
cluster: new Cluster(this, "Cluster"),
cpu: this.props.cpu,
memoryLimitMiB: this.props.memory,
cpu: this.props.service?.cpu,
memoryLimitMiB: this.props.service?.memory,
desiredCount: desiredTasks,
taskImageOptions: {
image: this.image,
containerPort: this.props.port,
containerPort: this.props.service?.port,
environment: this.props.service?.environment,
secrets: this.props.service?.secrets,
},
circuitBreaker: {
enable: true,
Expand All @@ -101,10 +112,10 @@ export class DockerCluster extends Construct {
publicLoadBalancer: false,
});

if (typeof this.props.tasks === "object") {
if (typeof this.props.service?.tasks === "object") {
this.service.service.autoScaleTaskCount({
minCapacity: this.props.tasks.minimum,
maxCapacity: this.props.tasks.maximum,
minCapacity: this.props.service?.tasks.minimum,
maxCapacity: this.props.service?.tasks.maximum,
});
}

Expand Down
17 changes: 11 additions & 6 deletions packages/cdk-next-app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,23 @@ The NextApp is based on the [DockerCluster](https://github.com/codedazur/toolkit
```ts
new NextApp(this, "NextApp", {
// ...
cpu: 1024,
memory: 4096,
tasks: 3,
service: {
cpu: 1024,
memory: 4096,
tasks: 3,
},
});
```

```ts
new NextApp(this, "NextApp", {
// ...
tasks: {
minimum: 1,
maximum: 5,
service: {
// ...
tasks: {
minimum: 1,
maximum: 5,
},
},
});
```
Loading

0 comments on commit 4bbe16d

Please sign in to comment.