Comments (5)
Hi @maroux! Sorry I didn't notice this issue before.
What you are seeing here is actually a Terraform Core limitation, where depends_on
is not properly supported for data resources. You can see more details about this in hashicorp/terraform#17034, along with our current plan for how to address it in a future release.
The usual workaround for this is to use implicit dependencies rather than explicit dependencies. This works because the explicit dependency gives Terraform's planning engine more information: it can tell what aspect of the other resource is significant and thus determine when it is safe to produce the archive early, during the plan phase.
Unfortunately this is hard to do directly with the archive_file
data source because it doesn't have any convenient attributes that can be interpolated from the null_resource
to create those implicit dependencies. This can in turn be worked around by making use of the null_data_source
data source as an extra indirection:
resource "null_resource" "lambda_exporter" {
# (some local-exec provisioner blocks, presumably...)
triggers {
index = "${base64sha256(file("${path.module}/lambda-files/index.js"))}"
}
}
data "null_data_source" "wait_for_lambda_exporter" {
inputs = {
# This ensures that this data resource will not be evaluated until
# after the null_resource has been created.
lambda_exporter_id = "${null_resource.lambda_exporter.id}"
# This value gives us something to implicitly depend on
# in the archive_file below.
source_dir = "${path.module}/lambda-files/"
}
}
data "archive_file" "lambda_exporter" {
output_path = "${path.module}/lambda-files.zip"
source_dir = "${data.null_data_source.wait_for_lambda_exporter.outputs["source_dir"]}"
type = "zip"
}
Introducing this extra data source allows us to use an implicit dependency rather than an explicit dependency in both cases: data.null_data_source.wait_for_lambda_exporter
implicitly depends on null_resource.lambda_exporter
, and archive_file.lambda_exporter
implicitly depends on datanull_data_source.wait_for_lambda_exporter
.
This extra complexity should be unnecessary after we complete the work described in hashicorp/terraform#17034.
Please note that we do not generally recommend using Terraform to construct deployment artifacts, because it is not an application build tool; the archive_file
feature is offered to enable some unusual cases, but in most situations it's better to construct a lambda function package via a separate build process -- for example, in the CI system for the application being deployed -- and use Terraform only for the final step of creating/updating the lambda function using that zip file. There are more details on this in our guide on using AWS Lambda and API Gateway with Terraform.
from terraform-provider-archive.
Since this is a Terraform Core issue and we already have hashicorp/terraform#17034 open for it, I'm going to close this issue just to consolidate the discussion over there. Thanks for reporting this!
from terraform-provider-archive.
Nice works for me
from terraform-provider-archive.
@ChineduUzoka really? Just tried the pattern @apparentlymart suggested and it failed. I've reverted back to a uuid
trigger
on my Lambda zip files. 😢
from terraform-provider-archive.
@joestump Take a look at this file here - https://github.com/ChineduUzoka/tf-module-lambda-deployment/blob/master/aws/modules/lambda_deployment/main.tf
it works flawlessly
from terraform-provider-archive.
Related Issues (20)
- Migrate to terraform-plugin-framework HOT 1
- Run archive_file on each apply HOT 2
- data.archive_file does not support resource tainting HOT 2
- Bump Development/Build Minimum Go Version to 1.17 HOT 2
- Issue archiving base64 encoded content w/ source block HOT 5
- Bump Expected Minimum Go Version to 1.18 HOT 1
- archive_file doesn't re-create the archive upon content change
- Source_dir conflicts with source HOT 1
- Zip file created by terraform archive_file cannot be properly read by python
- Generated archive contents include an extra (empty) file when `output_path` is configured within same directory as `source_dir`. HOT 2
- Migrate acceptance testing to terraform-plugin-testing HOT 1
- Bump Expected Minimum Go Version to 1.19 HOT 1
- GitHub Actions - deprecated warnings found - action required! HOT 2
- archive_file data source gets created during "terraform plan" vs "terraform apply" and also is not deleted during destroy HOT 10
- Error generated during the execution of acceptance test on archive_file resource
- Documentation and changelog require updating HOT 1
- Support Additional Compression Types(Ex: tar.gz format) HOT 4
- Update Go Module to Go 1.20 Minimum HOT 1
- archive_file produces a corrupted zip file HOT 5
- Error generating archive with archive_file when symlink is present and exclude_symlink_directories is set to true HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from terraform-provider-archive.