@fy-stack/task-construct 0.0.140 → 0.0.142

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;
@@ -1,6 +1,6 @@
1
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";
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,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;IA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,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, "TaskCluster", clusterArn);
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("pipes.amazonaws.com")
23
+ assumedBy: new iam.ServicePrincipal('pipes.amazonaws.com'),
24
24
  });
25
- this.taskDefinition = new ecs.FargateTaskDefinition(this, "Task", {
25
+ this.taskDefinition = new ecs.FargateTaskDefinition(this, 'Task', {
26
26
  cpu: 1024,
27
- memoryLimitMiB: 4096,
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("DefaultImage", {
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, "TaskQueue");
47
- const pipeRole = new iam.Role(this, "PipeRole", {
48
- assumedBy: new iam.ServicePrincipal("pipes.amazonaws.com")
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, "TaskPipe", {
50
+ new pipes.CfnPipe(this, 'TaskPipe', {
51
51
  source: queue.queueArn,
52
52
  target: this.cluster.clusterArn,
53
53
  roleArn: pipeRole.roleArn,
54
- desiredState: "RUNNING",
54
+ desiredState: 'RUNNING',
55
55
  targetParameters: {
56
56
  ecsTaskParameters: {
57
57
  taskDefinitionArn: this.taskDefinition.taskDefinitionArn,
58
- launchType: "FARGATE",
58
+ launchType: 'FARGATE',
59
59
  networkConfiguration: {
60
60
  awsvpcConfiguration: {
61
- assignPublicIp: "ENABLED",
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: "DefaultImage",
68
+ name: 'DefaultImage',
69
69
  environment: [
70
- { name: "TASK_NAME", value: "$.body.message" },
71
- { name: "PAYLOAD", value: "$.body.payload" }
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(([key, val]) => {
92
- return Object.fromEntries(Object.entries(val?.attachable() ?? {})
93
- .map(([subKey, subVal]) => [`${key}_${subKey}`, subVal]));
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;
@@ -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, "image" | "logging"> & {
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
  };
@@ -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,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"}
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"}
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@fy-stack/task-construct",
3
- "version": "0.0.140",
3
+ "version": "0.0.142",
4
4
  "dependencies": {
5
- "@fy-stack/types": "0.0.140",
5
+ "@fy-stack/types": "0.0.142",
6
6
  "tslib": "^2.3.0"
7
7
  },
8
8
  "peerDependencies": {