Skip to content

Commit

Permalink
fix(sns): create subscriptions in consumer scope (#3065)
Browse files Browse the repository at this point in the history
Since in most cases the consumer needs to reference the topic to
permit it to send them messages (e.g. invoke a lambda function or send
messages to the queue), it makes much more send to create the SNS subscription
resource on the consumer's scope/stack instead of the topic's.

This change adds an optional scope field to TopicSubscriptionConfig which
is respected by topic.addSubscription. If scope is not defined, the topic's
scope will be used. We also changed subscriberId to be optional, since in the
case where scope is specified, the natural ID for the subscription construct
would be the topic's unique ID, which is now the default. A runtime error will
be emitted if both scope and subscriberId are not provided.

Fixes #3064
  • Loading branch information
Elad Ben-Israel authored Jun 27, 2019
1 parent 71ad83d commit 64a203f
Show file tree
Hide file tree
Showing 19 changed files with 580 additions and 486 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -506,34 +506,34 @@
"EcsClusterDefaultAutoScalingGroupDrainECSHookFunctionServiceRole94543EDA"
]
},
"EcsClusterDefaultAutoScalingGroupLifecycleHookDrainHookTopicFunctionA8966A35": {
"Type": "AWS::SNS::Subscription",
"EcsClusterDefaultAutoScalingGroupDrainECSHookFunctionAllowInvokeawsecsintegecsEcsClusterDefaultAutoScalingGroupLifecycleHookDrainHookTopic0C4958AFBA77E328": {
"Type": "AWS::Lambda::Permission",
"Properties": {
"Protocol": "lambda",
"TopicArn": {
"Ref": "EcsClusterDefaultAutoScalingGroupLifecycleHookDrainHookTopicACD2D4A4"
},
"Endpoint": {
"Action": "lambda:InvokeFunction",
"FunctionName": {
"Fn::GetAtt": [
"EcsClusterDefaultAutoScalingGroupDrainECSHookFunctionE17A5F5E",
"Arn"
]
},
"Principal": "sns.amazonaws.com",
"SourceArn": {
"Ref": "EcsClusterDefaultAutoScalingGroupLifecycleHookDrainHookTopicACD2D4A4"
}
}
},
"EcsClusterDefaultAutoScalingGroupDrainECSHookFunctionTopicE6B1EBA6": {
"Type": "AWS::Lambda::Permission",
"EcsClusterDefaultAutoScalingGroupDrainECSHookFunctionTopic8F34E394": {
"Type": "AWS::SNS::Subscription",
"Properties": {
"Action": "lambda:InvokeFunction",
"FunctionName": {
"Protocol": "lambda",
"TopicArn": {
"Ref": "EcsClusterDefaultAutoScalingGroupLifecycleHookDrainHookTopicACD2D4A4"
},
"Endpoint": {
"Fn::GetAtt": [
"EcsClusterDefaultAutoScalingGroupDrainECSHookFunctionE17A5F5E",
"Arn"
]
},
"Principal": "sns.amazonaws.com",
"SourceArn": {
"Ref": "EcsClusterDefaultAutoScalingGroupLifecycleHookDrainHookTopicACD2D4A4"
}
}
},
Expand Down Expand Up @@ -700,6 +700,10 @@
"Volumes": []
}
},
"ScheduledEc2TaskScheduledTaskDefScheduledContainerLogGroupA85E11E6": {
"Type": "AWS::Logs::LogGroup",
"DeletionPolicy": "Retain"
},
"ScheduledEc2TaskScheduledTaskDefExecutionRole65A8CC6F": {
"Type": "AWS::IAM::Role",
"Properties": {
Expand Down Expand Up @@ -826,10 +830,6 @@
]
}
},
"ScheduledEc2TaskScheduledTaskDefScheduledContainerLogGroupA85E11E6": {
"Type": "AWS::Logs::LogGroup",
"DeletionPolicy": "Retain"
},
"ScheduledEc2TaskScheduledEventRuleFE2376A2": {
"Type": "AWS::Events::Rule",
"Properties": {
Expand Down Expand Up @@ -868,4 +868,4 @@
"Default": "/aws/service/ecs/optimized-ami/amazon-linux-2/recommended/image_id"
}
}
}
}
48 changes: 24 additions & 24 deletions packages/@aws-cdk/aws-ecs/test/ec2/integ.lb-awsvpc-nw.expected.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
{
"Parameters": {
"SsmParameterValueawsserviceecsoptimizedamiamazonlinux2recommendedimageidC96584B6F00A464EAD1953AFF4B05118Parameter": {
"Type": "AWS::SSM::Parameter::Value<String>",
"Default": "/aws/service/ecs/optimized-ami/amazon-linux-2/recommended/image_id"
}
},
"Resources": {
"Vpc8378EB38": {
"Type": "AWS::EC2::VPC",
Expand Down Expand Up @@ -516,9 +510,6 @@
}
}
},
"EcsClusterDefaultAutoScalingGroupLifecycleHookDrainHookTopicACD2D4A4": {
"Type": "AWS::SNS::Topic"
},
"EcsClusterDefaultAutoScalingGroupDrainECSHookFunctionServiceRole94543EDA": {
"Type": "AWS::IAM::Role",
"Properties": {
Expand Down Expand Up @@ -671,34 +662,34 @@
"EcsClusterDefaultAutoScalingGroupDrainECSHookFunctionServiceRole94543EDA"
]
},
"EcsClusterDefaultAutoScalingGroupLifecycleHookDrainHookTopicFunctionA8966A35": {
"Type": "AWS::SNS::Subscription",
"EcsClusterDefaultAutoScalingGroupDrainECSHookFunctionAllowInvokeawsecsintegEcsClusterDefaultAutoScalingGroupLifecycleHookDrainHookTopic7A89925AFDCBEE50": {
"Type": "AWS::Lambda::Permission",
"Properties": {
"Protocol": "lambda",
"TopicArn": {
"Ref": "EcsClusterDefaultAutoScalingGroupLifecycleHookDrainHookTopicACD2D4A4"
},
"Endpoint": {
"Action": "lambda:InvokeFunction",
"FunctionName": {
"Fn::GetAtt": [
"EcsClusterDefaultAutoScalingGroupDrainECSHookFunctionE17A5F5E",
"Arn"
]
},
"Principal": "sns.amazonaws.com",
"SourceArn": {
"Ref": "EcsClusterDefaultAutoScalingGroupLifecycleHookDrainHookTopicACD2D4A4"
}
}
},
"EcsClusterDefaultAutoScalingGroupDrainECSHookFunctionTopicE6B1EBA6": {
"Type": "AWS::Lambda::Permission",
"EcsClusterDefaultAutoScalingGroupDrainECSHookFunctionTopic8F34E394": {
"Type": "AWS::SNS::Subscription",
"Properties": {
"Action": "lambda:InvokeFunction",
"FunctionName": {
"Protocol": "lambda",
"TopicArn": {
"Ref": "EcsClusterDefaultAutoScalingGroupLifecycleHookDrainHookTopicACD2D4A4"
},
"Endpoint": {
"Fn::GetAtt": [
"EcsClusterDefaultAutoScalingGroupDrainECSHookFunctionE17A5F5E",
"Arn"
]
},
"Principal": "sns.amazonaws.com",
"SourceArn": {
"Ref": "EcsClusterDefaultAutoScalingGroupLifecycleHookDrainHookTopicACD2D4A4"
}
}
},
Expand Down Expand Up @@ -752,6 +743,9 @@
]
}
},
"EcsClusterDefaultAutoScalingGroupLifecycleHookDrainHookTopicACD2D4A4": {
"Type": "AWS::SNS::Topic"
},
"EcsClusterDefaultAutoScalingGroupLifecycleHookDrainHookFFA63029": {
"Type": "AWS::AutoScaling::LifecycleHook",
"Properties": {
Expand Down Expand Up @@ -1031,6 +1025,12 @@
}
}
},
"Parameters": {
"SsmParameterValueawsserviceecsoptimizedamiamazonlinux2recommendedimageidC96584B6F00A464EAD1953AFF4B05118Parameter": {
"Type": "AWS::SSM::Parameter::Value<String>",
"Default": "/aws/service/ecs/optimized-ami/amazon-linux-2/recommended/image_id"
}
},
"Outputs": {
"LoadBalancerDNS": {
"Value": {
Expand Down
48 changes: 24 additions & 24 deletions packages/@aws-cdk/aws-ecs/test/ec2/integ.lb-bridge-nw.expected.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
{
"Parameters": {
"SsmParameterValueawsserviceecsoptimizedamiamazonlinux2recommendedimageidC96584B6F00A464EAD1953AFF4B05118Parameter": {
"Type": "AWS::SSM::Parameter::Value<String>",
"Default": "/aws/service/ecs/optimized-ami/amazon-linux-2/recommended/image_id"
}
},
"Resources": {
"Vpc8378EB38": {
"Type": "AWS::EC2::VPC",
Expand Down Expand Up @@ -537,9 +531,6 @@
}
}
},
"EcsClusterDefaultAutoScalingGroupLifecycleHookDrainHookTopicACD2D4A4": {
"Type": "AWS::SNS::Topic"
},
"EcsClusterDefaultAutoScalingGroupDrainECSHookFunctionServiceRole94543EDA": {
"Type": "AWS::IAM::Role",
"Properties": {
Expand Down Expand Up @@ -692,34 +683,34 @@
"EcsClusterDefaultAutoScalingGroupDrainECSHookFunctionServiceRole94543EDA"
]
},
"EcsClusterDefaultAutoScalingGroupLifecycleHookDrainHookTopicFunctionA8966A35": {
"Type": "AWS::SNS::Subscription",
"EcsClusterDefaultAutoScalingGroupDrainECSHookFunctionAllowInvokeawsecsintegecsEcsClusterDefaultAutoScalingGroupLifecycleHookDrainHookTopic0C4958AFBA77E328": {
"Type": "AWS::Lambda::Permission",
"Properties": {
"Protocol": "lambda",
"TopicArn": {
"Ref": "EcsClusterDefaultAutoScalingGroupLifecycleHookDrainHookTopicACD2D4A4"
},
"Endpoint": {
"Action": "lambda:InvokeFunction",
"FunctionName": {
"Fn::GetAtt": [
"EcsClusterDefaultAutoScalingGroupDrainECSHookFunctionE17A5F5E",
"Arn"
]
},
"Principal": "sns.amazonaws.com",
"SourceArn": {
"Ref": "EcsClusterDefaultAutoScalingGroupLifecycleHookDrainHookTopicACD2D4A4"
}
}
},
"EcsClusterDefaultAutoScalingGroupDrainECSHookFunctionTopicE6B1EBA6": {
"Type": "AWS::Lambda::Permission",
"EcsClusterDefaultAutoScalingGroupDrainECSHookFunctionTopic8F34E394": {
"Type": "AWS::SNS::Subscription",
"Properties": {
"Action": "lambda:InvokeFunction",
"FunctionName": {
"Protocol": "lambda",
"TopicArn": {
"Ref": "EcsClusterDefaultAutoScalingGroupLifecycleHookDrainHookTopicACD2D4A4"
},
"Endpoint": {
"Fn::GetAtt": [
"EcsClusterDefaultAutoScalingGroupDrainECSHookFunctionE17A5F5E",
"Arn"
]
},
"Principal": "sns.amazonaws.com",
"SourceArn": {
"Ref": "EcsClusterDefaultAutoScalingGroupLifecycleHookDrainHookTopicACD2D4A4"
}
}
},
Expand Down Expand Up @@ -773,6 +764,9 @@
]
}
},
"EcsClusterDefaultAutoScalingGroupLifecycleHookDrainHookTopicACD2D4A4": {
"Type": "AWS::SNS::Topic"
},
"EcsClusterDefaultAutoScalingGroupLifecycleHookDrainHookFFA63029": {
"Type": "AWS::AutoScaling::LifecycleHook",
"Properties": {
Expand Down Expand Up @@ -994,6 +988,12 @@
}
}
},
"Parameters": {
"SsmParameterValueawsserviceecsoptimizedamiamazonlinux2recommendedimageidC96584B6F00A464EAD1953AFF4B05118Parameter": {
"Type": "AWS::SSM::Parameter::Value<String>",
"Default": "/aws/service/ecs/optimized-ami/amazon-linux-2/recommended/image_id"
}
},
"Outputs": {
"LoadBalancerDNS": {
"Value": {
Expand Down
50 changes: 25 additions & 25 deletions packages/@aws-cdk/aws-ecs/test/ec2/integ.sd-awsvpc-nw.expected.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
{
"Parameters": {
"SsmParameterValueawsserviceecsoptimizedamiamazonlinux2recommendedimageidC96584B6F00A464EAD1953AFF4B05118Parameter": {
"Type": "AWS::SSM::Parameter::Value<String>",
"Default": "/aws/service/ecs/optimized-ami/amazon-linux-2/recommended/image_id"
}
},
"Resources": {
"Vpc8378EB38": {
"Type": "AWS::EC2::VPC",
Expand Down Expand Up @@ -516,9 +510,6 @@
}
}
},
"EcsClusterDefaultAutoScalingGroupLifecycleHookDrainHookTopicACD2D4A4": {
"Type": "AWS::SNS::Topic"
},
"EcsClusterDefaultAutoScalingGroupDrainECSHookFunctionServiceRole94543EDA": {
"Type": "AWS::IAM::Role",
"Properties": {
Expand Down Expand Up @@ -671,34 +662,34 @@
"EcsClusterDefaultAutoScalingGroupDrainECSHookFunctionServiceRole94543EDA"
]
},
"EcsClusterDefaultAutoScalingGroupLifecycleHookDrainHookTopicFunctionA8966A35": {
"Type": "AWS::SNS::Subscription",
"EcsClusterDefaultAutoScalingGroupDrainECSHookFunctionAllowInvokeawsecsintegecsEcsClusterDefaultAutoScalingGroupLifecycleHookDrainHookTopic0C4958AFBA77E328": {
"Type": "AWS::Lambda::Permission",
"Properties": {
"Protocol": "lambda",
"TopicArn": {
"Ref": "EcsClusterDefaultAutoScalingGroupLifecycleHookDrainHookTopicACD2D4A4"
},
"Endpoint": {
"Action": "lambda:InvokeFunction",
"FunctionName": {
"Fn::GetAtt": [
"EcsClusterDefaultAutoScalingGroupDrainECSHookFunctionE17A5F5E",
"Arn"
]
},
"Principal": "sns.amazonaws.com",
"SourceArn": {
"Ref": "EcsClusterDefaultAutoScalingGroupLifecycleHookDrainHookTopicACD2D4A4"
}
}
},
"EcsClusterDefaultAutoScalingGroupDrainECSHookFunctionTopicE6B1EBA6": {
"Type": "AWS::Lambda::Permission",
"EcsClusterDefaultAutoScalingGroupDrainECSHookFunctionTopic8F34E394": {
"Type": "AWS::SNS::Subscription",
"Properties": {
"Action": "lambda:InvokeFunction",
"FunctionName": {
"Protocol": "lambda",
"TopicArn": {
"Ref": "EcsClusterDefaultAutoScalingGroupLifecycleHookDrainHookTopicACD2D4A4"
},
"Endpoint": {
"Fn::GetAtt": [
"EcsClusterDefaultAutoScalingGroupDrainECSHookFunctionE17A5F5E",
"Arn"
]
},
"Principal": "sns.amazonaws.com",
"SourceArn": {
"Ref": "EcsClusterDefaultAutoScalingGroupLifecycleHookDrainHookTopicACD2D4A4"
}
}
},
Expand Down Expand Up @@ -752,6 +743,9 @@
]
}
},
"EcsClusterDefaultAutoScalingGroupLifecycleHookDrainHookTopicACD2D4A4": {
"Type": "AWS::SNS::Topic"
},
"EcsClusterDefaultAutoScalingGroupLifecycleHookDrainHookFFA63029": {
"Type": "AWS::AutoScaling::LifecycleHook",
"Properties": {
Expand Down Expand Up @@ -945,5 +939,11 @@
}
}
}
},
"Parameters": {
"SsmParameterValueawsserviceecsoptimizedamiamazonlinux2recommendedimageidC96584B6F00A464EAD1953AFF4B05118Parameter": {
"Type": "AWS::SSM::Parameter::Value<String>",
"Default": "/aws/service/ecs/optimized-ami/amazon-linux-2/recommended/image_id"
}
}
}
}
Loading

0 comments on commit 64a203f

Please sign in to comment.