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

Enhancement | Remove empty provider configuration block warning #16

Merged
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ default_language_version:
python: python3

repos:
- repo: git://github.com/pre-commit/pre-commit-hooks
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
hooks:
- id: check-json
Expand All @@ -17,7 +17,7 @@ repos:
args:
- --markdown-linebreak-ext=md

- repo: git://github.com/antonbabenko/pre-commit-terraform
- repo: https://github.com/antonbabenko/pre-commit-terraform
rev: v1.43.0
hooks:
- id: terraform_fmt
Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
SHELL := /bin/bash
MAKEFILE_PATH := ./Makefile
MAKEFILES_DIR := ./@bin/makefiles
MAKEFILES_VER := v0.1.33
MAKEFILES_VER := v0.2.1
PROJECT_SHORT := bb

help:
Expand All @@ -20,6 +20,6 @@ init-makefiles: ## initialize makefiles

-include ${MAKEFILES_DIR}/circleci/circleci.mk
-include ${MAKEFILES_DIR}/release-mgmt/release.mk
-include ${MAKEFILES_DIR}/terraform14/terraform14-root-context.mk
-include ${MAKEFILES_DIR}/terraform14/terraform14.mk
-include ${MAKEFILES_DIR}/terratest14/terratest14.mk
-include ${MAKEFILES_DIR}/terraform1/terraform1-root-context.mk
-include ${MAKEFILES_DIR}/terraform1/terraform1.mk
-include ${MAKEFILES_DIR}/terratest1/terratest1.mk
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,11 @@ No modules.
#
provider "aws" {
region = "us-east-1
alias = "main_region"
}

provider "aws" {
region = "us-west-1"
alias = "secondary_region"
alias = "secondary"
}

# The following creates a Terraform State Backend with Bucket Replication enabled
Expand All @@ -140,8 +139,8 @@ module "terraform_state_backend_with_replication" {
bucket_replication_enabled = true

providers = {
aws.main_region = aws.main_region
aws.secondary_region = aws.secondary_region
aws.primary = aws
aws.secondary = aws.secondary
}
}

