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/lambda_function: fix drift when source_code_hash does not match upstream value #37669

Merged
merged 5 commits into from
May 23, 2024
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
15 changes: 15 additions & 0 deletions .changelog/37669.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
```release-note:bug
resource/aws_lambda_function: Fix issue when `source_code_hash` forces causes drift even if source code has not changed
```

```release-note:enhancement
resource/aws_lambda_function: Add `code_sha256` attribute
```

```release-note:enhancement
data-source/aws_lambda_function: Add `code_sha256` attribute
```

```release-note:note
data-source/aws_lambda_function: `source_code_hash` attribute has been deprecated in favor of `code_sha256`. Will be removed in a future major version
```
7 changes: 6 additions & 1 deletion internal/service/lambda/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ func resourceFunction() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"code_sha256": {
Type: schema.TypeString,
Computed: true,
},
"code_signing_config_arn": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -642,6 +646,7 @@ func resourceFunctionRead(ctx context.Context, d *schema.ResourceData, meta inte
d.Set("architectures", function.Architectures)
functionARN := aws.ToString(function.FunctionArn)
d.Set(names.AttrARN, functionARN)
d.Set("code_sha256", function.CodeSha256)
if function.DeadLetterConfig != nil && function.DeadLetterConfig.TargetArn != nil {
if err := d.Set("dead_letter_config", []interface{}{
map[string]interface{}{
Expand Down Expand Up @@ -695,7 +700,7 @@ func resourceFunctionRead(ctx context.Context, d *schema.ResourceData, meta inte
if err := d.Set("snap_start", flattenSnapStart(function.SnapStart)); err != nil {
return sdkdiag.AppendErrorf(diags, "setting snap_start: %s", err)
}
d.Set("source_code_hash", function.CodeSha256)
d.Set("source_code_hash", d.Get("source_code_hash"))
d.Set("source_code_size", function.CodeSize)
d.Set(names.AttrTimeout, function.Timeout)
tracingConfigMode := awstypes.TracingModePassThrough
Expand Down
10 changes: 8 additions & 2 deletions internal/service/lambda/function_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ func dataSourceFunction() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"code_sha256": {
Type: schema.TypeString,
Computed: true,
},
"code_signing_config_arn": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -186,8 +190,9 @@ func dataSourceFunction() *schema.Resource {
Computed: true,
},
"source_code_hash": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Computed: true,
Deprecated: "This attribute is deprecated and will be removed in a future major version. Use `code_sha256` instead.",
},
"source_code_size": {
Type: schema.TypeInt,
Expand Down Expand Up @@ -288,6 +293,7 @@ func dataSourceFunctionRead(ctx context.Context, d *schema.ResourceData, meta in
d.SetId(functionName)
d.Set("architectures", function.Architectures)
d.Set(names.AttrARN, unqualifiedARN)
d.Set("code_sha256", function.CodeSha256)
if function.DeadLetterConfig != nil && function.DeadLetterConfig.TargetArn != nil {
if err := d.Set("dead_letter_config", []interface{}{
map[string]interface{}{
Expand Down
3 changes: 2 additions & 1 deletion internal/service/lambda/function_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func TestAccLambdaFunctionDataSource_basic(t *testing.T) {
Config: testAccFunctionDataSourceConfig_basic(rName),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrPair(dataSourceName, names.AttrARN, resourceName, names.AttrARN),
resource.TestCheckResourceAttrPair(dataSourceName, "code_sha256", resourceName, "code_sha256"),
resource.TestCheckResourceAttrPair(dataSourceName, "code_signing_config_arn", resourceName, "code_signing_config_arn"),
resource.TestCheckResourceAttrPair(dataSourceName, "dead_letter_config.#", resourceName, "dead_letter_config.#"),
resource.TestCheckResourceAttrPair(dataSourceName, names.AttrDescription, resourceName, names.AttrDescription),
Expand All @@ -51,7 +52,7 @@ func TestAccLambdaFunctionDataSource_basic(t *testing.T) {
resource.TestCheckResourceAttrPair(dataSourceName, "runtime", resourceName, "runtime"),
resource.TestCheckResourceAttrPair(dataSourceName, "signing_job_arn", resourceName, "signing_job_arn"),
resource.TestCheckResourceAttrPair(dataSourceName, "signing_profile_version_arn", resourceName, "signing_profile_version_arn"),
resource.TestCheckResourceAttrPair(dataSourceName, "source_code_hash", resourceName, "source_code_hash"),
resource.TestCheckResourceAttrPair(dataSourceName, "source_code_hash", resourceName, "code_sha256"),
resource.TestCheckResourceAttrPair(dataSourceName, "source_code_size", resourceName, "source_code_size"),
resource.TestCheckResourceAttrPair(dataSourceName, acctest.CtTagsPercent, resourceName, acctest.CtTagsPercent),
resource.TestCheckResourceAttrPair(dataSourceName, names.AttrTimeout, resourceName, names.AttrTimeout),
Expand Down
3 changes: 2 additions & 1 deletion internal/service/lambda/function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func TestAccLambdaFunction_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "architectures.#", acctest.Ct1),
resource.TestCheckResourceAttr(resourceName, "architectures.0", string(awstypes.ArchitectureX8664)),
acctest.CheckResourceAttrRegionalARN(resourceName, names.AttrARN, "lambda", fmt.Sprintf("function:%s", funcName)),
resource.TestCheckResourceAttrSet(resourceName, "code_sha256"),
resource.TestCheckResourceAttr(resourceName, "ephemeral_storage.#", acctest.Ct1),
resource.TestCheckResourceAttr(resourceName, "ephemeral_storage.0.size", "512"),
resource.TestCheckResourceAttr(resourceName, "logging_config.#", acctest.Ct1),
Expand Down Expand Up @@ -1892,7 +1893,7 @@ func TestAccLambdaFunction_localUpdate(t *testing.T) {
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"filename", "publish"},
ImportStateVerifyIgnore: []string{"filename", "publish", "source_code_hash"},
},
{
PreConfig: func() {
Expand Down
3 changes: 2 additions & 1 deletion website/docs/d/lambda_function.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ This data source exports the following attributes in addition to the arguments a

* `architectures` - Instruction set architecture for the Lambda function.
* `arn` - Unqualified (no `:QUALIFIER` or `:VERSION` suffix) ARN identifying your Lambda Function. See also `qualified_arn`.
* `code_sha256` - Base64-encoded representation of raw SHA-256 sum of the zip file.
* `code_signing_config_arn` - ARN for a Code Signing Configuration.
* `dead_letter_config` - Configure the function's *dead letter queue*.
* `description` - Description of what your Lambda Function does.
Expand All @@ -56,7 +57,7 @@ This data source exports the following attributes in addition to the arguments a
* `runtime` - Runtime environment for the Lambda function.
* `signing_job_arn` - ARN of a signing job.
* `signing_profile_version_arn` - The ARN for a signing profile version.
* `source_code_hash` - Base64-encoded representation of raw SHA-256 sum of the zip file.
* `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.
* `timeout` - Function execution time at which Lambda should terminate the function.
* `tracing_config` - Tracing settings of the function.
Expand Down
3 changes: 2 additions & 1 deletion website/docs/r/lambda_function.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ Set the `replacement_security_group_ids` attribute to use a custom list of secur
* `s3_key` - (Optional) S3 key of an object containing the function's deployment package. When `s3_bucket` is set, `s3_key` is required.
* `s3_object_version` - (Optional) Object version containing the function's deployment package. Conflicts with `filename` and `image_uri`.
* `skip_destroy` - (Optional) Set to true if you do not wish the function to be deleted at destroy time, and instead just remove the function from the Terraform state.
* `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.
* `source_code_hash` - (Optional) Virtual attribute used to trigger replacement when source code changes. 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.
* `snap_start` - (Optional) Snap start settings block. Detailed below.
* `tags` - (Optional) Map of tags to assign to the object. 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`. See [Limits][5].
Expand Down Expand Up @@ -361,6 +361,7 @@ For network connectivity to AWS resources in a VPC, specify a list of security g
This resource exports the following attributes in addition to the arguments above:

* `arn` - Amazon Resource Name (ARN) identifying your Lambda Function.
* `code_sha256` - Base64-encoded representation of raw SHA-256 sum of the zip file.
* `invoke_arn` - ARN to be used for invoking Lambda Function from API Gateway - to be used in [`aws_api_gateway_integration`](/docs/providers/aws/r/api_gateway_integration.html)'s `uri`.
* `last_modified` - Date this resource was last modified.
* `qualified_arn` - ARN identifying your Lambda Function Version (if versioning is enabled via `publish = true`).
Expand Down
Loading