@@ -448,9 +448,11 @@ func resourceAwsSecurityGroupDelete(d *schema.ResourceData, meta interface{}) er
448
448
conn := meta .(* AWSClient ).ec2conn
449
449
450
450
log .Printf ("[DEBUG] Security Group destroy: %v" , d .Id ())
451
-
452
- if err := deleteLingeringLambdaENIs (conn , "group-id" , d .Id (), d .Timeout (schema .TimeoutDelete )); err != nil {
451
+ lingeringLambdaExists := false
452
+ if lambdaExists , err := deleteLingeringLambdaENIs (conn , "group-id" , d .Id (), d .Timeout (schema .TimeoutDelete )); err != nil {
453
453
return fmt .Errorf ("error deleting Lambda ENIs using Security Group (%s): %s" , d .Id (), err )
454
+ } else {
455
+ lingeringLambdaExists = lambdaExists
454
456
}
455
457
456
458
// conditionally revoke rules first before attempting to delete the group
@@ -468,7 +470,7 @@ func resourceAwsSecurityGroupDelete(d *schema.ResourceData, meta interface{}) er
468
470
if isAWSErr (err , "InvalidGroup.NotFound" , "" ) {
469
471
return nil
470
472
}
471
- if isAWSErr (err , "DependencyViolation" , "" ) {
473
+ if lingeringLambdaExists && isAWSErr (err , "DependencyViolation" , "" ) {
472
474
// If it is a dependency violation, we want to retry
473
475
return resource .RetryableError (err )
474
476
}
@@ -1403,12 +1405,14 @@ func sgProtocolIntegers() map[string]int {
1403
1405
1404
1406
// The AWS Lambda service creates ENIs behind the scenes and keeps these around for a while
1405
1407
// which would prevent SGs attached to such ENIs from being destroyed
1406
- func deleteLingeringLambdaENIs (conn * ec2.EC2 , filterName , resourceId string , timeout time.Duration ) error {
1408
+ func deleteLingeringLambdaENIs (conn * ec2.EC2 , filterName , resourceId string , timeout time.Duration ) ( bool , error ) {
1407
1409
// AWS Lambda service team confirms P99 deletion time of ~35 minutes. Buffer for safety.
1408
1410
if minimumTimeout := 45 * time .Minute ; timeout < minimumTimeout {
1409
1411
timeout = minimumTimeout
1410
1412
}
1411
1413
1414
+ lambdaExists := false
1415
+
1412
1416
resp , err := conn .DescribeNetworkInterfaces (& ec2.DescribeNetworkInterfacesInput {
1413
1417
Filters : buildEC2AttributeFilterList (map [string ]string {
1414
1418
filterName : resourceId ,
@@ -1417,7 +1421,11 @@ func deleteLingeringLambdaENIs(conn *ec2.EC2, filterName, resourceId string, tim
1417
1421
})
1418
1422
1419
1423
if err != nil {
1420
- return fmt .Errorf ("error describing ENIs: %s" , err )
1424
+ return lambdaExists , fmt .Errorf ("error describing ENIs: %s" , err )
1425
+ }
1426
+
1427
+ if len (resp .NetworkInterfaces ) > 0 {
1428
+ lambdaExists = true
1421
1429
}
1422
1430
1423
1431
for _ , eni := range resp .NetworkInterfaces {
@@ -1449,7 +1457,7 @@ func deleteLingeringLambdaENIs(conn *ec2.EC2, filterName, resourceId string, tim
1449
1457
}
1450
1458
1451
1459
if err != nil {
1452
- return fmt .Errorf ("error waiting for Lambda V2N ENI (%s) to become available for detachment: %s" , eniId , err )
1460
+ return lambdaExists , fmt .Errorf ("error waiting for Lambda V2N ENI (%s) to become available for detachment: %s" , eniId , err )
1453
1461
}
1454
1462
1455
1463
eni = eniRaw .(* ec2.NetworkInterface )
@@ -1458,17 +1466,17 @@ func deleteLingeringLambdaENIs(conn *ec2.EC2, filterName, resourceId string, tim
1458
1466
err = detachNetworkInterface (conn , eni , timeout )
1459
1467
1460
1468
if err != nil {
1461
- return fmt .Errorf ("error detaching Lambda ENI (%s): %s" , eniId , err )
1469
+ return lambdaExists , fmt .Errorf ("error detaching Lambda ENI (%s): %s" , eniId , err )
1462
1470
}
1463
1471
1464
1472
err = deleteNetworkInterface (conn , eniId )
1465
1473
1466
1474
if err != nil {
1467
- return fmt .Errorf ("error deleting Lambda ENI (%s): %s" , eniId , err )
1475
+ return lambdaExists , fmt .Errorf ("error deleting Lambda ENI (%s): %s" , eniId , err )
1468
1476
}
1469
1477
}
1470
1478
1471
- return nil
1479
+ return lambdaExists , nil
1472
1480
}
1473
1481
1474
1482
func initSecurityGroupRule (ruleMap map [string ]map [string ]interface {}, perm * ec2.IpPermission , desc string ) map [string ]interface {} {
0 commit comments