Skip to content

Commit

Permalink
r/aws_lambda_function: Update 'version' and 'qualified_arn' on publish
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremychase committed Sep 3, 2020
1 parent 7296db9 commit 0b00088
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 2 deletions.
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

0 comments on commit 0b00088

Please sign in to comment.