Skip to content

Commit

Permalink
Merge pull request #24 from clouddrove/feat/enable-disable-module
Browse files Browse the repository at this point in the history
feat: module enable-disable feature added
  • Loading branch information
d4kverma committed Oct 9, 2023
2 parents 0e29065 + 687d108 commit 2161c01
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ updates:
open-pull-requests-limit: 3

- package-ecosystem: "terraform" # See documentation for possible values
directory: "/_example/privat_ecr" # Location of package manifests
directory: "/_example/private_ecr" # Location of package manifests
schedule:
interval: "weekly"
# Add assignees
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/auto_assignee.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
workflow_dispatch:
jobs:
assignee:
uses: clouddrove/github-shared-workflows/.github/workflows/auto_assignee.yml@master
uses: clouddrove/github-shared-workflows/.github/workflows/auto_assignee.yml@1.0.6
secrets:
GITHUB: ${{ secrets.GITHUB }}
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/changelog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
workflow_dispatch:
jobs:
changelog:
uses: clouddrove/github-shared-workflows/.github/workflows/changelog.yml@master
uses: clouddrove/github-shared-workflows/.github/workflows/changelog.yml@1.0.6
secrets: inherit
with:
branch: 'master'
4 changes: 2 additions & 2 deletions .github/workflows/tf-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ on:
workflow_dispatch:
jobs:
private_ecr:
uses: clouddrove/github-shared-workflows/.github/workflows/tf-checks.yml@master
uses: clouddrove/github-shared-workflows/.github/workflows/tf-checks.yml@1.0.6
with:
working_directory: './_example/private_ecr/'
public_ecr:
uses: clouddrove/github-shared-workflows/.github/workflows/tf-checks.yml@master
uses: clouddrove/github-shared-workflows/.github/workflows/tf-checks.yml@1.0.6
with:
working_directory: './_example/public_ecr/'
2 changes: 1 addition & 1 deletion .github/workflows/tflint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ on:
workflow_dispatch:
jobs:
tf-lint:
uses: clouddrove/github-shared-workflows/.github/workflows/tf-lint.yml@master
uses: clouddrove/github-shared-workflows/.github/workflows/tf-lint.yml@1.0.6
secrets:
GITHUB: ${{ secrets.GITHUB }}
2 changes: 1 addition & 1 deletion .github/workflows/tfsec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
workflow_dispatch:
jobs:
tfsec:
uses: clouddrove/github-shared-workflows/.github/workflows/tfsec.yml@master
uses: clouddrove/github-shared-workflows/.github/workflows/tfsec.yml@1.0.6
secrets: inherit
with:
working_directory: '.'
3 changes: 2 additions & 1 deletion _example/private_ecr/example.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ locals {
## private_ecr module call.
##-----------------------------------------------------------------------------
module "private_ecr" {
source = "./../../"
source = "./../../"

enable_private_ecr = true
name = local.name
environment = local.environment
Expand Down
4 changes: 2 additions & 2 deletions _example/private_ecr/versions.tf
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Terraform version
terraform {
required_version = ">= 1.5.5"
required_version = ">= 1.5.7"

required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 5.13.1"
version = ">= 5.20.0"
}
}
}
4 changes: 2 additions & 2 deletions _example/public_ecr/versions.tf
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Terraform version
terraform {
required_version = ">= 1.5.5"
required_version = ">= 1.5.7"

required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 5.13.1"
version = ">= 5.20.0"
}
}
}
20 changes: 14 additions & 6 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module "labels" {
# Private Repository
################################################################################
resource "aws_ecr_repository" "default" {
count = var.enable_private_ecr ? 1 : 0
count = var.enable && var.enable_private_ecr ? 1 : 0
name = var.use_fullname != "" ? var.use_fullname : module.labels.id
tags = module.labels.tags
image_tag_mutability = var.image_tag_mutability
Expand All @@ -57,7 +57,7 @@ resource "aws_ecr_repository" "default" {
}

resource "aws_ecr_lifecycle_policy" "private" {
count = var.enable_private_ecr ? 1 : 0
count = var.enable && var.enable_private_ecr ? 1 : 0
repository = join("", aws_ecr_repository.default[*].name)

policy = <<EOF
Expand Down Expand Up @@ -96,7 +96,7 @@ EOF
# Public Repository
################################################################################
resource "aws_ecrpublic_repository" "default" {
count = var.enable_public_ecr ? 1 : 0
count = var.enable && var.enable_public_ecr ? 1 : 0

repository_name = var.use_fullname != "" ? var.use_fullname : module.labels.id

Expand All @@ -119,6 +119,7 @@ resource "aws_ecrpublic_repository" "default" {
# Private ECR IAM Policies
################################################################################
data "aws_iam_policy_document" "resource_readonly_access_private" {
count = var.enable ? 1 : 0
statement {
sid = "ReadonlyAccess"
effect = "Allow"
Expand Down Expand Up @@ -153,6 +154,7 @@ data "aws_iam_policy_document" "resource_readonly_access_private" {
}

data "aws_iam_policy_document" "resource_full_access_private" {
count = var.enable ? 1 : 0
statement {
sid = "FullAccess"
effect = "Allow"
Expand All @@ -170,12 +172,13 @@ data "aws_iam_policy_document" "resource_full_access_private" {
}

data "aws_iam_policy_document" "resource_private" {
count = var.enable ? 1 : 0
source_policy_documents = [local.principals_readonly_access_non_empty ? join("", data.aws_iam_policy_document.resource_readonly_access_private[*].json) : join("", data.aws_iam_policy_document.empty[*].json)]
override_policy_documents = [local.principals_full_access_non_empty ? join("", data.aws_iam_policy_document.resource_full_access_private[*].json) : join("", data.aws_iam_policy_document.empty[*].json)]
}

resource "aws_ecr_repository_policy" "private" {
count = local.ecr_need_policy && var.enable_private_ecr ? 1 : 0
count = var.enable && local.ecr_need_policy && var.enable_private_ecr ? 1 : 0
repository = join("", aws_ecr_repository.default[*].name)
policy = join("", data.aws_iam_policy_document.resource_private[*].json)
}
Expand All @@ -184,6 +187,7 @@ resource "aws_ecr_repository_policy" "private" {
# Public ECR IAM Policies
################################################################################
data "aws_iam_policy_document" "resource_readonly_access_public" {
count = var.enable ? 1 : 0
statement {
sid = "ReadonlyAccess"
effect = "Allow"
Expand All @@ -210,6 +214,7 @@ data "aws_iam_policy_document" "resource_readonly_access_public" {
}

data "aws_iam_policy_document" "resource_full_access_public" {
count = var.enable ? 1 : 0
statement {
sid = "FullAccess"
effect = "Allow"
Expand All @@ -228,14 +233,17 @@ data "aws_iam_policy_document" "resource_full_access_public" {


data "aws_iam_policy_document" "resource_public" {
count = var.enable ? 1 : 0
source_policy_documents = [local.principals_readonly_access_non_empty ? join("", data.aws_iam_policy_document.resource_readonly_access_public[*].json) : join("", data.aws_iam_policy_document.empty[*].json)]
override_policy_documents = [local.principals_full_access_non_empty ? join("", data.aws_iam_policy_document.resource_full_access_public[*].json) : join("", data.aws_iam_policy_document.empty[*].json)]
}

resource "aws_ecr_repository_policy" "public" {
count = local.ecr_need_policy && var.enable_public_ecr ? 1 : 0
count = var.enable && local.ecr_need_policy && var.enable_public_ecr ? 1 : 0
repository = join("", aws_ecrpublic_repository.default[*].repository_name)
policy = join("", data.aws_iam_policy_document.resource_public[*].json)
}

data "aws_iam_policy_document" "empty" {}
data "aws_iam_policy_document" "empty" {
count = var.enable ? 1 : 0
}
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ variable "tags" {
description = "Additional tags (e.g. map(`BusinessUnit`,`XYZ`)."
}

variable "enable" {
type = bool
default = true
description = "Set to false to prevent the module from creating any resources."
}

variable "enable_private_ecr" {
type = bool
default = false
Expand Down
4 changes: 2 additions & 2 deletions versions.tf
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Terraform version
terraform {
required_version = ">= 1.5.5"
required_version = ">= 1.5.7"

required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 5.13.1"
version = ">= 5.20.0"
}
}
}

0 comments on commit 2161c01

Please sign in to comment.