@fy-stack/task-construct 0.0.149 → 0.0.150-alpha.30002
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.
|
@@ -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;
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fy-stack/task-construct",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.150-alpha.30002",
|
|
4
4
|
"repository": "https://github.com/festusyuma/fy-stack",
|
|
5
5
|
"dependencies": {
|
|
6
|
-
"@fy-stack/types": "0.0.
|
|
6
|
+
"@fy-stack/types": "0.0.150-alpha.30002",
|
|
7
7
|
"tslib": "^2.3.0"
|
|
8
8
|
},
|
|
9
9
|
"peerDependencies": {
|