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

Skip to content

Provider produced inconsistent final plan in github actions pipeline #158

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

Closed
jack-dunleavy opened this issue Jun 7, 2021 · 11 comments
Closed

Comments

@jack-dunleavy
Copy link

jack-dunleavy commented Jun 7, 2021

Description

I'm seeing an issue deploying a lambda function using Github actions CI/CD. Specifically we have the pipeline set up to perform terraform plan, store the output, then run terraform apply in a later step. The issue appears to be due to the source_code_hash attribute that is calculated during plan not matching the hash during apply:

β”‚ Error: Provider produced inconsistent final plan
β”‚ 
β”‚ When expanding the plan for module.test_lambda.aws_lambda_function.this[0] to include new values learned so far during apply, provider "registry.terraform.io/hashicorp/aws" produced an invalid new value for .source_code_hash:
β”‚ was cty.StringVal("3mMv+AOi71OKcWRJFwQpJodzz13sdQ8U+W1DLqYnS+8="), but now cty.StringVal("L60e+m5YKaMuYvyjOS+J0HlXynS/xy072jqcLlArcY8=").
β”‚ 
β”‚ This is a bug in the provider, which should be reported in the provider's own issue tracker.

Versions

Terraform v0.15.1
on darwin_amd64

  • provider registry.terraform.io/hashicorp/aws v3.44.0
  • provider registry.terraform.io/hashicorp/external v2.1.0
  • provider registry.terraform.io/hashicorp/local v2.1.0
  • provider registry.terraform.io/hashicorp/null v3.1.0
  • provider registry.terraform.io/hashicorp/random v3.1.0

Reproduction

Steps to reproduce the behavior:

Are you using workspaces? : No

I think this is to do with the source files being checked out twice (once for plan and again for apply) and so having different metadata in each of the phases. I've been able to reproduce this issue locally by performing the following steps:

  1. Run terraform plan: TF_RECREATE_MISSING_LAMBDA_PACKAGE=false terraform plan -out sandbox.plan
  2. Create a copy of the lambda handler: cp src/index.js src/index_1.js
  3. Delete the lambda handler: rm src/index.js
  4. Rename the copy: mv src/index_1.js src/index.js
  5. Run terraform apply: TF_RECREATE_MISSING_LAMBDA_PACKAGE=false terraform apply sandbox.plan

Code Snippet to Reproduce

main.tf

module "test_lambda" {
  source  = "terraform-aws-modules/lambda/aws"
  version = "2.0.0"

  function_name              = "test-lambda"
  handler                           = "index.handler"
  runtime                           = "nodejs14.x"
  publish                           = true
  cloudwatch_logs_retention_in_days = 90

  source_path = "./src"
}

providers.tf

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 3.40"
    }
  }
}

provider "aws" {
  region = "eu-west-2"
}

src/index.js

exports.handler = async (event) => {
  return event;
};

Expected behavior

The new lambda is applied by Terraform

Actual behavior

The new lambda deployment fails with Error: Provider produced inconsistent final plan

Terminal Output Screenshot(s)

Screenshot 2021-06-07 at 12 23 20
Screenshot 2021-06-07 at 12 43 41

Additional context

@zachwhaley
Copy link

We are also running into this issue.

@jack-dunleavy
Copy link
Author

Any chance somebody could take a look? We've had to work around the issue by manually packaging our lambdas for the time being but it would be really nice to use the module for that

@antonbabenko
Copy link
Member

The issue happens when the correct zip archive is not yet created before running apply and previous source_code_hash is being used. terraform plan creates a small json-plan file but not zip-archive.

If the content of src files is changed before apply, the source_code_hash that it produces will differ from the one stored in terraform plan file (sandbox.plan). Setting TF_RECREATE_MISSING_LAMBDA_PACKAGE=false won't help.

@zachwhaley
Copy link

We are seeing this now when we run terraform apply with no plan. It is not consistent, but happens often enough.

@jack-dunleavy
Copy link
Author

The contents of the source code has not changed in our case, but because the repository is checked out separately between the plan and apply steps the metadata of the files changes (which in turn produces a different source code hash).

We've had to work around this by forcibly setting the modification time on every file to a fixed point after checking out (which makes the hash generation fully deterministic)

@antonbabenko
Copy link
Member

I believe this issue is resolved by #175 if you use v2.10.0 and set ignore_source_code_hash = true.

Or is there anything left for this issue to remain open?

@zachwhaley
Copy link

I would say this is not resolved by #175 but that that change enables a different form of deployment.

I would still like to be able to use the lambda module to package my lambda code during a plan and then deploy that lambda code during an apply without error.

@antonbabenko
Copy link
Member

Thanks, @zachwhaley !

Let's keep it open in case anyone can spend some time and make a fix for the problem you've described which is very much a valid use-case.

@antonbabenko
Copy link
Member

Please take a look at #181 (version v2.16.0). It allows one to not worry about archive recreation when it has been deployed but currently missing (eg, a different step in the pipeline).

It may be not 100% related to the issue but it will help a lot of users.

Actually, I don't know a better solution at the moment. There is one important factor implemented by the module - terraform plan will never create an archive, so users can't just run terraform plan into a file and then apply just that file without dirty hacks.

I want to close this issue but please open a new one using the latest version of the module because there were several small improvements since this issue was originally created.

@zachwhaley
Copy link

For the record, we are still hitting this issue with all the workarounds and latest updates 😩

It seems to be a particular lambda that is the offender 🀷🏻

I can create a new issue with more details once I've collected said details.

Thanks for the hard work πŸ’™

@github-actions
Copy link

github-actions bot commented Nov 9, 2022

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants