Skip to content

Commit

Permalink
resource/aws_lambda_function: Expand kms_key_arn documentation and te…
Browse files Browse the repository at this point in the history
…sting when no environment variables are present (#10850)

* resource/aws_lambda_function: Expand kms_key_arn documentation and testing when no environment variables are present

Reference: #6366

Output from acceptance testing:

```
--- PASS: TestAccAWSLambdaFunction_KmsKeyArn_NoEnvironmentVariables (56.91s)
```

* tests/resource/aws_lambda_function: Add more information to TestAccAWSLambdaFunction_KmsKeyArn_NoEnvironmentVariables test comment
  • Loading branch information
bflad authored Nov 12, 2019
1 parent c2e47d5 commit 2070567
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
69 changes: 69 additions & 0 deletions aws/resource_aws_lambda_function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,39 @@ func TestAccAWSLambdaFunction_tracingConfig(t *testing.T) {
})
}

// This test is to verify the existing behavior in the Lambda API where the KMS Key ARN
// is not returned if environment variables are not in use. If the API begins saving this
// value and the kms_key_arn check begins failing, the documentation should be updated.
// Reference: https://github.com/terraform-providers/terraform-provider-aws/issues/6366
func TestAccAWSLambdaFunction_KmsKeyArn_NoEnvironmentVariables(t *testing.T) {
var function1 lambda.GetFunctionOutput

rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_lambda_function.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLambdaFunctionDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSLambdaConfigKmsKeyArnNoEnvironmentVariables(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsLambdaFunctionExists(resourceName, rName, &function1),
resource.TestCheckResourceAttr(resourceName, "kms_key_arn", ""),
),
ExpectNonEmptyPlan: true,
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"filename", "publish"},
},
},
})
}

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

Expand Down Expand Up @@ -2192,6 +2225,42 @@ resource "aws_lambda_function" "test" {
`, funcName)
}

func testAccAWSLambdaConfigKmsKeyArnNoEnvironmentVariables(rName string) string {
return fmt.Sprintf(baseAccAWSLambdaConfig(rName, rName, rName)+`
resource "aws_kms_key" "test" {
description = %[1]q
deletion_window_in_days = 7
policy = <<POLICY
{
"Version": "2012-10-17",
"Id": "kms-tf-1",
"Statement": [
{
"Sid": "Enable IAM User Permissions",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "kms:*",
"Resource": "*"
}
]
}
POLICY
}
resource "aws_lambda_function" "test" {
filename = "test-fixtures/lambdatest.zip"
function_name = %[1]q
handler = "exports.example"
kms_key_arn = "${aws_kms_key.test.arn}"
role = "${aws_iam_role.iam_for_lambda.arn}"
runtime = "nodejs8.10"
}
`, rName)
}

func testAccAWSLambdaConfigWithLayers(funcName, layerName, policyName, roleName, sgName string) string {
return fmt.Sprintf(baseAccAWSLambdaConfig(policyName, roleName, sgName)+`
resource "aws_lambda_layer_version" "test" {
Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/lambda_function.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ large files efficiently.
* `publish` - (Optional) Whether to publish creation/change as new Lambda Function Version. Defaults to `false`.
* `vpc_config` - (Optional) Provide this to allow your function to access your VPC. Fields documented below. See [Lambda in VPC][7]
* `environment` - (Optional) The Lambda environment's configuration settings. Fields documented below.
* `kms_key_arn` - (Optional) The ARN for the KMS encryption key.
* `kms_key_arn` - (Optional) Amazon Resource Name (ARN) of the AWS Key Management Service (KMS) key that is used to encrypt environment variables. If this configuration is not provided when environment variables are in use, AWS Lambda uses a default service key. If this configuration is provided when environment variables are not in use, the AWS Lambda API does not save this configuration and Terraform will show a perpetual difference of adding the key. To fix the perpetual difference, remove this configuration.
* `source_code_hash` - (Optional) Used to trigger updates. Must be set to a base64-encoded SHA256 hash of the package file specified with either `filename` or `s3_key`. The usual way to set this is `filebase64sha256("file.zip")` (Terraform 0.11.12 and later) or `base64sha256(file("file.zip"))` (Terraform 0.11.11 and earlier), where "file.zip" is the local filename of the lambda function source archive.
* `tags` - (Optional) A mapping of tags to assign to the object.

Expand Down

0 comments on commit 2070567

Please sign in to comment.