Skip to content

Commit 1a53701

Browse files
authored
Fix s3 backend setting DynamoDB state lock on by default (#77)
1 parent 5493d6c commit 1a53701

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ ADDITIONAL_TF_OVERRIDE_LOCATIONS=/path/to/module1,path/to/module2 tflocal plan
7777

7878
## Change Log
7979

80+
* v0.22.0: Fix S3 backend forcing DynamoDB State Lock to be enabled by default
8081
* v0.21.0: Add ability to drop an override file in additional locations
8182
* v0.20.1: Fix list config rendering
8283
* v0.20.0: Fix S3 backend option merging

bin/tflocal

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,6 @@ def generate_s3_backend_config() -> str:
288288
# note: default values, updated by `backend_config` further below...
289289
"bucket": "tf-test-state",
290290
"key": "terraform.tfstate",
291-
"dynamodb_table": "tf-test-state",
292291
"region": get_region(),
293292
"skip_credentials_validation": True,
294293
"skip_metadata_api_check": True,
@@ -337,7 +336,8 @@ def generate_s3_backend_config() -> str:
337336
configs.update(backend_config)
338337
if not DRY_RUN:
339338
get_or_create_bucket(configs["bucket"])
340-
get_or_create_ddb_table(configs["dynamodb_table"], region=configs["region"])
339+
if "dynamodb_table" in configs:
340+
get_or_create_ddb_table(configs["dynamodb_table"], region=configs["region"])
341341
result = TF_S3_BACKEND_CONFIG
342342
config_options = ""
343343
for key, value in sorted(configs.items()):

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = terraform-local
3-
version = 0.21.0
3+
version = 0.22.0
44
url = https://github.com/localstack/terraform-local
55
author = LocalStack Team
66
author_email = info@localstack.cloud

tests/test_apply.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,43 @@ def test_s3_backend():
205205
assert result["ResponseMetadata"]["HTTPStatusCode"] == 200
206206

207207

208+
def test_s3_backend_state_lock_default():
209+
state_bucket = f"tf-state-{short_uid()}"
210+
bucket_name = f"bucket-{short_uid()}"
211+
state_region = "us-west-1"
212+
dynamodb_client = client("dynamodb", region_name=state_region)
213+
table_amount = len(dynamodb_client.list_tables()["TableNames"])
214+
config = """
215+
terraform {
216+
backend "s3" {
217+
bucket = "%s"
218+
key = "terraform.tfstate"
219+
region = "%s"
220+
skip_credentials_validation = true
221+
}
222+
}
223+
resource "aws_s3_bucket" "test-bucket" {
224+
bucket = "%s"
225+
}
226+
""" % (state_bucket, state_region, bucket_name)
227+
deploy_tf_script(config)
228+
229+
# assert that bucket with state file exists
230+
s3 = client("s3", region_name=state_region)
231+
result = s3.list_objects(Bucket=state_bucket)
232+
keys = [obj["Key"] for obj in result["Contents"]]
233+
assert "terraform.tfstate" in keys
234+
235+
# assert that DynamoDB table with state file locks has not been created by default
236+
result = dynamodb_client.list_tables()
237+
assert len(result["TableNames"]) == table_amount
238+
239+
# assert that S3 resource has been created
240+
s3 = client("s3")
241+
result = s3.head_bucket(Bucket=bucket_name)
242+
assert result["ResponseMetadata"]["HTTPStatusCode"] == 200
243+
244+
208245
def test_dry_run(monkeypatch):
209246
monkeypatch.setenv("DRY_RUN", "1")
210247
state_bucket = "tf-state-dry-run"

0 commit comments

Comments
 (0)