Expand All @@ -157,10 +156,10 @@ module "terraform_state_backend" {
# By default replication is disabled but it shows below for the sake of the example
bucket_replication_enabled = false

# Notice that even though replication is not enabled, we still need to pass a secondary_region provider
# Notice that even though replication is not enabled, we still need to pass a secondary provider
providers = {
aws.main_region = aws.main_region
aws.secondary_region = aws.main_region
aws.primary = aws
aws.secondary = aws.secondary
}
}
```
Expand Down
10 changes: 5 additions & 5 deletions bucket_replication.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
resource "aws_s3_bucket" "replication_bucket" {
count = var.bucket_replication_enabled ? 1 : 0

provider = aws.secondary_region
provider = aws.secondary
bucket = format("%s-%s-%s-replica", var.namespace, var.stage, var.name)

versioning {
Expand All @@ -25,7 +25,7 @@ resource "aws_s3_bucket" "replication_bucket" {
resource "aws_iam_role" "bucket_replication" {
count = var.bucket_replication_enabled ? 1 : 0

provider = aws.main_region
provider = aws.primary
name = format("%s-%s-%s-bucket-replication-module", var.namespace, var.stage, var.name)
assume_role_policy = <<POLICY
{
Expand All @@ -47,7 +47,7 @@ POLICY
resource "aws_iam_policy" "bucket_replication" {
count = var.bucket_replication_enabled ? 1 : 0

provider = aws.main_region
provider = aws.primary
name = format("%s-%s-%s-bucket-replication-module", var.namespace, var.stage, var.name)
policy = <<POLICY
{
Expand Down Expand Up @@ -91,7 +91,7 @@ POLICY
resource "aws_iam_policy_attachment" "bucket_replication" {
count = var.bucket_replication_enabled ? 1 : 0

provider = aws.main_region
provider = aws.primary
name = format("%s-%s-%s-role-policy-attachment", var.namespace, var.stage, var.name)
roles = [aws_iam_role.bucket_replication[0].name]
policy_arn = aws_iam_policy.bucket_replication[0].arn
Expand All @@ -101,7 +101,7 @@ resource "aws_iam_policy_attachment" "bucket_replication" {
resource "aws_s3_bucket_policy" "bucket_replication" {
count = var.bucket_replication_enabled && var.enforce_ssl_requests ? 1 : 0

provider = aws.secondary_region
provider = aws.secondary
bucket = aws_s3_bucket.replication_bucket[0].id
policy = <<POLICY
{
Expand Down
11 changes: 0 additions & 11 deletions config.tf

This file was deleted.

2 changes: 1 addition & 1 deletion examples/s3-tfstate-backend-crr-ssl/config.tf
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ variable "profile" {
# Backend Config (partial) #
#=============================#
terraform {
required_version = ">= 0.14.2"
required_version = ">= 1.0.9"

required_providers {
aws = "~> 3.0"
Expand Down
4 changes: 2 additions & 2 deletions examples/s3-tfstate-backend-crr-ssl/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module "terraform_state_backend" {
tags = var.tags

providers = {
aws.main_region = aws.main_region
aws.secondary_region = aws.secondary_region
aws.primary = aws.main_region
aws.secondary = aws.secondary_region
}
}
2 changes: 1 addition & 1 deletion examples/s3-tfstate-backend-crr-vpce/config.tf
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ variable "profile" {
# Backend Config (partial) #
#=============================#
terraform {
required_version = ">= 0.14.2"
required_version = ">= 1.0.9"

required_providers {
aws = "~> 3.0"
Expand Down
4 changes: 2 additions & 2 deletions examples/s3-tfstate-backend-crr-vpce/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ module "terraform_state_backend" {
tags = var.tags

providers = {
aws.main_region = aws.main_region
aws.secondary_region = aws.secondary_region
aws.primary = aws.main_region
aws.secondary = aws.secondary_region
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/s3-tfstate-backend-crr/config.tf
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ variable "profile" {
# Backend Config (partial) #
#=============================#
terraform {
required_version = ">= 0.14.2"
required_version = ">= 1.0.9"

required_providers {
aws = "~> 3.0"
Expand Down
4 changes: 2 additions & 2 deletions examples/s3-tfstate-backend-crr/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module "terraform_state_backend" {
tags = var.tags

providers = {
aws.main_region = aws.main_region
aws.secondary_region = aws.secondary_region
aws.primary = aws.main_region
aws.secondary = aws.secondary_region
}
}
2 changes: 1 addition & 1 deletion examples/s3-tfstate-backend/config.tf
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ variable "profile" {
# Backend Config (partial) #
#=============================#
terraform {
required_version = ">= 0.14.2"
required_version = ">= 1.0.9"

required_providers {
aws = "~> 3.0"
Expand Down
4 changes: 2 additions & 2 deletions examples/s3-tfstate-backend/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module "terraform_state_backend" {
tags = var.tags

providers = {
aws.main_region = aws.main_region
aws.secondary_region = aws.secondary_region
aws.primary = aws.main_region
aws.secondary = aws.secondary_region
}
}
8 changes: 4 additions & 4 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
resource "aws_s3_bucket" "default" {
provider = aws.main_region
provider = aws.primary

bucket = format("%s-%s-%s", var.namespace, var.stage, var.name)
acl = var.acl
Expand Down Expand Up @@ -53,7 +53,7 @@ resource "aws_s3_bucket" "default" {
}

resource "aws_s3_bucket_public_access_block" "default" {
provider = aws.main_region
provider = aws.primary
bucket = aws_s3_bucket.default.id
block_public_acls = var.block_public_acls
ignore_public_acls = var.ignore_public_acls
Expand All @@ -71,7 +71,7 @@ resource "time_sleep" "wait_30_secs" {
resource "aws_dynamodb_table" "with_server_side_encryption" {
count = var.enable_server_side_encryption == "true" ? 1 : 0

provider = aws.main_region
provider = aws.primary
name = format("%s-%s-%s", var.namespace, var.stage, var.name)
read_capacity = var.read_capacity
write_capacity = var.write_capacity
Expand Down Expand Up @@ -102,7 +102,7 @@ resource "aws_dynamodb_table" "with_server_side_encryption" {
resource "aws_dynamodb_table" "without_server_side_encryption" {
count = var.enable_server_side_encryption == "true" ? 0 : 1

provider = aws.main_region
provider = aws.primary
name = format("%s-%s-%s", var.namespace, var.stage, var.name)
read_capacity = var.read_capacity
write_capacity = var.write_capacity
Expand Down
8 changes: 4 additions & 4 deletions policy.tf
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
resource "aws_s3_bucket_policy" "default" {
count = var.enforce_ssl_requests ? 1 : 0

provider = aws.main_region
provider = aws.primary
bucket = aws_s3_bucket.default.id
policy = data.aws_iam_policy_document.default-ssl.json
depends_on = [aws_s3_bucket_public_access_block.default, time_sleep.wait_30_secs]
}

data "aws_iam_policy_document" "default-ssl" {
provider = aws.main_region
provider = aws.primary

#
# 1stg Policy Statement
Expand Down Expand Up @@ -43,14 +43,14 @@ data "aws_iam_policy_document" "default-ssl" {
resource "aws_s3_bucket_policy" "default-ssl-vpc" {
count = var.enforce_ssl_requests && var.enforce_vpc_requests && var.vpc_ids_list != [] ? 1 : 0

provider = aws.main_region
provider = aws.primary
bucket = aws_s3_bucket.default.id
policy = data.aws_iam_policy_document.default-ssl-vpc.json
depends_on = [aws_s3_bucket_public_access_block.default, time_sleep.wait_30_secs]
}

data "aws_iam_policy_document" "default-ssl-vpc" {
provider = aws.main_region
provider = aws.primary

#
# 1stg Policy Statement
Expand Down
9 changes: 6 additions & 3 deletions versions.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@

terraform {
required_version = ">= 0.13.2"
required_version = ">= 1.0.9"

required_providers {
aws = "~> 3.0"
aws = {
Copy link
Contributor

@diego-ojeda-binbash diego-ojeda-binbash Apr 13, 2022

Choose a reason for hiding this comment

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

@angelofenoglio So the fix is only this piece here, right?
I mean, to use the module you need to pass these providers:

providers = {
    aws           = aws
    aws.secondary = aws.secondary
}

So, I'm wondering if it'd be better to rather do it this way:

providers = {
    aws.primary   = aws
    aws.secondary = aws.secondary
}

That is, in order to follow the "Explicit is better than implicit." aphorism. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems reasonable, I'll make these changes then!

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, is that right? I would've thought that the following is implicit:

resource "foo" "bar" {
    ...
    provider = aws
    ...
}

And thus it does not need to be explicitly defined.

source = "hashicorp/aws"
version = "~> 3.0"
configuration_aliases = [aws.primary, aws.secondary]
}
}
}