@fy-stack/task-construct 0.0.139 → 0.0.141
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/dist/lib/service-construct.d.ts +18 -0
- package/dist/lib/service-construct.d.ts.map +1 -0
- package/dist/lib/service-construct.js +102 -0
- package/dist/lib/task-construct.d.ts +2 -2
- package/dist/lib/task-construct.d.ts.map +1 -1
- package/dist/lib/task-construct.js +30 -29
- package/dist/lib/types.d.ts +10 -2
- package/dist/lib/types.d.ts.map +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Attach, Attachable, 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 ServiceConstruct extends Construct implements 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=service-construct.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"service-construct.d.ts","sourceRoot":"","sources":["../../src/lib/service-construct.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,MAAM,EACN,UAAU,EACV,KAAK,EACL,SAAS,EACV,MAAM,iBAAiB,CAAC;AAGzB,OAAO,KAAK,GAAG,MAAM,qBAAqB,CAAC;AAC3C,OAAO,KAAK,GAAG,MAAM,qBAAqB,CAAC;AAE3C,OAAO,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AACzD,OAAO,EAEL,iBAAiB,EAClB,MAAM,mCAAmC,CAAC;AAE3C,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEvC,OAAO,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAE9C,qBAAa,gBACX,SAAQ,SACR,YAAW,KAAK,EAAE,MAAM;IAExB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAe;IAEhC,IAAI,EAAE,GAAG,CAAC,IAAI,CAAC;IACf,cAAc,EAAE,GAAG,CAAC,qBAAqB,CAAC;IACjD,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAW;gBAEnB,KAAK,EAAE,SAAS,EAAE,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,mBAAmB;IAkCpE,YAAY,CAAC,KAAK,EAAE,iBAAiB,GAAG,kBAAkB;IA0C1D,KAAK,CAAC,GAAG,UAAU,EAAE,SAAS,EAAE,GAAG,IAAI;IAMvC,MAAM,CAAC,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC;CAmB9C"}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ServiceConstruct = 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 ServiceConstruct 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: 2048,
|
|
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).map(([key, val]) => {
|
|
91
|
+
return Object.fromEntries(Object.entries(val?.attachable() ?? {}).map(([subKey, subVal]) => [
|
|
92
|
+
`${key}_${subKey}`.toUpperCase(),
|
|
93
|
+
subVal,
|
|
94
|
+
]));
|
|
95
|
+
}));
|
|
96
|
+
for (const i in params) {
|
|
97
|
+
const container = this.taskDefinition.defaultContainer;
|
|
98
|
+
container?.addEnvironment(i, params[i]);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
exports.ServiceConstruct = ServiceConstruct;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Attach, Attachable, EventResource, Grant, Grantable } from '@fy-stack/types';
|
|
2
|
-
import * as ecs from
|
|
3
|
-
import * as iam from
|
|
2
|
+
import * as ecs from 'aws-cdk-lib/aws-ecs';
|
|
3
|
+
import * as iam from 'aws-cdk-lib/aws-iam';
|
|
4
4
|
import { ITopicSubscription } from 'aws-cdk-lib/aws-sns';
|
|
5
5
|
import { SubscriptionProps } from 'aws-cdk-lib/aws-sns-subscriptions';
|
|
6
6
|
import { Construct } from 'constructs';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"task-construct.d.ts","sourceRoot":"","sources":["../../src/lib/task-construct.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"task-construct.d.ts","sourceRoot":"","sources":["../../src/lib/task-construct.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,MAAM,EACN,UAAU,EACV,aAAa,EACb,KAAK,EACL,SAAS,EACV,MAAM,iBAAiB,CAAC;AAGzB,OAAO,KAAK,GAAG,MAAM,qBAAqB,CAAC;AAC3C,OAAO,KAAK,GAAG,MAAM,qBAAqB,CAAC;AAE3C,OAAO,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AACzD,OAAO,EAEL,iBAAiB,EAClB,MAAM,mCAAmC,CAAC;AAE3C,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEvC,OAAO,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAE9C,qBAAa,aACX,SAAQ,SACR,YAAW,aAAa,EAAE,KAAK,EAAE,MAAM;IAEvC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAe;IAEhC,IAAI,EAAE,GAAG,CAAC,IAAI,CAAC;IACf,cAAc,EAAE,GAAG,CAAC,qBAAqB,CAAC;IACjD,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAW;gBAEnB,KAAK,EAAE,SAAS,EAAE,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,mBAAmB;IAkCpE,YAAY,CAAC,KAAK,EAAE,iBAAiB,GAAG,kBAAkB;IA0C1D,KAAK,CAAC,GAAG,UAAU,EAAE,SAAS,EAAE,GAAG,IAAI;IAMvC,MAAM,CAAC,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC;CAmB9C"}
|
|
@@ -17,25 +17,25 @@ class TaskConstruct extends constructs_1.Construct {
|
|
|
17
17
|
constructor(scope, id, props) {
|
|
18
18
|
super(scope, id);
|
|
19
19
|
const { clusterArn, defaultImage, output, vpc, ...definitionProps } = props;
|
|
20
|
-
this.cluster = ecs.Cluster.fromClusterArn(this,
|
|
20
|
+
this.cluster = ecs.Cluster.fromClusterArn(this, 'TaskCluster', clusterArn);
|
|
21
21
|
this.vpc = vpc;
|
|
22
22
|
this.role = new iam.Role(this, 'Role', {
|
|
23
|
-
assumedBy: new iam.ServicePrincipal(
|
|
23
|
+
assumedBy: new iam.ServicePrincipal('pipes.amazonaws.com'),
|
|
24
24
|
});
|
|
25
|
-
this.taskDefinition = new ecs.FargateTaskDefinition(this,
|
|
25
|
+
this.taskDefinition = new ecs.FargateTaskDefinition(this, 'Task', {
|
|
26
26
|
cpu: 1024,
|
|
27
|
-
memoryLimitMiB:
|
|
27
|
+
memoryLimitMiB: 2048,
|
|
28
28
|
runtimePlatform: {
|
|
29
|
-
cpuArchitecture: ecs.CpuArchitecture.X86_64
|
|
29
|
+
cpuArchitecture: ecs.CpuArchitecture.X86_64,
|
|
30
30
|
},
|
|
31
|
-
...definitionProps
|
|
31
|
+
...definitionProps,
|
|
32
32
|
});
|
|
33
33
|
if (defaultImage) {
|
|
34
34
|
const { container: containerProps, ...imageProps } = defaultImage;
|
|
35
|
-
this.taskDefinition.addContainer(
|
|
35
|
+
this.taskDefinition.addContainer('DefaultImage', {
|
|
36
36
|
image: ecs.ContainerImage.fromAsset(output, {
|
|
37
37
|
platform: ecrAssets.Platform.LINUX_AMD64,
|
|
38
|
-
...containerProps
|
|
38
|
+
...containerProps,
|
|
39
39
|
}),
|
|
40
40
|
logging: new ecs.AwsLogDriver({ streamPrefix: `${id}/task-runner` }),
|
|
41
41
|
...imageProps,
|
|
@@ -43,38 +43,38 @@ class TaskConstruct extends constructs_1.Construct {
|
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
45
|
subscription(props) {
|
|
46
|
-
const queue = new sqs.Queue(this,
|
|
47
|
-
const pipeRole = new iam.Role(this,
|
|
48
|
-
assumedBy: new iam.ServicePrincipal(
|
|
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
49
|
});
|
|
50
|
-
new pipes.CfnPipe(this,
|
|
50
|
+
new pipes.CfnPipe(this, 'TaskPipe', {
|
|
51
51
|
source: queue.queueArn,
|
|
52
52
|
target: this.cluster.clusterArn,
|
|
53
53
|
roleArn: pipeRole.roleArn,
|
|
54
|
-
desiredState:
|
|
54
|
+
desiredState: 'RUNNING',
|
|
55
55
|
targetParameters: {
|
|
56
56
|
ecsTaskParameters: {
|
|
57
57
|
taskDefinitionArn: this.taskDefinition.taskDefinitionArn,
|
|
58
|
-
launchType:
|
|
58
|
+
launchType: 'FARGATE',
|
|
59
59
|
networkConfiguration: {
|
|
60
60
|
awsvpcConfiguration: {
|
|
61
|
-
assignPublicIp:
|
|
62
|
-
subnets: this.vpc.publicSubnets.map(v => v.subnetId),
|
|
63
|
-
}
|
|
61
|
+
assignPublicIp: 'ENABLED',
|
|
62
|
+
subnets: this.vpc.publicSubnets.map((v) => v.subnetId),
|
|
63
|
+
},
|
|
64
64
|
},
|
|
65
65
|
overrides: {
|
|
66
66
|
containerOverrides: [
|
|
67
67
|
{
|
|
68
|
-
name:
|
|
68
|
+
name: 'DefaultImage',
|
|
69
69
|
environment: [
|
|
70
|
-
{ name:
|
|
71
|
-
{ name:
|
|
70
|
+
{ name: 'TASK_NAME', value: '$.body.message' },
|
|
71
|
+
{ name: 'PAYLOAD', value: '$.body.payload' },
|
|
72
72
|
],
|
|
73
|
-
}
|
|
74
|
-
]
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
}
|
|
73
|
+
},
|
|
74
|
+
],
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
78
|
});
|
|
79
79
|
queue.grantConsumeMessages(pipeRole);
|
|
80
80
|
this.taskDefinition.grantRun(pipeRole);
|
|
@@ -87,10 +87,11 @@ class TaskConstruct extends constructs_1.Construct {
|
|
|
87
87
|
}
|
|
88
88
|
attach(attachable) {
|
|
89
89
|
const params = {};
|
|
90
|
-
Object.assign(params, ...Object.entries(attachable)
|
|
91
|
-
.map(([
|
|
92
|
-
|
|
93
|
-
|
|
90
|
+
Object.assign(params, ...Object.entries(attachable).map(([key, val]) => {
|
|
91
|
+
return Object.fromEntries(Object.entries(val?.attachable() ?? {}).map(([subKey, subVal]) => [
|
|
92
|
+
`${key}_${subKey}`.toUpperCase(),
|
|
93
|
+
subVal,
|
|
94
|
+
]));
|
|
94
95
|
}));
|
|
95
96
|
for (const i in params) {
|
|
96
97
|
const container = this.taskDefinition.defaultContainer;
|
package/dist/lib/types.d.ts
CHANGED
|
@@ -1,11 +1,19 @@
|
|
|
1
1
|
import type { IVpc } from 'aws-cdk-lib/aws-ec2';
|
|
2
|
-
import type { AssetImageProps, ContainerDefinitionOptions, FargateTaskDefinitionProps } from 'aws-cdk-lib/aws-ecs';
|
|
2
|
+
import type { AssetImageProps, ContainerDefinitionOptions, Ec2TaskDefinitionProps, FargateTaskDefinitionProps } from 'aws-cdk-lib/aws-ecs';
|
|
3
3
|
export type TaskConstructsProps = FargateTaskDefinitionProps & {
|
|
4
4
|
vpc: IVpc;
|
|
5
5
|
clusterArn: string;
|
|
6
6
|
env?: Record<string, string>;
|
|
7
7
|
output: string;
|
|
8
|
-
defaultImage?: Omit<ContainerDefinitionOptions,
|
|
8
|
+
defaultImage?: Omit<ContainerDefinitionOptions, 'image' | 'logging'> & {
|
|
9
|
+
container: AssetImageProps;
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
export type ServiceConstructsProps = Ec2TaskDefinitionProps & {
|
|
13
|
+
vpc: IVpc;
|
|
14
|
+
env?: Record<string, string>;
|
|
15
|
+
output: string;
|
|
16
|
+
defaultImage?: Omit<ContainerDefinitionOptions, 'image' | 'logging'> & {
|
|
9
17
|
container: AssetImageProps;
|
|
10
18
|
};
|
|
11
19
|
};
|
package/dist/lib/types.d.ts.map
CHANGED
|
@@ -1 +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,
|
|
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,EACV,eAAe,EACf,0BAA0B,EAC1B,sBAAsB,EACtB,0BAA0B,EAC3B,MAAM,qBAAqB,CAAC;AAE7B,MAAM,MAAM,mBAAmB,GAAG,0BAA0B,GAAG;IAC7D,GAAG,EAAE,IAAI,CAAC;IACV,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;QACrE,SAAS,EAAE,eAAe,CAAC;KAC5B,CAAC;CACH,CAAC;AAEF,MAAM,MAAM,sBAAsB,GAAG,sBAAsB,GAAG;IAC5D,GAAG,EAAE,IAAI,CAAC;IACV,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;QACrE,SAAS,EAAE,eAAe,CAAC;KAC5B,CAAC;CACH,CAAC"}
|