@@ -409,7 +409,6 @@ def test_versioned_endpoints(monkeypatch, provider_version):
409409 secret_key = "test"
410410 skip_credentials_validation = true
411411 skip_metadata_api_check = true
412- skip_requesting_account_id = true
413412 endpoints {
414413 sns = "http://localhost:4566"
415414 }
@@ -444,6 +443,71 @@ def test_versioned_endpoints(monkeypatch, provider_version):
444443 assert "iotevents" not in endpoints
445444
446445
446+ @pytest .mark .parametrize ("endpoint_host" , ["" , "test-host" ])
447+ def test_subdomain_endpoints (monkeypatch , endpoint_host ):
448+ # we are using the `AWS_ENDPOINT_URL`, but setting the `LOCALSTACK_HOSTNAME` and `EDGE_PORT` (both deprecated)
449+ # would have the same behavior
450+ aws_endpoint_url = f"http://{ endpoint_host } :4566" if endpoint_host else ""
451+ monkeypatch .setenv ("AWS_ENDPOINT_URL" , aws_endpoint_url )
452+ bucket_name = f"bucket-{ short_uid ()} "
453+ config = """
454+ terraform {
455+ required_providers {
456+ aws = {
457+ source = "hashicorp/aws"
458+ version = ">= 6.23"
459+ }
460+ }
461+ }
462+
463+ provider "aws" {
464+ region = "us-east-1"
465+ access_key = "test"
466+ secret_key = "test"
467+ skip_credentials_validation = true
468+ skip_metadata_api_check = true
469+ }
470+
471+ resource "aws_s3_bucket" "example" {
472+ name = "%s"
473+ }
474+ """ % bucket_name
475+
476+ with tempfile .TemporaryDirectory (delete = True ) as temp_dir :
477+ with open (os .path .join (temp_dir , "test.tf" ), "w" ) as f :
478+ f .write (config )
479+
480+ # we need the `terraform init` command to create a lock file, so it cannot be a `DRY_RUN`
481+ run ([TFLOCAL_BIN , "init" ], cwd = temp_dir , env = dict (os .environ ))
482+ monkeypatch .setenv ("DRY_RUN" , "1" )
483+ run ([TFLOCAL_BIN , "apply" , "-auto-approve" ], cwd = temp_dir , env = dict (os .environ ))
484+
485+ override_file = os .path .join (temp_dir , "localstack_providers_override.tf" )
486+ assert check_override_file_exists (override_file )
487+
488+ with open (override_file , "r" ) as fp :
489+ result = hcl2 .load (fp )
490+ endpoints = result ["provider" ][0 ]["aws" ]["endpoints" ][0 ]
491+ assert "s3control" in endpoints
492+ assert "mwaa" in endpoints
493+
494+ if aws_endpoint_url == "" :
495+ # we assert that if the `LOCALSTACK_HOSTNAME` isn't set, the default endpooint value is `localhost`
496+ assert endpoints ["sns" ] == "http://localhost:4566"
497+ # the MWAA endpoint needs to be "subdomain resolvable", so we override it with the default localhost
498+ # localstack hostname
499+ assert endpoints ["mwaa" ] == "http://mwaa.localhost.localstack.cloud:4566"
500+ # s3 control will set the account id of the requested as a host prefix, so we also need a subdomain
501+ # compatible host
502+ assert endpoints ["s3control" ] == "http://localhost.localstack.cloud:4566"
503+ else :
504+ # if the user is manipulating the `LOCALSTACK_HOSTNAME`, they are responsible to make sure that the
505+ # domain they set is subdomain compatible, so we respect their configuration
506+ assert endpoints ["sns" ] == f"http://{ endpoint_host } :4566"
507+ assert endpoints ["mwaa" ] == f"http://mwaa.{ endpoint_host } :4566"
508+ assert endpoints ["s3control" ] == f"http://{ endpoint_host } :4566"
509+
510+
447511def test_dry_run (monkeypatch ):
448512 monkeypatch .setenv ("DRY_RUN" , "1" )
449513 state_bucket = "tf-state-dry-run"
0 commit comments