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

resource/aws_lambda_function: Ignore the VPC configuration if it is empty #1341

Merged
merged 1 commit into from
Sep 6, 2018
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
26 changes: 25 additions & 1 deletion aws/resource_aws_lambda_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,31 @@ func resourceAwsLambdaFunction() *schema.Resource {
},
},
},

// Suppress diffs if the VPC configuration is empty.
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if v, ok := d.GetOk("vpc_config"); ok {
configs := v.([]interface{})
config, ok := configs[0].(map[string]interface{})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @joshuaspence for submitting this with an acceptance test! This was definitely on the right track.

One note here: to prevent potential panics, we should ensure the length of configs before trying to reference the first element, e.g.

if v, ok := d.GetOk("vpc_config"); ok && len(v.([]interface{})) > 0 {

Style nitpick: Also, we can remove the nesting if return early:

if old != "0" && new != "1" {
  return false
}
v, ok := d.GetOk("vpc_config")
if !ok || len(v.([]interface{})) == 0 {
  return false
}
// other logic :)

I'll be rebasing this PR and applying a followup commit so we can get this released today, rather then making you do any additional work or having this linger further. 👍


if !ok {
return true
}

if config == nil {
return true
}

securityGroups := config["security_group_ids"].(*schema.Set)
subnets := config["subnet_ids"].(*schema.Set)

if securityGroups.Len() == 0 && subnets.Len() == 0 {
return true
}
}

return false
},
},
"arn": {
Type: schema.TypeString,
Expand Down Expand Up @@ -291,7 +316,6 @@ func resourceAwsLambdaFunctionCreate(d *schema.ResourceData, meta interface{}) e
}

if v, ok := d.GetOk("vpc_config"); ok {

configs := v.([]interface{})
config, ok := configs[0].(map[string]interface{})

Expand Down
41 changes: 41 additions & 0 deletions aws/resource_aws_lambda_function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,31 @@ func TestAccAWSLambdaFunction_VPC_withInvocation(t *testing.T) {
})
}

func TestAccAWSLambdaFunction_EmptyVpcConfig(t *testing.T) {
var conf lambda.GetFunctionOutput

rString := acctest.RandString(8)
funcName := fmt.Sprintf("tf_acc_lambda_func_empty_vpc_config_%s", rString)
policyName := fmt.Sprintf("tf_acc_policy_lambda_func_empty_vpc_config_%s", rString)
roleName := fmt.Sprintf("tf_acc_role_lambda_func_empty_vpc_config_%s", rString)
sgName := fmt.Sprintf("tf_acc_sg_lambda_func_empty_vpc_config_%s", rString)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLambdaFunctionDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSLambdaConfigWithEmptyVpcConfig(funcName, policyName, roleName, sgName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsLambdaFunctionExists("aws_lambda_function.test", funcName, &conf),
resource.TestCheckResourceAttr("aws_lambda_function.test", "vpc_config.#", "0"),
),
},
},
})
}

func TestAccAWSLambdaFunction_s3(t *testing.T) {
var conf lambda.GetFunctionOutput

Expand Down Expand Up @@ -1668,6 +1693,22 @@ resource "aws_security_group" "sg_for_lambda_2" {
`, funcName, sgName2)
}

func testAccAWSLambdaConfigWithEmptyVpcConfig(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 = "nodejs4.3"

vpc_config {
subnet_ids = []
security_group_ids = []
}
}`, funcName)
}

func testAccAWSLambdaConfigS3(bucketName, roleName, funcName string) string {
return fmt.Sprintf(`
resource "aws_s3_bucket" "lambda_bucket" {
Expand Down