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

Fix aws_lambda_event_source_mapping function name flapping #5454

Merged
merged 2 commits into from
Aug 29, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions aws/resource_aws_lambda_event_source_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ func resourceAwsLambdaEventSourceMapping() *schema.Resource {
"function_name": {
Type: schema.TypeString,
Required: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
// Using function name or ARN should not be shown as a diff.
// Try to convert the old and new values from ARN to function name
oldFunctionName, oldFunctionNameErr := getFunctionNameFromLambdaArn(old)
newFunctionName, newFunctionNameErr := getFunctionNameFromLambdaArn(old)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be getFunctionNameFromLambdaArn(new)?

return (oldFunctionName == new && oldFunctionNameErr == nil) || (newFunctionName == old && newFunctionNameErr == nil)
},
},
"starting_position": {
Type: schema.TypeString,
Expand Down
154 changes: 125 additions & 29 deletions aws/resource_aws_lambda_event_source_mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
func TestAccAWSLambdaEventSourceMapping_kinesis_basic(t *testing.T) {
var conf lambda.EventSourceMappingConfiguration

resourceName := "aws_lambda_event_source_mapping.lambda_event_source_mapping_test"

rString := acctest.RandString(8)
roleName := fmt.Sprintf("tf_acc_role_lambda_esm_basic_%s", rString)
policyName := fmt.Sprintf("tf_acc_policy_lambda_esm_basic_%s", rString)
Expand All @@ -35,22 +37,18 @@ func TestAccAWSLambdaEventSourceMapping_kinesis_basic(t *testing.T) {
{
Config: testAccAWSLambdaEventSourceMappingConfig_kinesis(roleName, policyName, attName, streamName, funcName, uFuncName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsLambdaEventSourceMappingExists("aws_lambda_event_source_mapping.lambda_event_source_mapping_test", &conf),
testAccCheckAwsLambdaEventSourceMappingExists(resourceName, &conf),
testAccCheckAWSLambdaEventSourceMappingAttributes(&conf),
),
},
{
Config: testAccAWSLambdaEventSourceMappingConfigUpdate_kinesis(roleName, policyName, attName, streamName, funcName, uFuncName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsLambdaEventSourceMappingExists("aws_lambda_event_source_mapping.lambda_event_source_mapping_test", &conf),
resource.TestCheckResourceAttr("aws_lambda_event_source_mapping.lambda_event_source_mapping_test",
"batch_size", strconv.Itoa(200)),
resource.TestCheckResourceAttr("aws_lambda_event_source_mapping.lambda_event_source_mapping_test",
"enabled", strconv.FormatBool(false)),
resource.TestMatchResourceAttr("aws_lambda_event_source_mapping.lambda_event_source_mapping_test",
"function_arn", uFuncArnRe),
resource.TestCheckResourceAttr("aws_lambda_event_source_mapping.lambda_event_source_mapping_test",
"starting_position", "TRIM_HORIZON"),
testAccCheckAwsLambdaEventSourceMappingExists(resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "batch_size", strconv.Itoa(200)),
resource.TestCheckResourceAttr(resourceName, "enabled", strconv.FormatBool(false)),
resource.TestMatchResourceAttr(resourceName, "function_arn", uFuncArnRe),
resource.TestCheckResourceAttr(resourceName, "starting_position", "TRIM_HORIZON"),
),
},
},
Expand All @@ -64,6 +62,8 @@ func TestAccAWSLambdaEventSourceMapping_kinesis_removeBatchSize(t *testing.T) {

var conf lambda.EventSourceMappingConfiguration

resourceName := "aws_lambda_event_source_mapping.lambda_event_source_mapping_test"

rString := acctest.RandString(8)
roleName := fmt.Sprintf("tf_acc_role_lambda_esm_basic_%s", rString)
policyName := fmt.Sprintf("tf_acc_policy_lambda_esm_basic_%s", rString)
Expand All @@ -80,20 +80,17 @@ func TestAccAWSLambdaEventSourceMapping_kinesis_removeBatchSize(t *testing.T) {
{
Config: testAccAWSLambdaEventSourceMappingConfig_kinesis(roleName, policyName, attName, streamName, funcName, uFuncName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsLambdaEventSourceMappingExists("aws_lambda_event_source_mapping.lambda_event_source_mapping_test", &conf),
testAccCheckAwsLambdaEventSourceMappingExists(resourceName, &conf),
testAccCheckAWSLambdaEventSourceMappingAttributes(&conf),
),
},
{
Config: testAccAWSLambdaEventSourceMappingConfigUpdate_kinesis_removeBatchSize(roleName, policyName, attName, streamName, funcName, uFuncName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsLambdaEventSourceMappingExists("aws_lambda_event_source_mapping.lambda_event_source_mapping_test", &conf),
resource.TestCheckResourceAttr("aws_lambda_event_source_mapping.lambda_event_source_mapping_test",
"batch_size", strconv.Itoa(100)),
resource.TestCheckResourceAttr("aws_lambda_event_source_mapping.lambda_event_source_mapping_test",
"enabled", strconv.FormatBool(true)),
resource.TestCheckResourceAttr("aws_lambda_event_source_mapping.lambda_event_source_mapping_test",
"starting_position", "TRIM_HORIZON"),
testAccCheckAwsLambdaEventSourceMappingExists(resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "batch_size", strconv.Itoa(100)),
resource.TestCheckResourceAttr(resourceName, "enabled", strconv.FormatBool(true)),
resource.TestCheckResourceAttr(resourceName, "starting_position", "TRIM_HORIZON"),
),
},
},
Expand All @@ -103,6 +100,8 @@ func TestAccAWSLambdaEventSourceMapping_kinesis_removeBatchSize(t *testing.T) {
func TestAccAWSLambdaEventSourceMapping_sqs_basic(t *testing.T) {
var conf lambda.EventSourceMappingConfiguration

resourceName := "aws_lambda_event_source_mapping.lambda_event_source_mapping_test"

rString := acctest.RandString(8)
roleName := fmt.Sprintf("tf_acc_role_lambda_sqs_basic_%s", rString)
policyName := fmt.Sprintf("tf_acc_policy_lambda_sqs_basic_%s", rString)
Expand All @@ -120,24 +119,52 @@ func TestAccAWSLambdaEventSourceMapping_sqs_basic(t *testing.T) {
{
Config: testAccAWSLambdaEventSourceMappingConfig_sqs(roleName, policyName, attName, streamName, funcName, uFuncName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsLambdaEventSourceMappingExists("aws_lambda_event_source_mapping.lambda_event_source_mapping_test", &conf),
testAccCheckAwsLambdaEventSourceMappingExists(resourceName, &conf),
testAccCheckAWSLambdaEventSourceMappingAttributes(&conf),
resource.TestCheckNoResourceAttr("aws_lambda_event_source_mapping.lambda_event_source_mapping_test",
resource.TestCheckNoResourceAttr(resourceName,
"starting_position"),
),
},
{
Config: testAccAWSLambdaEventSourceMappingConfigUpdate_sqs(roleName, policyName, attName, streamName, funcName, uFuncName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsLambdaEventSourceMappingExists("aws_lambda_event_source_mapping.lambda_event_source_mapping_test", &conf),
resource.TestCheckResourceAttr("aws_lambda_event_source_mapping.lambda_event_source_mapping_test",
"batch_size", strconv.Itoa(5)),
resource.TestCheckResourceAttr("aws_lambda_event_source_mapping.lambda_event_source_mapping_test",
"enabled", strconv.FormatBool(false)),
resource.TestMatchResourceAttr("aws_lambda_event_source_mapping.lambda_event_source_mapping_test",
"function_arn", uFuncArnRe),
resource.TestCheckNoResourceAttr("aws_lambda_event_source_mapping.lambda_event_source_mapping_test",
"starting_position"),
testAccCheckAwsLambdaEventSourceMappingExists(resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "batch_size", strconv.Itoa(5)),
resource.TestCheckResourceAttr(resourceName, "enabled", strconv.FormatBool(false)),
resource.TestMatchResourceAttr(resourceName, "function_arn", uFuncArnRe),
resource.TestCheckNoResourceAttr(resourceName, "starting_position"),
),
},
},
})
}

func TestAccAWSLambdaEventSourceMapping_sqs_withFunctionName(t *testing.T) {
var conf lambda.EventSourceMappingConfiguration

resourceName := "aws_lambda_event_source_mapping.lambda_event_source_mapping_test"

rString := acctest.RandString(8)
roleName := fmt.Sprintf("tf_acc_role_lambda_sqs_basic_%s", rString)
policyName := fmt.Sprintf("tf_acc_policy_lambda_sqs_basic_%s", rString)
attName := fmt.Sprintf("tf_acc_att_lambda_sqs_basic_%s", rString)
streamName := fmt.Sprintf("tf_acc_stream_lambda_sqs_basic_%s", rString)
funcName := fmt.Sprintf("tf_acc_lambda_sqs_basic_%s", rString)
funcArnRe := regexp.MustCompile(":" + funcName + "$")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLambdaEventSourceMappingDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSLambdaEventSourceMappingConfig_sqs_testWithFunctionName(roleName, policyName, attName, streamName, funcName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsLambdaEventSourceMappingExists(resourceName, &conf),
testAccCheckAWSLambdaEventSourceMappingAttributes(&conf),
resource.TestMatchResourceAttr(resourceName, "function_arn", funcArnRe),
resource.TestMatchResourceAttr(resourceName, "function_name", funcArnRe),
resource.TestCheckNoResourceAttr(resourceName, "starting_position"),
),
},
},
Expand Down Expand Up @@ -840,3 +867,72 @@ resource "aws_lambda_event_source_mapping" "lambda_event_source_mapping_test" {
function_name = "${aws_lambda_function.lambda_function_test_update.arn}"
}`, roleName, policyName, attName, streamName, funcName, uFuncName)
}

func testAccAWSLambdaEventSourceMappingConfig_sqs_testWithFunctionName(roleName, policyName, attName, streamName, funcName string) string {
return fmt.Sprintf(`
resource "aws_iam_role" "iam_for_lambda" {
name = "%[1]s"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}

resource "aws_iam_policy" "policy_for_role" {
name = "%[2]s"
path = "/"
description = "IAM policy for Lambda event mapping testing"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"sqs:*"
],
"Resource": "*"
}
]
}
EOF
}

resource "aws_iam_policy_attachment" "policy_attachment_for_role" {
name = "%[3]s"
roles = ["${aws_iam_role.iam_for_lambda.name}"]
policy_arn = "${aws_iam_policy.policy_for_role.arn}"
}

resource "aws_sqs_queue" "sqs_queue_test" {
name = "%[4]s"
}

resource "aws_lambda_function" "lambda_function_test_create" {
filename = "test-fixtures/lambdatest.zip"
function_name = "%[5]s"
role = "${aws_iam_role.iam_for_lambda.arn}"
handler = "exports.example"
runtime = "nodejs4.3"
}


resource "aws_lambda_event_source_mapping" "lambda_event_source_mapping_test" {
batch_size = 5
event_source_arn = "${aws_sqs_queue.sqs_queue_test.arn}"
depends_on = ["aws_iam_policy_attachment.policy_attachment_for_role"]
enabled = false
function_name = "%[5]s"
}`, roleName, policyName, attName, streamName, funcName)
}