Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provider: Make region optional #23384

Merged
merged 2 commits into from
Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/23384.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
provider: Makes `region` an optional parameter to allow sourcing from shared config files and IMDS
```
14 changes: 6 additions & 8 deletions internal/conns/conns.go
Original file line number Diff line number Diff line change
Expand Up @@ -1187,14 +1187,6 @@ func (client *AWSClient) RegionalHostname(prefix string) string {

// Client configures and returns a fully initialized AWSClient
func (c *Config) Client() (interface{}, error) {
// Get the auth and region. This can fail if keys/regions were not
// specified and we're attempting to use the environment.
if !c.SkipRegionValidation {
if err := awsbase.ValidateRegion(c.Region); err != nil {
return nil, err
}
}

awsbaseConfig := awsbase.Config{
AccessKey: c.AccessKey,
APNInfo: StdUserAgentProducts(c.TerraformVersion),
Expand Down Expand Up @@ -1247,6 +1239,12 @@ func (c *Config) Client() (interface{}, error) {
return nil, fmt.Errorf("error configuring Terraform AWS Provider: %w", err)
}

if !c.SkipRegionValidation {
if err := awsbase.ValidateRegion(cfg.Region); err != nil {
return nil, err
}
}

sess, err := awsbasev1.GetSession(&cfg, &awsbaseConfig)
if err != nil {
return nil, fmt.Errorf("error creating AWS SDK v1 session: %w", err)
Expand Down
7 changes: 1 addition & 6 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,14 +288,9 @@ func Provider() *schema.Provider {
},
"region": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"AWS_REGION",
"AWS_DEFAULT_REGION",
}, nil),
Optional: true,
Description: "The region where AWS operations will take place. Examples\n" +
"are us-east-1, us-west-2, etc.", // lintignore:AWSAT003,
InputDefault: "us-east-1", // lintignore:AWSAT003
},
"s3_force_path_style": {
Type: schema.TypeBool,
Expand Down
5 changes: 4 additions & 1 deletion website/docs/index.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,10 @@ In addition to [generic `provider` arguments](https://www.terraform.io/docs/conf
and the shared configuration parameter `max_attempts`.
* `profile` - (Optional) AWS profile name as set in the shared configuration and credentials files.
Can also be set using either the environment variables `AWS_PROFILE` or `AWS_DEFAULT_PROFILE`.
* `region` - (Optional) AWS region. Can also be set using either the `AWS_REGION` or `AWS_DEFAULT_REGION` environment variables, or via the shared configuration and credentials files if `profile` is used.
* `region` - (Optional) The AWS region where the provider will operate. The region must be set.
Can also be set with either the `AWS_REGION` or `AWS_DEFAULT_REGION` environment variables,
or via a shared config file parameter `region` if `profile` is used.
If credentials are retrieved from the EC2 Instance Metadata Service, the region can also be retrieved from the metadata.
Comment on lines +284 to +287
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* `region` - (Optional) The AWS region where the provider will operate. The region must be set.
Can also be set with either the `AWS_REGION` or `AWS_DEFAULT_REGION` environment variables,
or via a shared config file parameter `region` if `profile` is used.
If credentials are retrieved from the EC2 Instance Metadata Service, the region can also be retrieved from the metadata.
* `region` - (Optional) The AWS region where the provider will operate. The region must be set. Can also be set with either the `AWS_REGION` or `AWS_DEFAULT_REGION` environment variables, or via a shared config file parameter `region` if `profile` is used. If credentials are retrieved from the EC2 Instance Metadata Service, the region can also be retrieved from the metadata.

* `s3_force_path_style` - (Optional, **Deprecated**) Whether to enable the request to use path-style addressing, i.e., `https://s3.amazonaws.com/BUCKET/KEY`. By default, the S3 client will use virtual hosted bucket addressing, `https://BUCKET.s3.amazonaws.com/KEY`, when possible. Specific to the Amazon S3 service.
* `s3_use_path_style` - (Optional) Whether to enable the request to use path-style addressing, i.e., `https://s3.amazonaws.com/BUCKET/KEY`. By default, the S3 client will use virtual hosted bucket addressing, `https://BUCKET.s3.amazonaws.com/KEY`, when possible. Specific to the Amazon S3 service.
* `secret_key` - (Optional) AWS secret key. Can also be set with the `AWS_SECRET_ACCESS_KEY` environment variable, or via a shared configuration and credentials files if `profile` is used. See also `access_key`.
Expand Down