Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(aws-ecs-patterns): Add tagList and propagateTags to scheduled tasks #23666

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions packages/@aws-cdk/aws-ecs-patterns/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,45 @@ const scheduledFargateTask = new ecsPatterns.ScheduledFargateTask(this, 'Schedul
});
```

### Set Propagate Tags for ScheduledFargateTask

```ts
const vpc = new ec2.Vpc(this, 'Vpc', { maxAzs: 1 });
const cluster = new ecs.Cluster(this, 'EcsCluster', { vpc });

const scheduledFargateTask = new ecsPatterns.ScheduledFargateTask(this, 'ScheduledFargateTask', {
cluster,
scheduledFargateTaskImageOptions: {
image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
memoryLimitMiB: 512,
},
schedule: appscaling.Schedule.expression('rate(1 minute)'),
propagateTags: true,
});
```

### Pass a list of tags for ScheduledFargateTask

```ts
const vpc = new ec2.Vpc(this, 'Vpc', { maxAzs: 1 });
const cluster = new ecs.Cluster(this, 'EcsCluster', { vpc });

const scheduledFargateTask = new ecsPatterns.ScheduledFargateTask(this, 'ScheduledFargateTask', {
cluster,
scheduledFargateTaskImageOptions: {
image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
memoryLimitMiB: 512,
},
schedule: appscaling.Schedule.expression('rate(1 minute)'),
tagList: [
{
key: 'my-tag',
value: 'my-tag-value',
},
],
});
```

### Use the REMOVE_DEFAULT_DESIRED_COUNT feature flag

The REMOVE_DEFAULT_DESIRED_COUNT feature flag is used to override the default desiredCount that is autogenerated by the CDK. This will set the desiredCount of any service created by any of the following constructs to be undefined.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Schedule } from '@aws-cdk/aws-applicationautoscaling';
import { ISecurityGroup, IVpc, SubnetSelection, SubnetType } from '@aws-cdk/aws-ec2';
import { AwsLogDriver, Cluster, ContainerImage, ICluster, LogDriver, Secret, TaskDefinition } from '@aws-cdk/aws-ecs';
import { Rule } from '@aws-cdk/aws-events';
import { EcsTask } from '@aws-cdk/aws-events-targets';
import { EcsTask, Tag } from '@aws-cdk/aws-events-targets';
import { Stack } from '@aws-cdk/core';
import { Construct } from 'constructs';

Expand Down Expand Up @@ -71,6 +71,20 @@ export interface ScheduledTaskBaseProps {
* @default - a new security group will be created.
*/
readonly securityGroups?: ISecurityGroup[]

/**
* Specifies whether to propagate the tags from the task definition to the task. If no value is specified, the tags are not propagated.
*
* @default - Tags will not be propagated
*/
readonly propagateTags?: boolean;

/**
* The metadata that you apply to the task to help you categorize and organize them. Each tag consists of a key and an optional value, both of which you define.
*
* @default - No tags are applied to the task
*/
readonly tagList?: Tag[]
}

export interface ScheduledTaskImageProps {
Expand Down Expand Up @@ -146,6 +160,19 @@ export abstract class ScheduledTaskBase extends Construct {
*/
private readonly _securityGroups?: ISecurityGroup[];

/**
* Specifies whether to propagate the tags from the task definition to the task. If no value is specified, the tags are not propagated.
*/
readonly propagateTags?: boolean;

/**
* The metadata that you apply to the task to help you categorize and organize them. Each tag consists of a key and an optional value, both of which you define.
*
* @default - No tags are applied to the task
*/
readonly tagList?: Tag[]


/**
* Constructs a new instance of the ScheduledTaskBase class.
*/
Expand All @@ -159,6 +186,8 @@ export abstract class ScheduledTaskBase extends Construct {
this.desiredTaskCount = props.desiredTaskCount || 1;
this.subnetSelection = props.subnetSelection || { subnetType: SubnetType.PRIVATE_WITH_EGRESS };
this._securityGroups = props.securityGroups;
this.propagateTags = props.propagateTags;
this.tagList = props.tagList;

// An EventRule that describes the event trigger (in this case a scheduled run)
this.eventRule = new Rule(this, 'ScheduledEventRule', {
Expand All @@ -184,6 +213,8 @@ export abstract class ScheduledTaskBase extends Construct {
taskCount: this.desiredTaskCount,
subnetSelection: this.subnetSelection,
securityGroups: this._securityGroups,
propagateTags: this.propagateTags,
tagList: this.tagList,
});

this.addTaskAsTarget(eventRuleTarget);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ export class ScheduledFargateTask extends ScheduledTaskBase {
subnetSelection: this.subnetSelection,
platformVersion: props.platformVersion,
securityGroups: props.securityGroups,
propagateTags: props.propagateTags,
tagList: props.tagList,
});

this.addTaskAsTarget(this.task);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"version": "21.0.0",
"version": "29.0.0",
"files": {
"ca15a60f3a20460e6534c82e90142c42bda680cee1d53044cff4136cb5b7feb5": {
"45ee1817116bffce287e51bd4477bcad440916657502fad9157093fa3d54f67d": {
"source": {
"path": "aws-ecs-integ-ecs.template.json",
"packaging": "file"
},
"destinations": {
"current_account-current_region": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
"objectKey": "ca15a60f3a20460e6534c82e90142c42bda680cee1d53044cff4136cb5b7feb5.json",
"objectKey": "45ee1817116bffce287e51bd4477bcad440916657502fad9157093fa3d54f67d.json",
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,13 @@
]
},
"EcsParameters": {
"PropagateTags": "TASK_DEFINITION",
"TagList": [
{
"Key": "my-tag",
"Value": "my-tag-value"
}
],
"TaskCount": 2,
"TaskDefinitionArn": {
"Ref": "ScheduledEc2TaskScheduledTaskDef56328BA4"
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":"20.0.0"}
{"version":"29.0.0"}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "21.0.0",
"version": "29.0.0",
"testCases": {
"scheduledEc2TaskTest/DefaultTest": {
"stacks": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
{
"version": "21.0.0",
"version": "29.0.0",
"artifacts": {
"Tree": {
"type": "cdk:tree",
"properties": {
"file": "tree.json"
}
},
"aws-ecs-integ-ecs.assets": {
"type": "cdk:asset-manifest",
"properties": {
Expand All @@ -23,7 +17,7 @@
"validateOnSynth": false,
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}",
"cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}",
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/ca15a60f3a20460e6534c82e90142c42bda680cee1d53044cff4136cb5b7feb5.json",
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/45ee1817116bffce287e51bd4477bcad440916657502fad9157093fa3d54f67d.json",
"requiresBootstrapStackVersion": 6,
"bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version",
"additionalDependencies": [
Expand Down Expand Up @@ -328,6 +322,12 @@
]
},
"displayName": "scheduledEc2TaskTest/DefaultTest/DeployAssert"
},
"Tree": {
"type": "cdk:tree",
"properties": {
"file": "tree.json"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "21.0.0",
"version": "29.0.0",
"files": {
"21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": {
"source": {
Expand Down
Loading