Skip to content

Commit

Permalink
resource/aws_lambda_alias: Suppress differences for equivalent functi…
Browse files Browse the repository at this point in the history
…on_name name and ARN (#12902)

Output from acceptance testing:

```
--- PASS: TestAccAWSLambdaAlias_basic (39.20s)
--- PASS: TestAccAWSLambdaAlias_routingconfig (48.33s)
--- PASS: TestAccAWSLambdaAlias_nameupdate (50.40s)
```
  • Loading branch information
angelyan authored Apr 20, 2020
1 parent 9ba2bed commit 2559c9e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 48 deletions.
7 changes: 7 additions & 0 deletions aws/resource_aws_lambda_alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
83 changes: 35 additions & 48 deletions aws/resource_aws_lambda_alias_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
})
}
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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 = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
func testAccAwsLambdaAliasConfigUsingFunctionName(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"
role = "${aws_iam_role.iam_for_lambda.arn}"
handler = "exports.example"
runtime = "nodejs12.x"
source_code_hash = "${filebase64sha256("test-fixtures/lambdatest.zip")}"
publish = "true"
}
resource "aws_iam_policy" "policy_for_role" {
name = "%s"
path = "/"
description = "IAM policy for for Lamda alias testing"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"lambda:*"
],
"Resource": "*"
}
]
}
EOF
resource "aws_lambda_alias" "lambda_alias_test" {
name = "%s"
description = "a sample description"
function_name = "${aws_lambda_function.lambda_function_test_create.function_name}"
function_version = "1"
}
resource "aws_iam_policy_attachment" "policy_attachment_for_role" {
name = "%s"
roles = ["${aws_iam_role.iam_for_lambda.name}"]
policy_arn = "${aws_iam_policy.policy_for_role.arn}"
`, funcName, aliasName))
}

func testAccAwsLambdaAliasConfigWithRoutingConfig(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_modified.zip"
function_name = "%s"
Expand All @@ -359,5 +346,5 @@ resource "aws_lambda_alias" "lambda_alias_test" {
}
}
}
`, roleName, policyName, attachmentName, funcName, aliasName)
`, funcName, aliasName))
}

0 comments on commit 2559c9e

Please sign in to comment.