@fy-stack/task-construct 0.0.130

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/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # task-construct
2
+
3
+ This library was generated with [Nx](https://nx.dev).
4
+
5
+ ## Building
6
+
7
+ Run `nx build task-construct` to build the library.
@@ -0,0 +1,3 @@
1
+ export { TaskConstruct } from './lib/task-construct';
2
+ export type * from "./lib/types";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACrD,mBAAmB,aAAa,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TaskConstruct = void 0;
4
+ var task_construct_1 = require("./lib/task-construct");
5
+ Object.defineProperty(exports, "TaskConstruct", { enumerable: true, get: function () { return task_construct_1.TaskConstruct; } });
@@ -0,0 +1,18 @@
1
+ import { Attach, Attachable, EventResource, Grant, Grantable } from '@fy-stack/types';
2
+ import * as ecs from "aws-cdk-lib/aws-ecs";
3
+ import * as iam from "aws-cdk-lib/aws-iam";
4
+ import { ITopicSubscription } from 'aws-cdk-lib/aws-sns';
5
+ import { SubscriptionProps } from 'aws-cdk-lib/aws-sns-subscriptions';
6
+ import { Construct } from 'constructs';
7
+ import { TaskConstructsProps } from './types';
8
+ export declare class TaskConstruct extends Construct implements EventResource, Grant, Attach {
9
+ private readonly cluster;
10
+ role: iam.Role;
11
+ taskDefinition: ecs.FargateTaskDefinition;
12
+ private readonly vpc;
13
+ constructor(scope: Construct, id: string, props: TaskConstructsProps);
14
+ subscription(props: SubscriptionProps): ITopicSubscription;
15
+ grant(...grantables: Grantable[]): void;
16
+ attach(attachable: Record<string, Attachable>): void;
17
+ }
18
+ //# sourceMappingURL=task-construct.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"task-construct.d.ts","sourceRoot":"","sources":["../../src/lib/task-construct.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,aAAa,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAGtF,OAAO,KAAK,GAAG,MAAM,qBAAqB,CAAA;AAC1C,OAAO,KAAK,GAAG,MAAM,qBAAqB,CAAA;AAE1C,OAAO,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AACzD,OAAO,EAAmB,iBAAiB,EAAE,MAAM,mCAAmC,CAAC;AAEvF,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEvC,OAAO,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAE9C,qBAAa,aAAc,SAAQ,SAAU,YAAW,aAAa,EAAE,KAAK,EAAE,MAAM;IAElF,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAe;IAEhC,IAAI,EAAE,GAAG,CAAC,IAAI,CAAA;IACd,cAAc,EAAE,GAAG,CAAC,qBAAqB,CAAA;IAChD,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAU;gBAElB,KAAK,EAAE,SAAS,EAAE,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,mBAAmB;IAkCpE,YAAY,CAAC,KAAK,EAAE,iBAAiB,GAAG,kBAAkB;IA2C1D,KAAK,CAAC,GAAG,UAAU,EAAE,SAAS,EAAE,GAAG,IAAI;IAMvC,MAAM,CAAC,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC;CAkB9C"}
@@ -0,0 +1,101 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TaskConstruct = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const ecrAssets = tslib_1.__importStar(require("aws-cdk-lib/aws-ecr-assets"));
6
+ const ecs = tslib_1.__importStar(require("aws-cdk-lib/aws-ecs"));
7
+ const iam = tslib_1.__importStar(require("aws-cdk-lib/aws-iam"));
8
+ const pipes = tslib_1.__importStar(require("aws-cdk-lib/aws-pipes"));
9
+ const aws_sns_subscriptions_1 = require("aws-cdk-lib/aws-sns-subscriptions");
10
+ const sqs = tslib_1.__importStar(require("aws-cdk-lib/aws-sqs"));
11
+ const constructs_1 = require("constructs");
12
+ class TaskConstruct extends constructs_1.Construct {
13
+ cluster;
14
+ role;
15
+ taskDefinition;
16
+ vpc;
17
+ constructor(scope, id, props) {
18
+ super(scope, id);
19
+ const { clusterArn, defaultImage, output, vpc, ...definitionProps } = props;
20
+ this.cluster = ecs.Cluster.fromClusterArn(this, "TaskCluster", clusterArn);
21
+ this.vpc = vpc;
22
+ this.role = new iam.Role(this, 'Role', {
23
+ assumedBy: new iam.ServicePrincipal("pipes.amazonaws.com")
24
+ });
25
+ this.taskDefinition = new ecs.FargateTaskDefinition(this, "Task", {
26
+ cpu: 1024,
27
+ memoryLimitMiB: 4096,
28
+ runtimePlatform: {
29
+ cpuArchitecture: ecs.CpuArchitecture.X86_64
30
+ },
31
+ ...definitionProps
32
+ });
33
+ if (defaultImage) {
34
+ const { container: containerProps, ...imageProps } = defaultImage;
35
+ this.taskDefinition.addContainer("DefaultImage", {
36
+ image: ecs.ContainerImage.fromAsset(output, {
37
+ platform: ecrAssets.Platform.LINUX_AMD64,
38
+ ...containerProps
39
+ }),
40
+ logging: new ecs.AwsLogDriver({ streamPrefix: `${id}/task-runner` }),
41
+ ...imageProps,
42
+ });
43
+ }
44
+ }
45
+ subscription(props) {
46
+ const queue = new sqs.Queue(this, "TaskQueue");
47
+ const pipeRole = new iam.Role(this, "PipeRole", {
48
+ assumedBy: new iam.ServicePrincipal("pipes.amazonaws.com")
49
+ });
50
+ new pipes.CfnPipe(this, "TaskPipe", {
51
+ source: queue.queueArn,
52
+ target: this.cluster.clusterArn,
53
+ roleArn: pipeRole.roleArn,
54
+ desiredState: "RUNNING",
55
+ targetParameters: {
56
+ ecsTaskParameters: {
57
+ taskDefinitionArn: this.taskDefinition.taskDefinitionArn,
58
+ launchType: "FARGATE",
59
+ networkConfiguration: {
60
+ awsvpcConfiguration: {
61
+ assignPublicIp: "ENABLED",
62
+ subnets: this.vpc.publicSubnets.map(v => v.subnetId),
63
+ }
64
+ },
65
+ overrides: {
66
+ containerOverrides: [
67
+ {
68
+ name: "DefaultImage",
69
+ environment: [
70
+ { name: "TASK_NAME", value: "$.body.message" },
71
+ { name: "PAYLOAD", value: "$.body.payload" }
72
+ ],
73
+ }
74
+ ]
75
+ }
76
+ }
77
+ }
78
+ });
79
+ queue.grantConsumeMessages(pipeRole);
80
+ this.taskDefinition.grantRun(pipeRole);
81
+ return new aws_sns_subscriptions_1.SqsSubscription(queue, { ...props, rawMessageDelivery: true });
82
+ }
83
+ grant(...grantables) {
84
+ for (const i in grantables) {
85
+ grantables[i].grantable(this.role);
86
+ }
87
+ }
88
+ attach(attachable) {
89
+ const params = {};
90
+ Object.assign(params, ...Object.entries(attachable)
91
+ .map(([key, val]) => {
92
+ return Object.fromEntries(Object.entries(val?.attachable() ?? {})
93
+ .map(([subKey, subVal]) => [`${key}_${subKey}`, subVal]));
94
+ }));
95
+ for (const i in params) {
96
+ const container = this.taskDefinition.defaultContainer;
97
+ container?.addEnvironment(i, params[i]);
98
+ }
99
+ }
100
+ }
101
+ exports.TaskConstruct = TaskConstruct;
@@ -0,0 +1,12 @@
1
+ import type { IVpc } from 'aws-cdk-lib/aws-ec2';
2
+ import type { AssetImageProps, ContainerDefinitionOptions, FargateTaskDefinitionProps } from 'aws-cdk-lib/aws-ecs';
3
+ export type TaskConstructsProps = FargateTaskDefinitionProps & {
4
+ vpc: IVpc;
5
+ clusterArn: string;
6
+ env?: Record<string, string>;
7
+ output: string;
8
+ defaultImage?: Omit<ContainerDefinitionOptions, "image" | "logging"> & {
9
+ container: AssetImageProps;
10
+ };
11
+ };
12
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/lib/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,qBAAqB,CAAC;AAChD,OAAO,KAAK,EAAE,eAAe,EAAE,0BAA0B,EAAE,0BAA0B,EAAE,MAAM,qBAAqB,CAAC;AAEnH,MAAM,MAAM,mBAAmB,GAAG,0BAA0B,GAAG;IAC7D,GAAG,EAAE,IAAI,CAAA;IACT,UAAU,EAAE,MAAM,CAAC;IACnB,GAAG,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC7B,MAAM,EAAE,MAAM,CAAC;IACf,YAAY,CAAC,EAAE,IAAI,CAAC,0BAA0B,EAAE,OAAO,GAAG,SAAS,CAAC,GAAG;QAAE,SAAS,EAAE,eAAe,CAAA;KAAE,CAAA;CACtG,CAAA"}
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "name": "@fy-stack/task-construct",
3
+ "version": "0.0.130",
4
+ "dependencies": {
5
+ "@fy-stack/types": "0.0.130",
6
+ "tslib": "^2.3.0"
7
+ },
8
+ "peerDependencies": {
9
+ "aws-cdk-lib": "^2.174.1",
10
+ "constructs": "^10.4.2"
11
+ },
12
+ "type": "commonjs",
13
+ "main": "./dist/index.js",
14
+ "typings": "./dist/index.d.ts",
15
+ "files": [
16
+ "dist",
17
+ "!**/*.tsbuildinfo"
18
+ ],
19
+ "nx": {
20
+ "name": "task-construct"
21
+ }
22
+ }