From 8c62b1f750ceb484c257c4d4d7fc476c0e7720f1 Mon Sep 17 00:00:00 2001 From: Mourad T Date: Mon, 29 Apr 2019 16:28:05 +0100 Subject: [PATCH 1/3] add provider block in module call, add providers, comment out aws_region --- README.md | 9 ++++++++- examples/ecr.tf | 8 ++++++-- examples/main.tf | 18 +++++++++++++++--- main.tf | 9 +-------- variables.tf | 8 ++++---- 5 files changed, 34 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 887460b..32d096d 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,12 @@ This terraform module will create an ECR repository and IAM credentials to acces ## Usage +**This module will create the resources in the region of the providers specified in the `providers` input.** + +**Be sure to create the relevant providers, see example/main.tf** + +**From module version 3.2, this replaces the use of the `aws_region`.** + ```hcl module "best_team_ecr_credentials" { source = "github.com/ministryofjustice/cloud-platform-terraform-ecr-credentials" @@ -24,7 +30,8 @@ Note: From version 3.0 of this module, The AWS region will default to eu-west-2 | team_name | name of the team creating the credentials | string | - | yes | | enable_policy | Sets a ECR lifecycle policy to delete every image after count 40 | string | true | yes | aws_region | region into which the resource will be created | string | eu-west-2 | no -| +| providers | provider creating resources | arrays of string | default provider | no + ## Outputs diff --git a/examples/ecr.tf b/examples/ecr.tf index cb6fcfd..ef3d618 100644 --- a/examples/ecr.tf +++ b/examples/ecr.tf @@ -5,10 +5,14 @@ * */ module "example_team_ecr_credentials" { - source = "github.com/ministryofjustice/cloud-platform-terraform-ecr-credentials?ref=3.1" + source = "github.com/ministryofjustice/cloud-platform-terraform-ecr-credentials?ref=3.2" repo_name = "example-module" team_name = "example-team" - aws_region = "eu-west-2" # this overwrite the region from the provider defined above. + # aws_region = "eu-west-2" # This input is deprecated from version 3.2 of this module + + providers = { + aws = "aws.london" + } } resource "kubernetes_secret" "example_team_ecr_credentials" { diff --git a/examples/main.tf b/examples/main.tf index 157da74..d0cc60c 100644 --- a/examples/main.tf +++ b/examples/main.tf @@ -1,7 +1,19 @@ -terraform { - backend "s3" {} -} +# terraform { +# backend "s3" {} +# } provider "aws" { region = "eu-west-1" } + +# To be use in case the resources need to be created in London +provider "aws" { + alias = "london" + region = "eu-west-2" +} + +# To be use in case the resources need to be created in Ireland +provider "aws" { + alias = "ireland" + region = "eu-west-1" +} \ No newline at end of file diff --git a/main.tf b/main.tf index 6d3f545..afc6892 100644 --- a/main.tf +++ b/main.tf @@ -1,18 +1,11 @@ data "aws_caller_identity" "current" {} data "aws_region" "current" {} -provider "aws" { - alias = "destination" - region = "${var.aws_region}" -} - resource "aws_ecr_repository" "repo" { - provider = "aws.destination" name = "${var.team_name}/${var.repo_name}" } resource "aws_ecr_lifecycle_policy" "lifecycle_policy" { - provider = "aws.destination" count = "${var.enable_policy ? 1 : 0}" repository = "${aws_ecr_repository.repo.name}" @@ -76,7 +69,7 @@ data "aws_iam_policy_document" "policy" { ] resources = [ - "arn:aws:ecr:${var.aws_region}:${data.aws_caller_identity.current.account_id}:repository/${var.team_name}/*", + "arn:aws:ecr:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:repository/${var.team_name}/*", ] } } diff --git a/variables.tf b/variables.tf index 3303d31..106d6be 100644 --- a/variables.tf +++ b/variables.tf @@ -7,7 +7,7 @@ variable "enable_policy" { default = true } -variable "aws_region" { - description = "Region into which the resource will be created." - default = "eu-west-2" -} +# variable "aws_region" { +# description = "Region into which the resource will be created." +# default = "eu-west-2" +# } From f5f9515c9f4573a65de322a9928f50a43dccc1ce Mon Sep 17 00:00:00 2001 From: Mourad T Date: Mon, 29 Apr 2019 16:31:49 +0100 Subject: [PATCH 2/3] add providers example in README --- README.md | 6 +++++- variables.tf | 8 ++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 32d096d..995148d 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,11 @@ module "best_team_ecr_credentials" { repo_name = "test-repo" team_name = "best-team" - aws_region= "eu-west-2" + + # This is a new input + providers = { + aws = "aws.london" + } } ``` diff --git a/variables.tf b/variables.tf index 106d6be..3303d31 100644 --- a/variables.tf +++ b/variables.tf @@ -7,7 +7,7 @@ variable "enable_policy" { default = true } -# variable "aws_region" { -# description = "Region into which the resource will be created." -# default = "eu-west-2" -# } +variable "aws_region" { + description = "Region into which the resource will be created." + default = "eu-west-2" +} From f147361ac0676e9c52b1d59a5db5ef86bef2b9dd Mon Sep 17 00:00:00 2001 From: Mourad T Date: Mon, 29 Apr 2019 16:35:46 +0100 Subject: [PATCH 3/3] reformatted with tf fmt --- examples/ecr.tf | 7 ++++--- examples/main.tf | 6 +++--- main.tf | 2 +- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/examples/ecr.tf b/examples/ecr.tf index ef3d618..51b42f1 100644 --- a/examples/ecr.tf +++ b/examples/ecr.tf @@ -5,9 +5,10 @@ * */ module "example_team_ecr_credentials" { - source = "github.com/ministryofjustice/cloud-platform-terraform-ecr-credentials?ref=3.2" - repo_name = "example-module" - team_name = "example-team" + source = "github.com/ministryofjustice/cloud-platform-terraform-ecr-credentials?ref=3.2" + repo_name = "example-module" + team_name = "example-team" + # aws_region = "eu-west-2" # This input is deprecated from version 3.2 of this module providers = { diff --git a/examples/main.tf b/examples/main.tf index d0cc60c..8d519ea 100644 --- a/examples/main.tf +++ b/examples/main.tf @@ -8,12 +8,12 @@ provider "aws" { # To be use in case the resources need to be created in London provider "aws" { - alias = "london" + alias = "london" region = "eu-west-2" } # To be use in case the resources need to be created in Ireland provider "aws" { - alias = "ireland" + alias = "ireland" region = "eu-west-1" -} \ No newline at end of file +} diff --git a/main.tf b/main.tf index afc6892..0007b7d 100644 --- a/main.tf +++ b/main.tf @@ -2,7 +2,7 @@ data "aws_caller_identity" "current" {} data "aws_region" "current" {} resource "aws_ecr_repository" "repo" { - name = "${var.team_name}/${var.repo_name}" + name = "${var.team_name}/${var.repo_name}" } resource "aws_ecr_lifecycle_policy" "lifecycle_policy" {