@codedazur/cdk-docker-cluster 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ # @codedazur/cdk-docker-cluster
2
+
3
+ ## 0.2.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [`603c25a`](https://github.com/codedazur/toolkit/commit/603c25a4e1920c0ac50c83742c1384f7cca5653b) Thanks [@thijsdaniels](https://github.com/thijsdaniels)! - The DockerCluster now supports auto-scaling.
8
+
9
+ ## 0.1.0
10
+
11
+ ### Minor Changes
12
+
13
+ - [`bcb3361`](https://github.com/codedazur/toolkit/commit/bcb33613b40edcfb0097d961fa16511035c18b83) Thanks [@thijsdaniels](https://github.com/thijsdaniels)! - Experimental release.
package/LICENSE.md ADDED
@@ -0,0 +1,9 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 code d'azur Interactive B.V.
4
+
5
+ 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:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ 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.
package/README.md ADDED
@@ -0,0 +1,16 @@
1
+ # DockerCluster
2
+
3
+ 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.
4
+
5
+ ```ts
6
+ new DockerCluster({
7
+ path: "../my/directory", // Must contain a Dockerfile.
8
+ port: 3000,
9
+ cpu: 256,
10
+ memory: 512,
11
+ instances: 1,
12
+ secrets: {
13
+ foo: DockerBuildSecret.fromSrc("./foo"),
14
+ },
15
+ });
16
+ ```
@@ -0,0 +1,23 @@
1
+ import { ApplicationLoadBalancedFargateServiceProps } from 'aws-cdk-lib/aws-ecs-patterns';
2
+ import { Construct } from 'constructs';
3
+
4
+ interface DockerClusterProps {
5
+ path: string;
6
+ secrets?: Record<string, string>;
7
+ port?: number;
8
+ tasks?: number | {
9
+ minimum: number;
10
+ maximum: number;
11
+ };
12
+ cpu?: ApplicationLoadBalancedFargateServiceProps["cpu"];
13
+ memory?: ApplicationLoadBalancedFargateServiceProps["memoryLimitMiB"];
14
+ }
15
+ /**
16
+ * An Docker cluster on a load balanced Fargate service on EC2, using an image
17
+ * built from a Dockerfile in a directory and pushed to ECR.
18
+ */
19
+ declare class DockerCluster extends Construct {
20
+ constructor(scope: Construct, id: string, props: DockerClusterProps);
21
+ }
22
+
23
+ export { DockerCluster, DockerClusterProps };
@@ -0,0 +1,23 @@
1
+ import { ApplicationLoadBalancedFargateServiceProps } from 'aws-cdk-lib/aws-ecs-patterns';
2
+ import { Construct } from 'constructs';
3
+
4
+ interface DockerClusterProps {
5
+ path: string;
6
+ secrets?: Record<string, string>;
7
+ port?: number;
8
+ tasks?: number | {
9
+ minimum: number;
10
+ maximum: number;
11
+ };
12
+ cpu?: ApplicationLoadBalancedFargateServiceProps["cpu"];
13
+ memory?: ApplicationLoadBalancedFargateServiceProps["memoryLimitMiB"];
14
+ }
15
+ /**
16
+ * An Docker cluster on a load balanced Fargate service on EC2, using an image
17
+ * built from a Dockerfile in a directory and pushed to ECR.
18
+ */
19
+ declare class DockerCluster extends Construct {
20
+ constructor(scope: Construct, id: string, props: DockerClusterProps);
21
+ }
22
+
23
+ export { DockerCluster, DockerClusterProps };
package/dist/index.js ADDED
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ DockerCluster: () => DockerCluster
24
+ });
25
+ module.exports = __toCommonJS(src_exports);
26
+
27
+ // src/constructs/DockerCluster.ts
28
+ var import_cdk_cache_invalidator = require("@codedazur/cdk-cache-invalidator");
29
+ var import_aws_cloudfront = require("aws-cdk-lib/aws-cloudfront");
30
+ var import_aws_cloudfront_origins = require("aws-cdk-lib/aws-cloudfront-origins");
31
+ var import_aws_ecr_assets = require("aws-cdk-lib/aws-ecr-assets");
32
+ var import_aws_ecs = require("aws-cdk-lib/aws-ecs");
33
+ var import_aws_ecs_patterns = require("aws-cdk-lib/aws-ecs-patterns");
34
+ var import_constructs = require("constructs");
35
+ var DockerCluster = class extends import_constructs.Construct {
36
+ constructor(scope, id, props) {
37
+ var _a;
38
+ super(scope, id);
39
+ const image = new import_aws_ecr_assets.DockerImageAsset(this, "Image", {
40
+ directory: props.path,
41
+ buildSecrets: props.secrets,
42
+ platform: import_aws_ecr_assets.Platform.LINUX_AMD64
43
+ });
44
+ const desiredTasks = typeof props.tasks === "number" ? props.tasks : (_a = props.tasks) == null ? void 0 : _a.minimum;
45
+ const fargate = new import_aws_ecs_patterns.ApplicationLoadBalancedFargateService(this, "Service", {
46
+ cluster: new import_aws_ecs.Cluster(this, "Cluster"),
47
+ cpu: props.cpu,
48
+ memoryLimitMiB: props.memory,
49
+ desiredCount: desiredTasks,
50
+ taskImageOptions: {
51
+ // image: ContainerImage.fromEcrRepository(image.repository, image.imageTag),
52
+ image: import_aws_ecs.ContainerImage.fromDockerImageAsset(image),
53
+ containerPort: props.port
54
+ },
55
+ circuitBreaker: {
56
+ enable: true,
57
+ rollback: true
58
+ }
59
+ // @todo certificate: ...
60
+ // @todo publicLoadBalancer: false,
61
+ });
62
+ if (typeof props.tasks === "object") {
63
+ fargate.service.autoScaleTaskCount({
64
+ minCapacity: props.tasks.minimum,
65
+ maxCapacity: props.tasks.maximum
66
+ });
67
+ }
68
+ const distribution = new import_aws_cloudfront.Distribution(this, "Distribution", {
69
+ defaultBehavior: {
70
+ origin: new import_aws_cloudfront_origins.LoadBalancerV2Origin(fargate.loadBalancer, {
71
+ protocolPolicy: import_aws_cloudfront.OriginProtocolPolicy.HTTP_ONLY
72
+ // @todo OriginProtocolPolicy.HTTPS_ONLY
73
+ })
74
+ }
75
+ });
76
+ new import_cdk_cache_invalidator.CacheInvalidator(this, "CacheInvalidator", {
77
+ distribution
78
+ });
79
+ }
80
+ };
81
+ // Annotate the CommonJS export names for ESM import in node:
82
+ 0 && (module.exports = {
83
+ DockerCluster
84
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,59 @@
1
+ // src/constructs/DockerCluster.ts
2
+ import { CacheInvalidator } from "@codedazur/cdk-cache-invalidator";
3
+ import { Distribution, OriginProtocolPolicy } from "aws-cdk-lib/aws-cloudfront";
4
+ import { LoadBalancerV2Origin } from "aws-cdk-lib/aws-cloudfront-origins";
5
+ import { DockerImageAsset, Platform } from "aws-cdk-lib/aws-ecr-assets";
6
+ import { Cluster, ContainerImage } from "aws-cdk-lib/aws-ecs";
7
+ import {
8
+ ApplicationLoadBalancedFargateService
9
+ } from "aws-cdk-lib/aws-ecs-patterns";
10
+ import { Construct } from "constructs";
11
+ var DockerCluster = class extends Construct {
12
+ constructor(scope, id, props) {
13
+ var _a;
14
+ super(scope, id);
15
+ const image = new DockerImageAsset(this, "Image", {
16
+ directory: props.path,
17
+ buildSecrets: props.secrets,
18
+ platform: Platform.LINUX_AMD64
19
+ });
20
+ const desiredTasks = typeof props.tasks === "number" ? props.tasks : (_a = props.tasks) == null ? void 0 : _a.minimum;
21
+ const fargate = new ApplicationLoadBalancedFargateService(this, "Service", {
22
+ cluster: new Cluster(this, "Cluster"),
23
+ cpu: props.cpu,
24
+ memoryLimitMiB: props.memory,
25
+ desiredCount: desiredTasks,
26
+ taskImageOptions: {
27
+ // image: ContainerImage.fromEcrRepository(image.repository, image.imageTag),
28
+ image: ContainerImage.fromDockerImageAsset(image),
29
+ containerPort: props.port
30
+ },
31
+ circuitBreaker: {
32
+ enable: true,
33
+ rollback: true
34
+ }
35
+ // @todo certificate: ...
36
+ // @todo publicLoadBalancer: false,
37
+ });
38
+ if (typeof props.tasks === "object") {
39
+ fargate.service.autoScaleTaskCount({
40
+ minCapacity: props.tasks.minimum,
41
+ maxCapacity: props.tasks.maximum
42
+ });
43
+ }
44
+ const distribution = new Distribution(this, "Distribution", {
45
+ defaultBehavior: {
46
+ origin: new LoadBalancerV2Origin(fargate.loadBalancer, {
47
+ protocolPolicy: OriginProtocolPolicy.HTTP_ONLY
48
+ // @todo OriginProtocolPolicy.HTTPS_ONLY
49
+ })
50
+ }
51
+ });
52
+ new CacheInvalidator(this, "CacheInvalidator", {
53
+ distribution
54
+ });
55
+ }
56
+ };
57
+ export {
58
+ DockerCluster
59
+ };
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@codedazur/cdk-docker-cluster",
3
+ "version": "0.2.0",
4
+ "main": ".dist/index.js",
5
+ "module": "./dist/index.mjs",
6
+ "types": "./dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "require": "./dist/index.js",
10
+ "import": "./dist/index.mjs"
11
+ }
12
+ },
13
+ "publishConfig": {
14
+ "access": "public"
15
+ },
16
+ "license": "MIT",
17
+ "scripts": {
18
+ "develop": "tsup src/index.ts --format esm,cjs --dts --watch",
19
+ "build": "tsup src/index.ts --format esm,cjs --dts",
20
+ "audit": "npm audit --omit dev",
21
+ "lint": "TIMING=1 eslint \"**/*.ts*\"",
22
+ "types": "tsc --noEmit",
23
+ "test": "vitest run --passWithNoTests"
24
+ },
25
+ "dependencies": {
26
+ "@codedazur/cdk-cache-invalidator": "*"
27
+ },
28
+ "peerDependencies": {
29
+ "aws-cdk-lib": ">=2",
30
+ "constructs": ">=10"
31
+ },
32
+ "devDependencies": {
33
+ "@codedazur/eslint-config": "*",
34
+ "@codedazur/tsconfig": "*",
35
+ "@types/node": "^20.12.8",
36
+ "aws-cdk-lib": "^2.139.1",
37
+ "constructs": "^10.3.0",
38
+ "esbuild": "^0.20.2",
39
+ "eslint": "^8.30.0",
40
+ "tsconfig": "*",
41
+ "typescript": "^5.4.5"
42
+ }
43
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,5 @@
1
+ {
2
+ "extends": "@codedazur/tsconfig/cdk.json",
3
+ "include": ["."],
4
+ "exclude": ["dist", "build", "node_modules"]
5
+ }