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

r/aws_lambda_function: Update 'version' and 'qualified_arn' on publish #15020

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
4 changes: 2 additions & 2 deletions aws/resource_aws_lambda_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ func resourceAwsLambdaFunction() *schema.Resource {
}

func updateComputedAttributesOnPublish(_ context.Context, d *schema.ResourceDiff, meta interface{}) error {
if needsFunctionCodeUpdate(d) {
if needsFunctionCodeUpdate(d) || d.HasChange("publish") {
d.SetNewComputed("last_modified")
publish := d.Get("publish").(bool)
if publish {
Expand Down Expand Up @@ -924,7 +924,7 @@ func resourceAwsLambdaFunctionUpdate(d *schema.ResourceData, meta interface{}) e
}

publish := d.Get("publish").(bool)
if publish && (codeUpdate || configUpdate) {
if publish && (codeUpdate || configUpdate || d.HasChange("publish")) {
versionReq := &lambda.PublishVersionInput{
FunctionName: aws.String(d.Id()),
}
Expand Down
111 changes: 111 additions & 0 deletions aws/resource_aws_lambda_function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,104 @@ func TestAccAWSLambdaFunction_versionedUpdate(t *testing.T) {
})
}

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

rString := acctest.RandString(8)
funcName := fmt.Sprintf("tf_acc_lambda_func_enable_publish_%s", rString)
policyName := fmt.Sprintf("tf_acc_policy_lambda_func_enable_publish_%s", rString)
roleName := fmt.Sprintf("tf_acc_role_lambda_func_enable_publish_%s", rString)
sgName := fmt.Sprintf("tf_acc_sg_lambda_func_enable_publish_%s", rString)
resourceName := "aws_lambda_function.test"
fileName := "test-fixtures/lambdatest.zip"

unpublishedVersion := "$LATEST"
publishedVersion := "1"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLambdaFunctionDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSLambdaConfigPublishable(fileName, funcName, policyName, roleName, sgName, false),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsLambdaFunctionExists(resourceName, funcName, &conf),
testAccCheckAwsLambdaFunctionName(&conf, funcName),
testAccCheckResourceAttrRegionalARN(resourceName, "arn", "lambda", fmt.Sprintf("function:%s", funcName)),
resource.TestCheckResourceAttr(resourceName, "version", unpublishedVersion),
testAccCheckResourceAttrRegionalARN(resourceName, "qualified_arn", "lambda", fmt.Sprintf("function:%s:%s", funcName, unpublishedVersion)),
),
},
{
Config: testAccAWSLambdaConfigPublishable(fileName, funcName, policyName, roleName, sgName, true),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsLambdaFunctionExists(resourceName, funcName, &conf),
testAccCheckAwsLambdaFunctionName(&conf, funcName),
testAccCheckResourceAttrRegionalARN(resourceName, "arn", "lambda", fmt.Sprintf("function:%s", funcName)),
resource.TestCheckResourceAttr(resourceName, "version", publishedVersion),
testAccCheckResourceAttrRegionalARN(resourceName, "qualified_arn", "lambda", fmt.Sprintf("function:%s:%s", funcName, publishedVersion)),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"filename", "publish"},
},
},
})
}

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

rString := acctest.RandString(8)
funcName := fmt.Sprintf("tf_acc_lambda_func_disable_publish_%s", rString)
policyName := fmt.Sprintf("tf_acc_policy_lambda_func_disable_publish_%s", rString)
roleName := fmt.Sprintf("tf_acc_role_lambda_func_disable_publish_%s", rString)
sgName := fmt.Sprintf("tf_acc_sg_lambda_func_disable_publish_%s", rString)
resourceName := "aws_lambda_function.test"
fileName := "test-fixtures/lambdatest.zip"

publishedVersion := "1"
unpublishedVersion := publishedVersion // Should remain the last published version

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLambdaFunctionDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSLambdaConfigPublishable(fileName, funcName, policyName, roleName, sgName, true),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsLambdaFunctionExists(resourceName, funcName, &conf),
testAccCheckAwsLambdaFunctionName(&conf, funcName),
testAccCheckResourceAttrRegionalARN(resourceName, "arn", "lambda", fmt.Sprintf("function:%s", funcName)),
resource.TestCheckResourceAttr(resourceName, "version", publishedVersion),
testAccCheckResourceAttrRegionalARN(resourceName, "qualified_arn", "lambda", fmt.Sprintf("function:%s:%s", funcName, publishedVersion)),
),
},
{
Config: testAccAWSLambdaConfigPublishable(fileName, funcName, policyName, roleName, sgName, false),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsLambdaFunctionExists(resourceName, funcName, &conf),
testAccCheckAwsLambdaFunctionName(&conf, funcName),
testAccCheckResourceAttrRegionalARN(resourceName, "arn", "lambda", fmt.Sprintf("function:%s", funcName)),
resource.TestCheckResourceAttr(resourceName, "version", unpublishedVersion),
testAccCheckResourceAttrRegionalARN(resourceName, "qualified_arn", "lambda", fmt.Sprintf("function:%s:%s", funcName, unpublishedVersion)),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"filename", "publish"},
},
},
})
}

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

Expand Down Expand Up @@ -1917,6 +2015,19 @@ resource "aws_lambda_function" "test" {
`, fileName, funcName)
}

func testAccAWSLambdaConfigPublishable(fileName, funcName, policyName, roleName, sgName string, publish bool) string {
return fmt.Sprintf(baseAccAWSLambdaConfig(policyName, roleName, sgName)+`
resource "aws_lambda_function" "test" {
filename = "%s"
function_name = "%s"
publish = %t
role = aws_iam_role.iam_for_lambda.arn
handler = "exports.example"
runtime = "nodejs12.x"
}
`, fileName, funcName, publish)
}

func testAccAWSLambdaFileSystemConfig(funcName, policyName, roleName, sgName string) string {
return fmt.Sprintf(baseAccAWSLambdaConfig(policyName, roleName, sgName)+`
resource "aws_efs_file_system" "efs_for_lambda" {
Expand Down