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(events-targets): add dlq support for ecs target #21396

Merged
merged 3 commits into from
Aug 1, 2022
Merged
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
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-events-targets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ EventBridge.

## Event retry policy and using dead-letter queues

The Codebuild, CodePipeline, Lambda, StepFunctions, LogGroup, SQSQueue and SNSTopic targets support attaching a [dead letter queue and setting retry policies](https://docs.aws.amazon.com/eventbridge/latest/userguide/rule-dlq.html). See the [lambda example](#invoke-a-lambda-function).
The Codebuild, CodePipeline, Lambda, StepFunctions, LogGroup, SQSQueue, SNSTopic and ECSTask targets support attaching a [dead letter queue and setting retry policies](https://docs.aws.amazon.com/eventbridge/latest/userguide/rule-dlq.html). See the [lambda example](#invoke-a-lambda-function).
Use [escape hatches](https://docs.aws.amazon.com/cdk/latest/guide/cfn_layer.html) for the other target types.

## Invoke a Lambda function
Expand Down
9 changes: 7 additions & 2 deletions packages/@aws-cdk/aws-events-targets/lib/ecs-task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import * as iam from '@aws-cdk/aws-iam';
import * as cdk from '@aws-cdk/core';
import { Construct } from 'constructs';
import { ContainerOverride } from './ecs-task-properties';
import { singletonEventRole } from './util';
import { addToDeadLetterQueueResourcePolicy, bindBaseTargetConfig, singletonEventRole, TargetBaseProps } from './util';

/**
* Properties to define an ECS Event Task
*/
export interface EcsTaskProps {
export interface EcsTaskProps extends TargetBaseProps {
/**
* Cluster where service will be deployed
*/
Expand Down Expand Up @@ -180,7 +180,12 @@ export class EcsTask implements events.IRuleTarget {
}
: baseEcsParameters;

if (this.props.deadLetterQueue) {
addToDeadLetterQueueResourcePolicy(_rule, this.props.deadLetterQueue);
}

return {
...bindBaseTargetConfig(this.props),
arn,
role,
ecsParameters,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,50 @@
"EcsClusterDefaultAutoScalingGroupLifecycleHookDrainHookRoleA38EC83B"
]
},
"MyDeadLetterQueueD997968A": {
"Type": "AWS::SQS::Queue",
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete"
},
"MyDeadLetterQueuePolicyCC35D52C": {
"Type": "AWS::SQS::QueuePolicy",
"Properties": {
"PolicyDocument": {
"Statement": [
{
"Action": "sqs:SendMessage",
"Condition": {
"ArnEquals": {
"aws:SourceArn": {
"Fn::GetAtt": [
"Rule4C995B7F",
"Arn"
]
}
}
},
"Effect": "Allow",
"Principal": {
"Service": "events.amazonaws.com"
},
"Resource": {
"Fn::GetAtt": [
"MyDeadLetterQueueD997968A",
"Arn"
]
},
"Sid": "AllowEventRuleawsecsintegecsRuleB8841E8B"
}
],
"Version": "2012-10-17"
},
"Queues": [
{
"Ref": "MyDeadLetterQueueD997968A"
}
]
}
},
"TaskDefTaskRole1EDB4A67": {
"Type": "AWS::IAM::Role",
"Properties": {
Expand Down Expand Up @@ -908,6 +952,14 @@
"Arn"
]
},
"DeadLetterConfig": {
"Arn": {
"Fn::GetAtt": [
"MyDeadLetterQueueD997968A",
"Arn"
]
}
},
"EcsParameters": {
"TaskCount": 1,
"TaskDefinitionArn": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"version": "20.0.0",
"testCases": {
"EcsTest/DefaultTest": {
"stacks": [
"aws-ecs-integ-ecs"
],
"assertionStack": "EcsTestDefaultTestDeployAssert8B2741C4"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,18 @@
"data": "SsmParameterValueawsserviceecsoptimizedamiamazonlinux2recommendedimageidC96584B6F00A464EAD1953AFF4B05118Parameter"
}
],
"/aws-ecs-integ-ecs/MyDeadLetterQueue/Resource": [
{
"type": "aws:cdk:logicalId",
"data": "MyDeadLetterQueueD997968A"
}
],
"/aws-ecs-integ-ecs/MyDeadLetterQueue/Policy/Resource": [
{
"type": "aws:cdk:logicalId",
"data": "MyDeadLetterQueuePolicyCC35D52C"
}
],
"/aws-ecs-integ-ecs/TaskDef/TaskRole/Resource": [
{
"type": "aws:cdk:logicalId",
Expand Down Expand Up @@ -258,6 +270,15 @@
]
},
"displayName": "aws-ecs-integ-ecs"
},
"EcsTestDefaultTestDeployAssert8B2741C4": {
"type": "aws:cloudformation:stack",
"environment": "aws://unknown-account/unknown-region",
"properties": {
"templateFile": "EcsTestDefaultTestDeployAssert8B2741C4.template.json",
"validateOnSynth": false
},
"displayName": "EcsTest/DefaultTest/DeployAssert"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"path": "Tree",
"constructInfo": {
"fqn": "constructs.Construct",
"version": "10.1.33"
"version": "10.1.58"
}
},
"aws-ecs-integ-ecs": {
Expand Down Expand Up @@ -922,7 +922,7 @@
},
"constructInfo": {
"fqn": "constructs.Construct",
"version": "10.1.33"
"version": "10.1.58"
}
},
"LifecycleHookDrainHook": {
Expand Down Expand Up @@ -1102,6 +1102,85 @@
"version": "0.0.0"
}
},
"MyDeadLetterQueue": {
"id": "MyDeadLetterQueue",
"path": "aws-ecs-integ-ecs/MyDeadLetterQueue",
"children": {
"Resource": {
"id": "Resource",
"path": "aws-ecs-integ-ecs/MyDeadLetterQueue/Resource",
"attributes": {
"aws:cdk:cloudformation:type": "AWS::SQS::Queue",
"aws:cdk:cloudformation:props": {}
},
"constructInfo": {
"fqn": "@aws-cdk/aws-sqs.CfnQueue",
"version": "0.0.0"
}
},
"Policy": {
"id": "Policy",
"path": "aws-ecs-integ-ecs/MyDeadLetterQueue/Policy",
"children": {
"Resource": {
"id": "Resource",
"path": "aws-ecs-integ-ecs/MyDeadLetterQueue/Policy/Resource",
"attributes": {
"aws:cdk:cloudformation:type": "AWS::SQS::QueuePolicy",
"aws:cdk:cloudformation:props": {
"policyDocument": {
"Statement": [
{
"Action": "sqs:SendMessage",
"Condition": {
"ArnEquals": {
"aws:SourceArn": {
"Fn::GetAtt": [
"Rule4C995B7F",
"Arn"
]
}
}
},
"Effect": "Allow",
"Principal": {
"Service": "events.amazonaws.com"
},
"Resource": {
"Fn::GetAtt": [
"MyDeadLetterQueueD997968A",
"Arn"
]
},
"Sid": "AllowEventRuleawsecsintegecsRuleB8841E8B"
}
],
"Version": "2012-10-17"
},
"queues": [
{
"Ref": "MyDeadLetterQueueD997968A"
}
]
}
},
"constructInfo": {
"fqn": "@aws-cdk/aws-sqs.CfnQueuePolicy",
"version": "0.0.0"
}
}
},
"constructInfo": {
"fqn": "@aws-cdk/aws-sqs.QueuePolicy",
"version": "0.0.0"
}
}
},
"constructInfo": {
"fqn": "@aws-cdk/aws-sqs.Queue",
"version": "0.0.0"
}
},
"TaskDef": {
"id": "TaskDef",
"path": "aws-ecs-integ-ecs/TaskDef",
Expand Down Expand Up @@ -1517,6 +1596,14 @@
"Ref": "TaskDef54694570"
}
},
"deadLetterConfig": {
"arn": {
"Fn::GetAtt": [
"MyDeadLetterQueueD997968A",
"Arn"
]
}
},
"input": "{\"containerOverrides\":[{\"name\":\"TheContainer\",\"environment\":[{\"name\":\"I_WAS_TRIGGERED\",\"value\":\"From CloudWatch Events\"}]}]}"
}
]
Expand All @@ -1538,6 +1625,42 @@
"fqn": "@aws-cdk/core.Stack",
"version": "0.0.0"
}
},
"EcsTest": {
"id": "EcsTest",
"path": "EcsTest",
"children": {
"DefaultTest": {
"id": "DefaultTest",
"path": "EcsTest/DefaultTest",
"children": {
"Default": {
"id": "Default",
"path": "EcsTest/DefaultTest/Default",
"constructInfo": {
"fqn": "constructs.Construct",
"version": "10.1.58"
}
},
"DeployAssert": {
"id": "DeployAssert",
"path": "EcsTest/DefaultTest/DeployAssert",
"constructInfo": {
"fqn": "@aws-cdk/core.Stack",
"version": "0.0.0"
}
}
},
"constructInfo": {
"fqn": "@aws-cdk/integ-tests.IntegTestCase",
"version": "0.0.0"
}
}
},
"constructInfo": {
"fqn": "@aws-cdk/integ-tests.IntegTest",
"version": "0.0.0"
}
}
},
"constructInfo": {
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"version": "17.0.0",
"version": "20.0.0",
"files": {
"f68c909c141898dc9228a20066545eb77eb23a8b9d68b9231c2b2636e53e1832": {
"b1481d24efb56aa5b7c39fd39926bf062d67288f130d5c694a59ca686db1d30a": {
"source": {
"path": "aws-ecs-integ-fargate.template.json",
"packaging": "file"
},
"destinations": {
"current_account-current_region": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
"objectKey": "f68c909c141898dc9228a20066545eb77eb23a8b9d68b9231c2b2636e53e1832.json",
"objectKey": "b1481d24efb56aa5b7c39fd39926bf062d67288f130d5c694a59ca686db1d30a.json",
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
}
}
Expand Down
Loading