From 2559c9e03118e82b91cca09ed377d1b7572ba25f Mon Sep 17 00:00:00 2001 From: angelyan <46203110+angelyan@users.noreply.github.com> Date: Mon, 20 Apr 2020 16:10:49 -0300 Subject: [PATCH] resource/aws_lambda_alias: Suppress differences for equivalent function_name name and ARN (#12902) Output from acceptance testing: ``` --- PASS: TestAccAWSLambdaAlias_basic (39.20s) --- PASS: TestAccAWSLambdaAlias_routingconfig (48.33s) --- PASS: TestAccAWSLambdaAlias_nameupdate (50.40s) ``` --- aws/resource_aws_lambda_alias.go | 7 +++ aws/resource_aws_lambda_alias_test.go | 83 +++++++++++---------------- 2 files changed, 42 insertions(+), 48 deletions(-) diff --git a/aws/resource_aws_lambda_alias.go b/aws/resource_aws_lambda_alias.go index 40ce9ebd5ef..50ec672a2c6 100644 --- a/aws/resource_aws_lambda_alias.go +++ b/aws/resource_aws_lambda_alias.go @@ -30,6 +30,13 @@ func resourceAwsLambdaAlias() *schema.Resource { Type: schema.TypeString, Required: true, ForceNew: 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(new) + return (oldFunctionName == new && oldFunctionNameErr == nil) || (newFunctionName == old && newFunctionNameErr == nil) + }, }, "function_version": { Type: schema.TypeString, diff --git a/aws/resource_aws_lambda_alias_test.go b/aws/resource_aws_lambda_alias_test.go index 92a1bfc4592..5b857c5b2bc 100644 --- a/aws/resource_aws_lambda_alias_test.go +++ b/aws/resource_aws_lambda_alias_test.go @@ -44,6 +44,10 @@ func TestAccAWSLambdaAlias_basic(t *testing.T) { ImportStateId: fmt.Sprintf("%s/%s", funcName, aliasName), ImportStateVerify: true, }, + { + Config: testAccAwsLambdaAliasConfigUsingFunctionName(roleName, policyName, attachmentName, funcName, aliasName), + PlanOnly: true, + }, }, }) } @@ -219,8 +223,7 @@ func testAccCheckAwsLambdaAliasRoutingConfigDoesNotExist(mapping *lambda.AliasCo return nil } } - -func testAccAwsLambdaAliasConfig(roleName, policyName, attachmentName, funcName, aliasName string) string { +func testAccAwsLambdaAliasBaseConfig(roleName, policyName, attachmentName string) string { return fmt.Sprintf(` resource "aws_iam_role" "iam_for_lambda" { name = "%s" @@ -268,7 +271,13 @@ resource "aws_iam_policy_attachment" "policy_attachment_for_role" { roles = ["${aws_iam_role.iam_for_lambda.name}"] policy_arn = "${aws_iam_policy.policy_for_role.arn}" } +`, roleName, policyName, attachmentName) +} +func testAccAwsLambdaAliasConfig(roleName, policyName, attachmentName, funcName, aliasName string) string { + return composeConfig( + testAccAwsLambdaAliasBaseConfig(roleName, policyName, attachmentName), + fmt.Sprintf(` resource "aws_lambda_function" "lambda_function_test_create" { filename = "test-fixtures/lambdatest.zip" function_name = "%s" @@ -285,58 +294,36 @@ resource "aws_lambda_alias" "lambda_alias_test" { function_name = "${aws_lambda_function.lambda_function_test_create.arn}" function_version = "1" } -`, roleName, policyName, attachmentName, funcName, aliasName) +`, funcName, aliasName)) } -func testAccAwsLambdaAliasConfigWithRoutingConfig(roleName, policyName, attachmentName, funcName, aliasName string) string { - return fmt.Sprintf(` -resource "aws_iam_role" "iam_for_lambda" { - name = "%s" - - assume_role_policy = <