localstack / localstack/terraform-local

Errors during tflocal init

Open
#70 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
323
Forks
44
PR merge metrics
No merged PRs in 30d

Description

Running `tflocal init`
```
File ~/.pyenv/versions/3.12.6/lib/python3.12/site-packages/botocore/handlers.py:295, in validate_bucket_name(params, **kwargs)
294 bucket = params['Bucket']
--> 295 if not VALID_BUCKET.search(bucket) and not VALID_S3_ARN.search(bucket):
296 error_msg = (
297 f'Invalid bucket name "{bucket}": Bucket name must match '
298 f'the regex "{VALID_BUCKET.pattern}" or be an ARN matching '
299 f'the regex "{VALID_S3_ARN.pattern}"'
300 )

TypeError: expected string or bytes-like object, got 'list'
```

Why?

Well, terraform-local seems to use hcl2 to parse terraform files.

I have a perfectly standard provider.tf file
```hcl
terraform {
required_version = "~> 1.8"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.91"
}
awscc = {
source = "hashicorp/awscc"
version = "~> 0.14"
}
archive = {
source = "hashicorp/archive"
version = "~> 2.4"
}
}

backend "s3" {
bucket = "ninety-terraform-state"
key = "devops-infra"
region = "us-east-1"
}
}

provider "aws" {
region = "us-east-1"
}

provider "awscc" {
region = "us-east-1"
}
```

When I parse that with hcl2 I get
```
with open('/home/gmauer/code/ninety-infra/devops/provider.tf', 'r') as fp:
foo = hcl2.load(fp)

In [25]: ...: ...:
In [26]: foo
Out[26]:
{'terraform': [{'required_version': ['~> 1.8'],
'required_providers': [{'aws': [{'source': 'hashicorp/aws',
'version': '~> 5.91'}],
'awscc': [{'source': 'hashicorp/awscc', 'version': '~> 0.14'}],
'archive': [{'source': 'hashicorp/archive', 'version': '~> 2.4'}]}],
'backend': [{'s3': {'bucket': ['ninety-terraform-state'],
'key': ['devops-infra'],
'region': ['us-east-1']}}],
'__start_line__': 1,
'__end_line__': 23}],
'provider': [{'aws': {'region': ['us-east-1'],
'__start_line__': 25,
'__end_line__': 27}},
{'awscc': {'region': ['us-east-1'],
'__start_line__': 29,
'__end_line__': 31}}]}
```
see that `backend[0].s3.bucket`? It's a list! The type signature of the handling code is pretty clear that it expects an `str`

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the tflocal init path that reads the Terraform backend configuration and passes the bucket value to botocore, then inspect how hcl2's parsed lists are handled. Reproduce the reported provider.tf case; done means init accepts the scalar backend settings without passing a list as the bucket name.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, terraform
Domain
backend, cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.