Skip to content

Commit

Permalink
Merge pull request #17610 from grahamhar/master
Browse files Browse the repository at this point in the history
  • Loading branch information
gdavison committed Jun 4, 2021
2 parents 3ab0d24 + 5fa4a7a commit 8beaad3
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .changelog/17610.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_lambda_function: Prevents perpetual diff in `vpc_config`
```
5 changes: 3 additions & 2 deletions aws/resource_aws_lambda_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,8 @@ func hasConfigChanges(d resourceDiffer) bool {
d.HasChange("layers") ||
d.HasChange("dead_letter_config") ||
d.HasChange("tracing_config") ||
d.HasChange("vpc_config") ||
d.HasChange("vpc_config.0.security_group_ids") ||
d.HasChange("vpc_config.0.subnet_ids") ||
d.HasChange("runtime") ||
d.HasChange("environment")
}
Expand Down Expand Up @@ -1009,7 +1010,7 @@ func resourceAwsLambdaFunctionUpdate(d *schema.ResourceData, meta interface{}) e
}
}
}
if d.HasChange("vpc_config") {
if d.HasChanges("vpc_config.0.security_group_ids", "vpc_config.0.subnet_ids") {
configReq.VpcConfig = &lambda.VpcConfig{
SecurityGroupIds: []*string{},
SubnetIds: []*string{},
Expand Down
118 changes: 118 additions & 0 deletions aws/resource_aws_lambda_function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,90 @@ func TestAccAWSLambdaFunction_VPC_withInvocation(t *testing.T) {
})
}

// See https://github.com/hashicorp/terraform-provider-aws/issues/17385
// When the vpc config doesn't change the version shouldn't change
func TestAccAWSLambdaFunction_VPC_publish_No_Changes(t *testing.T) {
var conf lambda.GetFunctionOutput

rString := acctest.RandString(8)
funcName := fmt.Sprintf("tf_acc_lambda_func_vpc_w_invc_%s", rString)
policyName := fmt.Sprintf("tf_acc_policy_lambda_func_vpc_w_invc_%s", rString)
roleName := fmt.Sprintf("tf_acc_role_lambda_func_vpc_w_invc_%s", rString)
sgName := fmt.Sprintf("tf_acc_sg_lambda_func_vpc_w_invc_%s", rString)
resourceName := "aws_lambda_function.test"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, lambda.EndpointsID),
Providers: testAccProviders,
CheckDestroy: testAccCheckLambdaFunctionDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSLambdaConfigWithVPCPublish(funcName, policyName, roleName, sgName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsLambdaFunctionExists(resourceName, funcName, &conf),
resource.TestCheckResourceAttr(resourceName, "version", "1"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"filename", "publish"},
},
{
Config: testAccAWSLambdaConfigWithVPCPublish(funcName, policyName, roleName, sgName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsLambdaFunctionExists(resourceName, funcName, &conf),
resource.TestCheckResourceAttr(resourceName, "version", "1"),
),
},
},
})
}

// See https://github.com/hashicorp/terraform-provider-aws/issues/17385
// When the vpc config changes the version should change
func TestAccAWSLambdaFunction_VPC_publish_Has_Changes(t *testing.T) {
var conf lambda.GetFunctionOutput

rString := acctest.RandString(8)
funcName := fmt.Sprintf("tf_acc_lambda_func_vpc_w_invc_%s", rString)
policyName := fmt.Sprintf("tf_acc_policy_lambda_func_vpc_w_invc_%s", rString)
roleName := fmt.Sprintf("tf_acc_role_lambda_func_vpc_w_invc_%s", rString)
sgName := fmt.Sprintf("tf_acc_sg_lambda_func_vpc_w_invc_%s", rString)
resourceName := "aws_lambda_function.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, lambda.EndpointsID),
Providers: testAccProviders,
CheckDestroy: testAccCheckLambdaFunctionDestroy,

Steps: []resource.TestStep{
{
Config: testAccAWSLambdaConfigWithVPCPublish(funcName, policyName, roleName, sgName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsLambdaFunctionExists(resourceName, funcName, &conf),
resource.TestCheckResourceAttr(resourceName, "version", "1"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"filename", "publish"},
},
{
Config: testAccAWSLambdaConfigWithVPCUpdatedPublish(funcName, policyName, roleName, sgName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsLambdaFunctionExists(resourceName, funcName, &conf),
resource.TestCheckResourceAttr(resourceName, "version", "2"),
),
},
},
})
}

// Reference: https://github.com/hashicorp/terraform-provider-aws/issues/10044
func TestAccAWSLambdaFunction_VpcConfig_ProperIamDependencies(t *testing.T) {
var function lambda.GetFunctionOutput
Expand Down Expand Up @@ -2875,6 +2959,40 @@ resource "aws_lambda_function" "test" {
`, funcName)
}

func testAccAWSLambdaConfigWithVPCPublish(funcName, policyName, roleName, sgName string) string {
return fmt.Sprintf(baseAccAWSLambdaConfig(policyName, roleName, sgName)+`
resource "aws_lambda_function" "test" {
filename = "test-fixtures/lambdatest.zip"
function_name = "%s"
role = aws_iam_role.iam_for_lambda.arn
handler = "exports.example"
runtime = "nodejs12.x"
publish = true
vpc_config {
subnet_ids = [aws_subnet.subnet_for_lambda.id]
security_group_ids = [aws_security_group.sg_for_lambda.id]
}
}
`, funcName)
}

func testAccAWSLambdaConfigWithVPCUpdatedPublish(funcName, policyName, roleName, sgName string) string {
return fmt.Sprintf(baseAccAWSLambdaConfig(policyName, roleName, sgName)+`
resource "aws_lambda_function" "test" {
filename = "test-fixtures/lambdatest.zip"
function_name = "%s"
role = aws_iam_role.iam_for_lambda.arn
handler = "exports.example"
runtime = "nodejs12.x"
publish = true
vpc_config {
security_group_ids = []
subnet_ids = []
}
}
`, funcName)
}

func testAccAWSLambdaConfigWithVPCUpdated(funcName, policyName, roleName, sgName, sgName2 string) string {
return fmt.Sprintf(baseAccAWSLambdaConfig(policyName, roleName, sgName)+`
resource "aws_lambda_function" "test" {
Expand Down

0 comments on commit 8beaad3

Please sign in to comment.