Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
7 changes: 7 additions & 0 deletions .changelog/44080.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:enhancement
resource/aws_lambda_function: Add `source_kms_key_arn` argument
```

```release-note:enhancement
data-source/aws_lambda_function: Add `source_kms_key_arn` attribute
```
18 changes: 18 additions & 0 deletions internal/service/lambda/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,12 @@ func resourceFunction() *schema.Resource {
Type: schema.TypeInt,
Computed: true,
},
"source_kms_key_arn": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: verify.ValidARN,
ConflictsWith: []string{"image_uri"},
},
names.AttrTags: tftags.TagsSchema(),
names.AttrTagsAll: tftags.TagsSchemaComputed(),
names.AttrTimeout: {
Expand Down Expand Up @@ -575,6 +581,10 @@ func resourceFunctionCreate(ctx context.Context, d *schema.ResourceData, meta an
input.SnapStart = expandSnapStart(v.([]any))
}

if v, ok := d.GetOk("source_kms_key_arn"); ok {
input.Code.SourceKMSKeyArn = aws.String(v.(string))
}

if v, ok := d.GetOk("tracing_config"); ok && len(v.([]any)) > 0 && v.([]any)[0] != nil {
input.TracingConfig = &awstypes.TracingConfig{
Mode: awstypes.TracingMode(v.([]any)[0].(map[string]any)[names.AttrMode].(string)),
Expand Down Expand Up @@ -669,6 +679,7 @@ func resourceFunctionRead(ctx context.Context, d *schema.ResourceData, meta any)
}

function := output.Configuration
functionCode := output.Code
d.Set("architectures", function.Architectures)
functionARN := aws.ToString(function.FunctionArn)
d.Set(names.AttrARN, functionARN)
Expand Down Expand Up @@ -728,6 +739,7 @@ func resourceFunctionRead(ctx context.Context, d *schema.ResourceData, meta any)
}
d.Set("source_code_hash", d.Get("source_code_hash"))
d.Set("source_code_size", function.CodeSize)
d.Set("source_kms_key_arn", functionCode.SourceKMSKeyArn)
d.Set(names.AttrTimeout, function.Timeout)
tracingConfigMode := awstypes.TracingModePassThrough
if function.TracingConfig != nil {
Expand Down Expand Up @@ -1000,6 +1012,11 @@ func resourceFunctionUpdate(ctx context.Context, d *schema.ResourceData, meta an
}
}

// If source_kms_key_arn is set, it should be always included in the update
if v, ok := d.GetOk("source_kms_key_arn"); ok {
input.SourceKMSKeyArn = aws.String(v.(string))
}

_, err := conn.UpdateFunctionCode(ctx, &input)

if err != nil {
Expand Down Expand Up @@ -1489,6 +1506,7 @@ func needsFunctionCodeUpdate(d sdkv2.ResourceDiffer) bool {
d.HasChange(names.AttrS3Bucket) ||
d.HasChange("s3_key") ||
d.HasChange("s3_object_version") ||
d.HasChange("source_kms_key_arn") ||
d.HasChange("image_uri") ||
d.HasChange("architectures")
}
Expand Down
6 changes: 6 additions & 0 deletions internal/service/lambda/function_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ func dataSourceFunction() *schema.Resource {
Type: schema.TypeInt,
Computed: true,
},
"source_kms_key_arn": {
Type: schema.TypeString,
Computed: true,
},
names.AttrTags: tftags.TagsSchemaComputed(),
names.AttrTimeout: {
Type: schema.TypeInt,
Expand Down Expand Up @@ -297,6 +301,7 @@ func dataSourceFunctionRead(ctx context.Context, d *schema.ResourceData, meta an
}

function := output.Configuration
functionCode := output.Code
functionARN := aws.ToString(function.FunctionArn)
qualifierSuffix := fmt.Sprintf(":%s", aws.ToString(input.Qualifier))
versionSuffix := fmt.Sprintf(":%s", aws.ToString(function.Version))
Expand Down Expand Up @@ -358,6 +363,7 @@ func dataSourceFunctionRead(ctx context.Context, d *schema.ResourceData, meta an
d.Set("signing_profile_version_arn", function.SigningProfileVersionArn)
d.Set("source_code_hash", function.CodeSha256)
d.Set("source_code_size", function.CodeSize)
d.Set("source_kms_key_arn", functionCode.SourceKMSKeyArn)
d.Set(names.AttrTimeout, function.Timeout)
tracingConfigMode := awstypes.TracingModePassThrough
if function.TracingConfig != nil {
Expand Down
1 change: 1 addition & 0 deletions internal/service/lambda/function_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func TestAccLambdaFunctionDataSource_basic(t *testing.T) {
resource.TestCheckResourceAttrPair(dataSourceName, "signing_profile_version_arn", resourceName, "signing_profile_version_arn"),
resource.TestCheckResourceAttrPair(dataSourceName, "source_code_hash", resourceName, "code_sha256"),
resource.TestCheckResourceAttrPair(dataSourceName, "source_code_size", resourceName, "source_code_size"),
resource.TestCheckResourceAttrPair(dataSourceName, "source_kms_key_arn", resourceName, "source_kms_key_arn"),
resource.TestCheckResourceAttrPair(dataSourceName, acctest.CtTagsPercent, resourceName, acctest.CtTagsPercent),
resource.TestCheckResourceAttrPair(dataSourceName, names.AttrTimeout, resourceName, names.AttrTimeout),
resource.TestCheckResourceAttrPair(dataSourceName, "tracing_config.#", resourceName, "tracing_config.#"),
Expand Down
115 changes: 115 additions & 0 deletions internal/service/lambda/function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2356,6 +2356,58 @@ func TestAccLambdaFunction_ipv6AllowedForDualStack(t *testing.T) {
})
}

func TestAccLambdaFunction_sourceKMSKeyARN(t *testing.T) {
ctx := acctest.Context(t)
var conf lambda.GetFunctionOutput
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_lambda_function.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.LambdaServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckFunctionDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccFunctionConfig_sourceKMSKeyARN(rName, "test"),
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectResourceAction(resourceName, plancheck.ResourceActionCreate),
},
},
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckFunctionExists(ctx, resourceName, &conf),
testAccCheckFunctionInvokeARN(resourceName, &conf),
testAccCheckFunctionQualifiedInvokeARN(resourceName, &conf),
testAccCheckFunctionName(&conf, rName),
resource.TestCheckResourceAttrPair(resourceName, "source_kms_key_arn", "aws_kms_key.test", names.AttrARN),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"filename", "publish"},
},
{
Config: testAccFunctionConfig_sourceKMSKeyARN(rName, "test2"),
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectResourceAction(resourceName, plancheck.ResourceActionUpdate),
},
},
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckFunctionExists(ctx, resourceName, &conf),
testAccCheckFunctionInvokeARN(resourceName, &conf),
testAccCheckFunctionQualifiedInvokeARN(resourceName, &conf),
testAccCheckFunctionName(&conf, rName),
resource.TestCheckResourceAttrPair(resourceName, "source_kms_key_arn", "aws_kms_key.test2", names.AttrARN),
),
},
},
})
}

func TestAccLambdaFunction_skipDestroy(t *testing.T) {
ctx := acctest.Context(t)
var conf lambda.GetFunctionOutput
Expand Down Expand Up @@ -4186,6 +4238,69 @@ resource "aws_lambda_function" "test" {
`, rName))
}

