hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io/
Other
42.34k stars 9.49k forks source link

Feature Request: Set provider configs more easier for different stages #26142

Open dfang opened 4 years ago

dfang commented 4 years ago

Current Terraform Version

$ tf version
Terraform v0.12.29
+ provider.aws v3.5.0

Your version of Terraform is out of date! The latest version
is 0.13.2. You can update by downloading from https://www.terraform.io/downloads.html

Use-cases

for aws, if you use localstack for local testing in local or dev stage. you need to set various endpoints for aws provider like this.

and you want to use real aws for endpoints for other stages. eg. you have stages, dev、staging、prod 。

so you want to use localstack in dev stage, how do you set this ?

for set different config backends for different stages, this would help, but i don't find a effective way to implement different provider configs for different stages.

in simple terms, what i want to achieve is (pseudo code):

provider "aws" {
  if is_dev_stage or is_local_stage {
    s3_force_path_style         = true
    secret_key                  = "mock_secret_key"
    skip_credentials_validation = true
    skip_metadata_api_check     = true
    skip_requesting_account_id  = true

    endpoints {
        apigateway     = "http://localhost:4567"
        cloudformation = "http://localhost:4581"
        cloudwatch     = "http://localhost:4582"
        dynamodb       = "http://localhost:4569"
        es             = "http://localhost:4578"
        firehose       = "http://localhost:4573"
        iam            = "http://localhost:4593"
        kinesis        = "http://localhost:4568"
        lambda         = "http://localhost:4574"
        route53        = "http://localhost:4580"
        redshift       = "http://localhost:4577"
        s3             = "http://localhost:4572"
        secretsmanager = "http://localhost:4584"
        ses            = "http://localhost:4579"
        sns            = "http://localhost:4575"
        sqs            = "http://localhost:4576"
        ssm            = "http://localhost:4583"
        stepfunctions  = "http://localhost:4585"
        sts            = "http://localhost:4592"
    }
  }
}

Attempted Solutions

tree environment/
environment/
├── dev
├── prod
└── staging

tried to set providers configs in different stages, but don't know how to reference it in main.tf.

for resource, you can use provider alias reference provider like provider = aws.dev, but how for provider config ?

Proposal

References

https://github.com/terraform-providers/terraform-provider-aws/issues/15050

dfang commented 3 years ago

@bflad I want to implement this by myself, but i have no clue. can you give some hint?

thanks !