@@ -15,6 +15,7 @@ import (
1515 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1616 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/structure"
1717 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
18+ "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/sns/finder"
1819 "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/sns/waiter"
1920)
2021
@@ -104,13 +105,15 @@ func resourceAwsSnsTopicSubscription() *schema.Resource {
104105 DiffSuppressFunc : suppressEquivalentJsonDiffs ,
105106 },
106107 "subscription_role_arn" : {
107- Type : schema .TypeString ,
108- Optional : true ,
108+ Type : schema .TypeString ,
109+ Optional : true ,
110+ ValidateFunc : validateArn ,
109111 },
110112 "topic_arn" : {
111- Type : schema .TypeString ,
112- Required : true ,
113- ForceNew : true ,
113+ Type : schema .TypeString ,
114+ Required : true ,
115+ ForceNew : true ,
116+ ValidateFunc : validateArn ,
114117 },
115118 },
116119 }
@@ -168,22 +171,27 @@ func resourceAwsSnsTopicSubscriptionRead(d *schema.ResourceData, meta interface{
168171
169172 log .Printf ("[DEBUG] Loading subscription %s" , d .Id ())
170173
171- output , err := conn . GetSubscriptionAttributes ( & sns.GetSubscriptionAttributesInput {
172- SubscriptionArn : aws .String (d .Id ( )),
173- })
174+ input := & sns.ListSubscriptionsByTopicInput {
175+ TopicArn : aws .String (d .Get ( "topic_arn" ).( string )),
176+ }
174177
175- if ! d .IsNewResource () && (tfawserr .ErrCodeEquals (err , sns .ErrCodeResourceNotFoundException ) || tfawserr .ErrCodeEquals (err , sns .ErrCodeNotFoundException )) {
176- log .Printf ("[WARN] SNS subscription attributes (%s) not found, removing from state" , d .Id ())
178+ _ , err := conn .ListSubscriptionsByTopic (input )
179+
180+ if ! d .IsNewResource () && tfawserr .ErrCodeEquals (err , sns .ErrCodeNotFoundException ) {
181+ log .Printf ("[WARN] SNS Topic Subscription (%s) not found, removing from state" , d .Id ())
177182 d .SetId ("" )
178183 return nil
179184 }
180185
186+ output , err := finder .SubscriptionByARN (conn , d .Id ())
181187 if err != nil {
182188 return fmt .Errorf ("getting SNS subscription attributes (%s): %w" , d .Id (), err )
183189 }
184190
185- if output == nil || output .Attributes == nil || len (output .Attributes ) == 0 {
186- return fmt .Errorf ("getting SNS subscription attributes (%s): empty response" , d .Id ())
191+ if output == nil {
192+ log .Printf ("[WARN] SNS subscription attributes (%s) not found, removing from state" , d .Id ())
193+ d .SetId ("" )
194+ return nil
187195 }
188196
189197 attributes := output .Attributes
@@ -282,6 +290,13 @@ func resourceAwsSnsTopicSubscriptionDelete(d *schema.ResourceData, meta interfac
282290 return nil
283291 }
284292
293+ if _ , err := waiter .SubscriptionDeleted (conn , d .Id ()); err != nil {
294+ if tfawserr .ErrCodeEquals (err , sns .ErrCodeNotFoundException ) {
295+ return nil
296+ }
297+ return fmt .Errorf ("error waiting for SNS topic subscription (%s) deletion: %w" , d .Id (), err )
298+ }
299+
285300 return err
286301}
287302
0 commit comments