func testAccFunctionConfig_sourceKMSKeyARN(rName, kmsIdentifier string) string {
return acctest.ConfigCompose(
acctest.ConfigLambdaBase(rName, rName, rName),
fmt.Sprintf(`
resource "aws_kms_key" "test" {
description = "%[1]s-1"
deletion_window_in_days = 7
enable_key_rotation = true

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_kms_key" "test2" {
description = "%[1]s-2"
deletion_window_in_days = 7
enable_key_rotation = true

policy = <<POLICY
{
"Version": "2012-10-17",
"Id": "kms-tf-2",
"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
role = aws_iam_role.iam_for_lambda.arn
handler = "exports.example"
runtime = "nodejs20.x"
source_kms_key_arn = aws_kms_key.%[2]s.arn
}
`, rName, kmsIdentifier))
}

func testAccFunctionConfig_skipDestroy(rName string) string {
return acctest.ConfigCompose(acctest.ConfigLambdaBase(rName, rName, rName), fmt.Sprintf(`
resource "aws_lambda_function" "test" {
Expand Down
1 change: 1 addition & 0 deletions website/docs/d/lambda_function.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ This data source exports the following attributes in addition to the arguments a
* `signing_profile_version_arn` - ARN for a signing profile version.
* `source_code_hash` - (**Deprecated** use `code_sha256` instead) Base64-encoded representation of raw SHA-256 sum of the zip file.
* `source_code_size` - Size in bytes of the function .zip file.
* `source_kms_key_arn` - ARN of the AWS Key Management Service key used to encrypt the function's `.zip` deployment package.
* `tags` - Map of tags assigned to the Lambda Function.
* `timeout` - Function execution time at which Lambda should terminate the function.
* `tracing_config` - Tracing settings of the function. [See below](#tracing_config-attribute-reference).
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/lambda_function.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@ The following arguments are optional:
* `skip_destroy` - (Optional) Whether to retain the old version of a previously deployed Lambda Layer. Default is `false`.
* `snap_start` - (Optional) Configuration block for snap start settings. [See below](#snap_start-configuration-block).
* `source_code_hash` - (Optional) Base64-encoded SHA256 hash of the package file. Used to trigger updates when source code changes.
* `source_kms_key_arn` - (Optional) ARN of the AWS Key Management Service key used to encrypt the function's `.zip` deployment package. Conflicts with `image_uri`.
* `tags` - (Optional) Key-value map of tags for the Lambda function. If configured with a provider [`default_tags` configuration block](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level.
* `timeout` - (Optional) Amount of time your Lambda Function has to run in seconds. Defaults to 3. Valid between 1 and 900.
* `tracing_config` - (Optional) Configuration block for X-Ray tracing. [See below](#tracing_config-configuration-block).
Expand Down
Loading