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

d/aws_lambda_function: prevent read error when fetching code signing config for "Image" package type #17180

Merged
merged 2 commits into from
Jan 21, 2021
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
29 changes: 16 additions & 13 deletions aws/data_source_aws_lambda_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,23 +313,26 @@ func dataSourceAwsLambdaFunctionRead(d *schema.ResourceData, meta interface{}) e
return nil
}

// Get Code Signing Config Output
// If code signing config output exists, set it to that value, otherwise set it empty.
codeSigningConfigInput := &lambda.GetFunctionCodeSigningConfigInput{
FunctionName: aws.String(d.Get("function_name").(string)),
}
// Get Code Signing Config Output.
// Code Signing is only supported on zip packaged lambda functions.
var codeSigningConfigArn string

getCodeSigningConfigOutput, err := conn.GetFunctionCodeSigningConfig(codeSigningConfigInput)
if err != nil {
return fmt.Errorf("error getting Lambda Function (%s) Code Signing Config: %w", aws.StringValue(function.FunctionName), err)
}
if aws.StringValue(function.PackageType) == lambda.PackageTypeZip {
codeSigningConfigInput := &lambda.GetFunctionCodeSigningConfigInput{
FunctionName: function.FunctionName,
}
getCodeSigningConfigOutput, err := conn.GetFunctionCodeSigningConfig(codeSigningConfigInput)
if err != nil {
return fmt.Errorf("error getting Lambda Function (%s) Code Signing Config: %w", aws.StringValue(function.FunctionName), err)
}

if getCodeSigningConfigOutput == nil || getCodeSigningConfigOutput.CodeSigningConfigArn == nil {
d.Set("code_signing_config_arn", "")
} else {
d.Set("code_signing_config_arn", getCodeSigningConfigOutput.CodeSigningConfigArn)
if getCodeSigningConfigOutput != nil {
codeSigningConfigArn = aws.StringValue(getCodeSigningConfigOutput.CodeSigningConfigArn)
}
}

d.Set("code_signing_config_arn", codeSigningConfigArn)

d.SetId(aws.StringValue(function.FunctionName))

return nil
Expand Down
50 changes: 50 additions & 0 deletions aws/data_source_aws_lambda_function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package aws

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
Expand Down Expand Up @@ -180,6 +181,27 @@ func TestAccDataSourceAWSLambdaFunction_fileSystemConfig(t *testing.T) {
})
}

func TestAccDataSourceAWSLambdaFunction_imageConfig(t *testing.T) {
rName := acctest.RandomWithPrefix("tf-acc-test")
dataSourceName := "data.aws_lambda_function.test"
resourceName := "aws_lambda_function.test"

imageLatestID := os.Getenv("AWS_LAMBDA_IMAGE_LATEST_ID")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccDataSourceLambdaImagePreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAWSLambdaFunctionConfigImageConfig(rName, imageLatestID),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(dataSourceName, "code_signing_config_arn", resourceName, "code_signing_config_arn"),
),
},
},
})
}

func testAccDataSourceAWSLambdaFunctionConfigBase(rName string) string {
return fmt.Sprintf(`
resource "aws_iam_role" "lambda" {
Expand Down Expand Up @@ -495,3 +517,31 @@ data "aws_lambda_function" "test" {
}
`, rName)
}

func testAccDataSourceAWSLambdaFunctionConfigImageConfig(rName, imageID string) string {
return composeConfig(
testAccDataSourceAWSLambdaFunctionConfigBase(rName),
fmt.Sprintf(`
resource "aws_lambda_function" "test" {
image_uri = %q
function_name = %q
role = aws_iam_role.lambda.arn
package_type = "Image"
image_config {
entry_point = ["/bootstrap-with-handler"]
command = ["app.lambda_handler"]
working_directory = "/var/task"
}
}

data "aws_lambda_function" "test" {
function_name = aws_lambda_function.test.function_name
}
`, imageID, rName))
}

func testAccDataSourceLambdaImagePreCheck(t *testing.T) {
if os.Getenv("AWS_LAMBDA_IMAGE_LATEST_ID") == "" {
anGie44 marked this conversation as resolved.
Show resolved Hide resolved
t.Skip("AWS_LAMBDA_IMAGE_LATEST_ID env var must be set for Lambda Function Data Source Image Support acceptance tests.")
}
}