Skip to content

Commit

Permalink
feat: add docker cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
thijsdaniels committed May 2, 2024
1 parent cfc7fc9 commit bcb3361
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/fair-rules-love.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@codedazur/cdk-docker-cluster": minor
---

Experimental release.
5 changes: 5 additions & 0 deletions packages/cdk-docker-cluster/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Source Code
src

# Turbo Artifacts
.turbo
9 changes: 9 additions & 0 deletions packages/cdk-docker-cluster/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright (c) 2023 code d'azur Interactive B.V.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16 changes: 16 additions & 0 deletions packages/cdk-docker-cluster/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# DockerCluster

This construct creates a load balanced Fargate service, for which it builds a Docker image from a local directory. A CloudFront distribution is connected to the load balancer.

```ts
new DockerCluster({
path: "../my/directory", // Must contain a Dockerfile.
port: 3000,
cpu: 256,
memory: 512,
instances: 1,
secrets: {
foo: DockerBuildSecret.fromSrc("./foo"),
},
});
```
43 changes: 43 additions & 0 deletions packages/cdk-docker-cluster/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "@codedazur/cdk-docker-cluster",
"version": "0.0.0",
"main": ".dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"exports": {
".": {
"require": "./dist/index.js",
"import": "./dist/index.mjs"
}
},
"publishConfig": {
"access": "public"
},
"license": "MIT",
"scripts": {
"develop": "tsup src/index.ts --format esm,cjs --dts --watch",
"build": "tsup src/index.ts --format esm,cjs --dts",
"audit": "npm audit --omit dev",
"lint": "TIMING=1 eslint \"**/*.ts*\"",
"types": "tsc --noEmit",
"test": "vitest run --passWithNoTests",
"prepublishOnly": "npm run build"
},
"dependencies": {
"@codedazur/cdk-cache-invalidator": "*"
},
"peerDependencies": {
"aws-cdk-lib": ">=2",
"constructs": ">=10"
},
"devDependencies": {
"@types/node": "^20.12.8",
"aws-cdk-lib": "^2.139.1",
"constructs": "^10.3.0",
"esbuild": "^0.20.2",
"eslint-config-custom": "*",
"eslint": "^8.30.0",
"tsconfig": "*",
"typescript": "^5.4.5"
}
}
65 changes: 65 additions & 0 deletions packages/cdk-docker-cluster/src/constructs/DockerCluster.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { CacheInvalidator } from "@codedazur/cdk-cache-invalidator";
import { Distribution, OriginProtocolPolicy } from "aws-cdk-lib/aws-cloudfront";
import { LoadBalancerV2Origin } from "aws-cdk-lib/aws-cloudfront-origins";
import { DockerImageAsset, Platform } from "aws-cdk-lib/aws-ecr-assets";
import { Cluster, ContainerImage } from "aws-cdk-lib/aws-ecs";
import {
ApplicationLoadBalancedFargateService,
ApplicationLoadBalancedFargateServiceProps,
} from "aws-cdk-lib/aws-ecs-patterns";
import { Construct } from "constructs";

export interface DockerClusterProps {
path: string;
secrets?: Record<string, string>;
port?: number;
tasks?: number;
cpu?: ApplicationLoadBalancedFargateServiceProps["cpu"];
memory?: ApplicationLoadBalancedFargateServiceProps["memoryLimitMiB"];
}

/**
* An Docker cluster on a load balanced Fargate service on EC2, using an image
* built from a Dockerfile in a directory and pushed to ECR.
*/
export class DockerCluster extends Construct {
constructor(scope: Construct, id: string, props: DockerClusterProps) {
super(scope, id);

const image = new DockerImageAsset(this, "Image", {
directory: props.path,
buildSecrets: props.secrets,
platform: Platform.LINUX_AMD64,
});

const service = new ApplicationLoadBalancedFargateService(this, "Service", {
cluster: new Cluster(this, "Cluster"),
cpu: props.cpu,
memoryLimitMiB: props.memory,
desiredCount: props.tasks,
taskImageOptions: {
// image: ContainerImage.fromEcrRepository(image.repository, image.imageTag),
image: ContainerImage.fromDockerImageAsset(image),
containerPort: props.port,
},
circuitBreaker: {
enable: true,
rollback: true,
},
// @todo certificate: ...
// @todo publicLoadBalancer: false,
});

const distribution = new Distribution(this, "Distribution", {
defaultBehavior: {
origin: new LoadBalancerV2Origin(service.loadBalancer, {
protocolPolicy: OriginProtocolPolicy.HTTP_ONLY, // @todo OriginProtocolPolicy.HTTPS_ONLY
}),
},
});

new CacheInvalidator(this, "CacheInvalidator", {
distribution,
});
}
}
1 change: 1 addition & 0 deletions packages/cdk-docker-cluster/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./constructs/DockerCluster";
5 changes: 5 additions & 0 deletions packages/cdk-docker-cluster/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": "@codedazur/tsconfig/cdk.json",
"include": ["."],
"exclude": ["dist", "build", "node_modules"]
}

0 comments on commit bcb3361

Please sign in to comment.