From 2b4062e5caa4cf4139bfb51b41f92e378e67ce46 Mon Sep 17 00:00:00 2001 From: yadavprakash Date: Tue, 29 Jun 2021 15:47:22 +0530 Subject: [PATCH 1/4] add github-action,pre-coomit,readme.yml --- .github/workflows/readme.yml | 54 ++++++++++++ .github/workflows/terraform.yml | 116 ++++++++++++++++++++++++++ .github/workflows/terratest.yml | 42 ++++++++++ .gitignore | 2 +- .pre-commit-config.yaml | 21 +++++ LICENSE | 21 +++++ Makefile | 2 + README.md | 0 README.yaml | 110 ++++++++++++++++++++++++ _example/event_bus_archive/example.tf | 1 - _example/event_rule_target/example.tf | 1 - _example/event_rule_target/iam.tf | 2 +- main.tf | 5 +- variable.tf | 15 +++- versions.tf | 11 +++ 15 files changed, 396 insertions(+), 7 deletions(-) create mode 100644 .github/workflows/readme.yml create mode 100644 .github/workflows/terraform.yml create mode 100644 .github/workflows/terratest.yml create mode 100644 .pre-commit-config.yaml create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README.md create mode 100644 README.yaml create mode 100644 versions.tf diff --git a/.github/workflows/readme.yml b/.github/workflows/readme.yml new file mode 100644 index 0000000..3edbae3 --- /dev/null +++ b/.github/workflows/readme.yml @@ -0,0 +1,54 @@ +name: 'Create README.md file' +on: + push: + branches: + - master + +jobs: + readme-create: + name: 'readme-create' + runs-on: ubuntu-latest + steps: + - name: 'Checkout' + uses: actions/checkout@master + + - name: Set up Python 3.7. + uses: actions/setup-python@v2 + with: + python-version: '3.x' + + - name: 'create readme' + uses: 'clouddrove/github-actions@v7.0' + with: + actions_subcommand: 'readme' + github_token: '${{ secrets.GITHUB}}' + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN}} + + + - name: pre-commit check errors + uses: pre-commit/action@v2.0.0 + continue-on-error: true + + - name: pre-commit fix erros + uses: pre-commit/action@v2.0.0 + continue-on-error: true + + - name: 'push readme' + uses: 'clouddrove/github-actions@v7.0' + continue-on-error: true + with: + actions_subcommand: 'push' + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN}} + + - name: 'Slack Notification' + uses: clouddrove/action-slack@v2 + with: + status: ${{ job.status }} + fields: repo,author + author_name: 'CloudDrove' + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # required + SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_TERRAFORM }} # required + if: always() diff --git a/.github/workflows/terraform.yml b/.github/workflows/terraform.yml new file mode 100644 index 0000000..865cebb --- /dev/null +++ b/.github/workflows/terraform.yml @@ -0,0 +1,116 @@ +name: 'Terraform GitHub Actions' +on: + pull_request: + branches: + - master + +jobs: + fmt: + name: 'terraform fmt' + runs-on: ubuntu-latest + steps: + - name: 'Checkout' + uses: actions/checkout@v2.3.4 + + - name: 'Terraform Format' + uses: 'clouddrove/github-actions@v7.0' + with: + actions_subcommand: 'fmt' + - name: 'Terraform Format' + uses: 'clouddrove/github-actions@v7.0' + with: + actions_subcommand: 'fmt' + + event_bus_archive: + name: 'event_bus_archive' + runs-on: ubuntu-latest + steps: + - name: 'Checkout' + uses: actions/checkout@v2.3.4 + + - name: 'Configure AWS Credentials' + uses: clouddrove/configure-aws-credentials@v1 + with: + aws-access-key-id: ${{ secrets.TEST_AWS_ACCESS_KEY }} + aws-secret-access-key: ${{ secrets.TEST_AWS_ACCESS_SECRET_KEY }} + aws-region: us-east-2 + + - name: 'Terraform init' + uses: 'clouddrove/github-actions@v7.0' + with: + actions_subcommand: 'init' + tf_actions_working_dir: ./_example/event_bus_archive + + - name: 'Terraform validate for event_bus_archive' + uses: 'clouddrove/github-actions@v7.0' + with: + actions_subcommand: 'validate' + tf_actions_working_dir: ./_example/event_bus_archive + + - name: 'Terraform plan for event_bus_archive' + uses: 'clouddrove/github-actions@v7.0' + with: + actions_subcommand: 'plan' + tf_actions_working_dir: ./_example/event_bus_archive + + event_rule_target: + name: 'secure_example' + runs-on: ubuntu-latest + steps: + - name: 'Checkout' + uses: actions/checkout@v2.3.4 + + - name: 'Configure AWS Credentials' + uses: clouddrove/configure-aws-credentials@v1 + with: + aws-access-key-id: ${{ secrets.TEST_AWS_ACCESS_KEY }} + aws-secret-access-key: ${{ secrets.TEST_AWS_ACCESS_SECRET_KEY }} + aws-region: us-east-2 + + - name: 'Terraform init' + uses: 'clouddrove/github-actions@v7.0' + with: + actions_subcommand: 'init' + tf_actions_working_dir: ./_example/event_rule_target + + - name: 'Terraform validate for event_rule_target' + uses: 'clouddrove/github-actions@v7.0' + with: + actions_subcommand: 'validate' + tf_actions_working_dir: ./_example/event_rule_target + + - name: 'Terraform plan for event_rule_target' + uses: 'clouddrove/github-actions@v7.0' + with: + actions_subcommand: 'plan' + tf_actions_working_dir: ./_example/event_rule_target + + pre-commit: + name: 'Pre-Commit' + needs: + - fmt + - event_bus_archive + - event_rule_target + runs-on: ubuntu-latest + steps: + - name: 'Checkout' + uses: actions/checkout@v2.3.4 + + - name: 'Install Tflint' + run: | + curl https://raw.githubusercontent.com/terraform-linters/tflint/master/install_linux.sh | bash + + - name: 'Pre-Commit 🔎' + uses: pre-commit/action@v2.0.3 + continue-on-error: true + + - name: 'Slack Notification' + uses: clouddrove/action-slack@v2 + with: + status: ${{ job.status }} + fields: repo,author + author_name: 'CloudDrove' + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # required + SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_TERRAFORM }} # required + if: always() diff --git a/.github/workflows/terratest.yml b/.github/workflows/terratest.yml new file mode 100644 index 0000000..d39bc81 --- /dev/null +++ b/.github/workflows/terratest.yml @@ -0,0 +1,42 @@ +name: 'Terratest GitHub Actions' +on: + pull_request: + branches: + - master + types: [labeled] + +jobs: + terraform: + name: 'Terraform' + runs-on: ubuntu-latest + steps: + + - name: 'Checkout' + uses: actions/checkout@master + + - name: Configure AWS Credentials + uses: clouddrove/configure-aws-credentials@v1 + with: + aws-access-key-id: ${{ secrets.TEST_AWS_ACCESS_KEY }} + aws-secret-access-key: ${{ secrets.TEST_AWS_ACCESS_SECRET_KEY }} + aws-region: us-east-2 + + - name: 'Terratest' + if: ${{ github.event.label.name == 'terratest' }} + uses: 'clouddrove/github-actions@v4.0' + with: + actions_subcommand: 'terratest' + tf_actions_working_dir: '_test' + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: 'Slack Notification' + uses: clouddrove/action-slack@v2 + with: + status: ${{ job.status }} + fields: repo,author + author_name: 'CloudDrove' + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # required + SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_TERRAFORM }} # required + if: always() diff --git a/.gitignore b/.gitignore index dd937fd..98d0c1b 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,4 @@ *.iml _example/.terraform.tfstate.lock.info *.terrafrom.lock.hcl -.terraform.lock.hcl \ No newline at end of file +.terraform.lock.hcl diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..9e713a5 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,21 @@ +repos: + + - repo: https://github.com/gruntwork-io/pre-commit + rev: v0.1.12 # Get the latest from: https://github.com/gruntwork-io/pre-commit/releases + hooks: + - id: terraform-fmt + - id: shellcheck + - id: tflint + + - repo: git://github.com/pre-commit/pre-commit-hooks + rev: v4.0.1 # Use the ref you want to point at + hooks: + - id: end-of-file-fixer + - id: trailing-whitespace + - id: mixed-line-ending + - id: check-byte-order-marker + - id: check-executables-have-shebangs + - id: check-merge-conflict + - id: debug-statements + - id: check-yaml + - id: check-added-large-files diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..807110a --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 Cloud Drove + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..073c5aa --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +export GENIE_PATH ?= $(shell 'pwd')/../../../genie +include $(GENIE_PATH)/Makefile diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/README.yaml b/README.yaml new file mode 100644 index 0000000..0831490 --- /dev/null +++ b/README.yaml @@ -0,0 +1,110 @@ +--- +# +# This is the canonical configuration for the `README.md` +# Run `make readme` to rebuild the `README.md` +# + + +# Name of this project +name: Terraform AWS eventbridge + +# License of this project +license: "MIT" + +# Canonical GitHub repo +github_repo: clouddrove/terraform-aws-eventbridge + +# Badges to display +badges: + - name: "Terraform" + image: "https://img.shields.io/badge/Terraform-v0.15-green" + url: "https://www.terraform.io" + - name: "Licence" + image: "https://img.shields.io/badge/License-MIT-blue.svg" + url: "LICENSE.md" + +# description of this project +# yamllint disable rule:line-length + +description: |- + Terraform module is used to create eventbridge resource on AWS for network connectivity.. + +# extra content +include: + - "terraform.md" + +# How to use this project +# yamllint disable rule:line-length +usage: |- + ### event_bus_archive + Here is an example of how you can use this module in your inventory structure: + ```hcl + module "eventbridge_archive" { + + create_bus = true + create_archives = true + + bus_name = "clouddrove-bus" + + + archives = { + "launch-archive-existing-bus" = { + description = "launch-archive", + retention_days = 1 + event_pattern = jsonencode( + { + "source" : ["aws.autoscaling"], + "detail-type" : ["EC2 Instance Launch Successful"] + } + ) + } + } + + } + ``` + + ### event_rule_target + ```hcl + module "eventbridge" { + + name = "subnets" + environment = "test" + label_order = ["name", "environment"] + + bus_name = "test" + rules = { + orders = { + description = "Capture all order data" + event_pattern = jsonencode({ "source" : ["myapp.orders"] }) + enabled = true + role_arn = module.iam-role.arn + } + emails = { + description = "Capture all emails data" + event_pattern = jsonencode({ "source" : ["myapp.emails"] }) + enabled = true + } + } + targets = { + orders = [ + { + name = "send-orders-to-sqs-wth-dead-letter" + arn = aws_sqs_queue.queue.arn + dead_letter_arn = aws_sqs_queue.dlq.arn + }, + { + name = "send-orders-to-sqs" + arn = aws_sqs_queue.queue.arn + } + ] + emails = [ + { + name = "send-orders-to-kinesis" + dead_letter_arn = aws_sqs_queue.dlq.arn + arn = aws_sqs_queue.dlq.arn + + } + ] + } + } + ``` diff --git a/_example/event_bus_archive/example.tf b/_example/event_bus_archive/example.tf index eea82bc..3352e05 100644 --- a/_example/event_bus_archive/example.tf +++ b/_example/event_bus_archive/example.tf @@ -25,4 +25,3 @@ module "eventbridge_archive" { } } - diff --git a/_example/event_rule_target/example.tf b/_example/event_rule_target/example.tf index 887eb4a..bbaa1b3 100644 --- a/_example/event_rule_target/example.tf +++ b/_example/event_rule_target/example.tf @@ -58,4 +58,3 @@ module "eventbridge" { ] } } - \ No newline at end of file diff --git a/_example/event_rule_target/iam.tf b/_example/event_rule_target/iam.tf index 7babf31..3175823 100644 --- a/_example/event_rule_target/iam.tf +++ b/_example/event_rule_target/iam.tf @@ -47,4 +47,4 @@ data "aws_iam_policy_document" "iam-policy" { aws_sqs_queue.dlq.arn ] } -} \ No newline at end of file +} diff --git a/main.tf b/main.tf index 9322aad..4c647fb 100644 --- a/main.tf +++ b/main.tf @@ -24,6 +24,7 @@ module "labels" { name = var.name environment = var.environment label_order = var.label_order + repository = var.repository } resource "aws_cloudwatch_event_bus" "this" { @@ -47,7 +48,7 @@ resource "aws_cloudwatch_event_rule" "this" { is_enabled = lookup(each.value, "enabled", true) event_pattern = lookup(each.value, "event_pattern", null) schedule_expression = lookup(each.value, "schedule_expression", null) - role_arn = lookup(each.value, "role_arn" , null ) + role_arn = lookup(each.value, "role_arn", null) tags = merge(var.tags, { Name = each.value.Name @@ -64,7 +65,7 @@ resource "aws_cloudwatch_event_target" "this" { rule = each.value.Name arn = each.value.arn - role_arn = lookup(each.value, "role_arn", null) + role_arn = lookup(each.value, "role_arn", null) target_id = lookup(each.value, "target_id", null) input = lookup(each.value, "input", null) input_path = lookup(each.value, "input_path", null) diff --git a/variable.tf b/variable.tf index 46d8dc8..a80a6af 100644 --- a/variable.tf +++ b/variable.tf @@ -97,4 +97,17 @@ variable "label_order" { type = list(any) default = [] description = "Label order, e.g. `name`,`application`." -} \ No newline at end of file +} + + +variable "repository" { + type = string + default = "https://github.com/clouddrove/terraform-aws-eventbridge" + description = "Terraform current module repo" + + validation { + # regex(...) fails if it cannot find a match + condition = can(regex("^https://", var.repository)) + error_message = "The module-repo value must be a valid Git repo link." + } +} diff --git a/versions.tf b/versions.tf new file mode 100644 index 0000000..46e9063 --- /dev/null +++ b/versions.tf @@ -0,0 +1,11 @@ +# Terraform version +terraform { + required_version = ">= 0.12.0" + + required_providers { + aws = { + source = "hashicorp/aws" + version = ">= 3.1.15" + } + } +} From b85b054e0b4aebc6edc17958b3e984c90f8f8bf1 Mon Sep 17 00:00:00 2001 From: Sohan Date: Wed, 7 Jul 2021 19:21:51 +0530 Subject: [PATCH 2/4] standardised the module --- _example/event_bus_archive/example.tf | 4 ++- _example/event_bus_archive/output.tf | 5 +++ _example/event_rule_target/example.tf | 5 +-- _example/event_rule_target/iam.tf | 14 --------- _example/event_rule_target/output.tf | 5 +++ _test/event_bus_archive/event_bus_archive.go | 32 ++++++++++++++++++++ _test/event_rule_target/event_bus_archive.go | 32 ++++++++++++++++++++ 7 files changed, 78 insertions(+), 19 deletions(-) create mode 100644 _test/event_bus_archive/event_bus_archive.go create mode 100644 _test/event_rule_target/event_bus_archive.go diff --git a/_example/event_bus_archive/example.tf b/_example/event_bus_archive/example.tf index 3352e05..df8b612 100644 --- a/_example/event_bus_archive/example.tf +++ b/_example/event_bus_archive/example.tf @@ -8,7 +8,9 @@ module "eventbridge_archive" { create_bus = true create_archives = true - bus_name = "clouddrove-bus" + name = "bus" + environment = "test" + label_order = ["name", "environment"] archives = { diff --git a/_example/event_bus_archive/output.tf b/_example/event_bus_archive/output.tf index e68438a..23f8710 100644 --- a/_example/event_bus_archive/output.tf +++ b/_example/event_bus_archive/output.tf @@ -12,3 +12,8 @@ output "eventbridge_rule_arns" { description = "The EventBridge Rule ARNs" value = module.eventbridge_archive.eventbridge_rule_arns } + +output "tags" { + value = module.eventbridge_archive.tags + description = "A mapping of tags to assign to the resource." +} \ No newline at end of file diff --git a/_example/event_rule_target/example.tf b/_example/event_rule_target/example.tf index bbaa1b3..bde7007 100644 --- a/_example/event_rule_target/example.tf +++ b/_example/event_rule_target/example.tf @@ -2,12 +2,10 @@ provider "aws" { region = "eu-west-1" } - resource "random_pet" "this" { length = 2 } - resource "aws_sqs_queue" "queue" { name = "${random_pet.this.id}-queue" } @@ -18,11 +16,10 @@ resource "aws_sqs_queue" "dlq" { module "eventbridge" { source = "../../" - name = "subnets" + name = "eventbridge" environment = "test" label_order = ["name", "environment"] - bus_name = "test" rules = { orders = { description = "Capture all order data" diff --git a/_example/event_rule_target/iam.tf b/_example/event_rule_target/iam.tf index 3175823..9b206cf 100644 --- a/_example/event_rule_target/iam.tf +++ b/_example/event_rule_target/iam.tf @@ -23,20 +23,6 @@ data "aws_iam_policy_document" "default" { } } -# data "aws_iam_policy_document" "iam-policy" { -# statement { -# actions = [ -# "ssm:UpdateInstanceInformation", -# "ssmmessages:CreateControlChannel", -# "ssmmessages:CreateDataChannel", -# "ssmmessages:OpenControlChannel", -# "ssmmessages:OpenDataChannel"] -# effect = "Allow" -# resources = ["*"] -# } -# } - - data "aws_iam_policy_document" "iam-policy" { statement { sid = "eventspolicy" diff --git a/_example/event_rule_target/output.tf b/_example/event_rule_target/output.tf index 89ca939..6f569cd 100644 --- a/_example/event_rule_target/output.tf +++ b/_example/event_rule_target/output.tf @@ -12,3 +12,8 @@ output "eventbridge_rule_arns" { description = "The EventBridge Rule ARNs" value = module.eventbridge.eventbridge_rule_arns } + +output "tags" { + value = module.eventbridge.tags + description = "A mapping of tags to assign to the resource." +} \ No newline at end of file diff --git a/_test/event_bus_archive/event_bus_archive.go b/_test/event_bus_archive/event_bus_archive.go new file mode 100644 index 0000000..5574659 --- /dev/null +++ b/_test/event_bus_archive/event_bus_archive.go @@ -0,0 +1,32 @@ +// Managed By : CloudDrove +// Description : This Terratest is used to test the Terraform KeyPair module. +// Copyright @ CloudDrove. All Right Reserved. +package test + +import ( + "testing" + "github.com/stretchr/testify/assert" + "github.com/gruntwork-io/terratest/modules/terraform" +) + +func Test(t *testing.T) { + t.Parallel() + + terraformOptions := &terraform.Options{ + // Source path of Terraform directory. + TerraformDir: "../_example", + Upgrade: true, + } + + // This will run 'terraform init' and 'terraform application' and will fail the test if any errors occur + terraform.InitAndApply(t, terraformOptions) + + // To clean up any resources that have been created, run 'terraform destroy' towards the end of the test + defer terraform.Destroy(t, terraformOptions) + + // To get the value of an output variable, run 'terraform output' + name := terraform.OutputList(t, terraformOptions, "name") + + // Check that we get back the outputs that we expect + assert.Equal(t, "key-test", name[0]) +} diff --git a/_test/event_rule_target/event_bus_archive.go b/_test/event_rule_target/event_bus_archive.go new file mode 100644 index 0000000..4fe2e86 --- /dev/null +++ b/_test/event_rule_target/event_bus_archive.go @@ -0,0 +1,32 @@ +// Managed By : CloudDrove +// Description : This Terratest is used to test the Terraform KeyPair module. +// Copyright @ CloudDrove. All Right Reserved. +package test + +import ( + "testing" + "github.com/stretchr/testify/assert" + "github.com/gruntwork-io/terratest/modules/terraform" +) + +func Test(t *testing.T) { + t.Parallel() + + terraformOptions := &terraform.Options{ + // Source path of Terraform directory. + TerraformDir: "../../_example/event_bus_archive", + Upgrade: true, + } + + // This will run 'terraform init' and 'terraform application' and will fail the test if any errors occur + terraform.InitAndApply(t, terraformOptions) + + // To clean up any resources that have been created, run 'terraform destroy' towards the end of the test + defer terraform.Destroy(t, terraformOptions) + + // To get the value of an output variable, run 'terraform output' + Tags := terraform.OutputMap(t, terraformOptions, "tags") + + // Check that we get back the outputs that we expect + assert.Equal(t, "bus-test", Tags["Name"]) +} From 8b71107bdeb8162e0eebbb0cc92506eb40a6b619 Mon Sep 17 00:00:00 2001 From: Sohan Date: Wed, 7 Jul 2021 19:25:17 +0530 Subject: [PATCH 3/4] standardised the module --- .github/workflows/terraform.yml | 2 +- .gitignore | 4 ++++ LICENSE | 2 +- README.yaml | 5 +++-- _example/event_bus_archive/output.tf | 2 +- _example/event_rule_target/output.tf | 2 +- _test/event_bus_archive/event_bus_archive.go | 6 +++--- _test/event_rule_target/event_bus_archive.go | 4 ++-- main.tf | 20 +++++++++++++++++--- output.tf | 11 +++++++---- variable.tf | 7 ------- 11 files changed, 40 insertions(+), 25 deletions(-) diff --git a/.github/workflows/terraform.yml b/.github/workflows/terraform.yml index 865cebb..ef84b50 100644 --- a/.github/workflows/terraform.yml +++ b/.github/workflows/terraform.yml @@ -109,7 +109,7 @@ jobs: with: status: ${{ job.status }} fields: repo,author - author_name: 'CloudDrove' + author_name: 'CloudDrove Inc' env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # required SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_TERRAFORM }} # required diff --git a/.gitignore b/.gitignore index 98d0c1b..e8ace56 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,7 @@ _example/.terraform.tfstate.lock.info *.terrafrom.lock.hcl .terraform.lock.hcl +/_test/event_bus_archive/go.mod +/_test/event_bus_archive/go.sum +/_test/event_rule_target/go.sum +/_test/event_rule_target/go.sum diff --git a/LICENSE b/LICENSE index 807110a..55beef5 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2020 Cloud Drove +Copyright (c) 2021 Cloud Drove Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.yaml b/README.yaml index 0831490..a5a9881 100644 --- a/README.yaml +++ b/README.yaml @@ -44,7 +44,9 @@ usage: |- create_bus = true create_archives = true - bus_name = "clouddrove-bus" + name = "bus" + environment = "test" + label_order = ["name", "environment"] archives = { @@ -71,7 +73,6 @@ usage: |- environment = "test" label_order = ["name", "environment"] - bus_name = "test" rules = { orders = { description = "Capture all order data" diff --git a/_example/event_bus_archive/output.tf b/_example/event_bus_archive/output.tf index 23f8710..e6ecfbe 100644 --- a/_example/event_bus_archive/output.tf +++ b/_example/event_bus_archive/output.tf @@ -16,4 +16,4 @@ output "eventbridge_rule_arns" { output "tags" { value = module.eventbridge_archive.tags description = "A mapping of tags to assign to the resource." -} \ No newline at end of file +} diff --git a/_example/event_rule_target/output.tf b/_example/event_rule_target/output.tf index 6f569cd..d27278f 100644 --- a/_example/event_rule_target/output.tf +++ b/_example/event_rule_target/output.tf @@ -16,4 +16,4 @@ output "eventbridge_rule_arns" { output "tags" { value = module.eventbridge.tags description = "A mapping of tags to assign to the resource." -} \ No newline at end of file +} diff --git a/_test/event_bus_archive/event_bus_archive.go b/_test/event_bus_archive/event_bus_archive.go index 5574659..4fe2e86 100644 --- a/_test/event_bus_archive/event_bus_archive.go +++ b/_test/event_bus_archive/event_bus_archive.go @@ -14,7 +14,7 @@ func Test(t *testing.T) { terraformOptions := &terraform.Options{ // Source path of Terraform directory. - TerraformDir: "../_example", + TerraformDir: "../../_example/event_bus_archive", Upgrade: true, } @@ -25,8 +25,8 @@ func Test(t *testing.T) { defer terraform.Destroy(t, terraformOptions) // To get the value of an output variable, run 'terraform output' - name := terraform.OutputList(t, terraformOptions, "name") + Tags := terraform.OutputMap(t, terraformOptions, "tags") // Check that we get back the outputs that we expect - assert.Equal(t, "key-test", name[0]) + assert.Equal(t, "bus-test", Tags["Name"]) } diff --git a/_test/event_rule_target/event_bus_archive.go b/_test/event_rule_target/event_bus_archive.go index 4fe2e86..2400dbf 100644 --- a/_test/event_rule_target/event_bus_archive.go +++ b/_test/event_rule_target/event_bus_archive.go @@ -14,7 +14,7 @@ func Test(t *testing.T) { terraformOptions := &terraform.Options{ // Source path of Terraform directory. - TerraformDir: "../../_example/event_bus_archive", + TerraformDir: "../../_example/event_rule_target", Upgrade: true, } @@ -28,5 +28,5 @@ func Test(t *testing.T) { Tags := terraform.OutputMap(t, terraformOptions, "tags") // Check that we get back the outputs that we expect - assert.Equal(t, "bus-test", Tags["Name"]) + assert.Equal(t, "eventbridge-test", Tags["Name"]) } diff --git a/main.tf b/main.tf index 4c647fb..62ea036 100644 --- a/main.tf +++ b/main.tf @@ -17,6 +17,10 @@ locals { ]) } +#Module : labels +#Description : This terraform module is designed to generate consistent label names and tags +# for resources. You can use terraform-labels to implement a strict naming +# convention. module "labels" { source = "clouddrove/labels/aws" version = "0.15.0" @@ -27,13 +31,17 @@ module "labels" { repository = var.repository } +#Module : Event Bus +#Description : Provides an EventBridge event bus resource. resource "aws_cloudwatch_event_bus" "this" { count = var.create && var.create_bus ? 1 : 0 - name = var.bus_name - tags = var.tags + name = module.labels.id + tags = module.labels.tags } +#Module : Event Rule +#Description : Provides an EventBridge Rule resource. resource "aws_cloudwatch_event_rule" "this" { for_each = var.create && var.create_rules ? { for rule in local.eventbridge_rules : rule.name => rule @@ -50,11 +58,13 @@ resource "aws_cloudwatch_event_rule" "this" { schedule_expression = lookup(each.value, "schedule_expression", null) role_arn = lookup(each.value, "role_arn", null) - tags = merge(var.tags, { + tags = merge(module.labels.tags, { Name = each.value.Name }) } +#Module : Event Rule +#Description : Provides an EventBridge Target resource. resource "aws_cloudwatch_event_target" "this" { for_each = var.create && var.create_targets ? { for target in local.eventbridge_targets : target.name => target @@ -177,6 +187,8 @@ resource "aws_cloudwatch_event_target" "this" { depends_on = [aws_cloudwatch_event_rule.this] } +#Module : Event Rule +#Description : Provides an EventBridge event archive resource. resource "aws_cloudwatch_event_archive" "this" { for_each = var.create && var.create_archives ? var.archives : {} @@ -188,6 +200,8 @@ resource "aws_cloudwatch_event_archive" "this" { retention_days = lookup(each.value, "retention_days", null) } +#Module : Event Rule +#Description : Provides a resource to create an EventBridge permission to support cross-account events in the current account default event bus. resource "aws_cloudwatch_event_permission" "this" { for_each = var.create && var.create_permissions ? var.permissions : {} diff --git a/output.tf b/output.tf index 032afa3..51c808d 100644 --- a/output.tf +++ b/output.tf @@ -1,8 +1,6 @@ # EventBridge Bus -output "eventbridge_bus_name" { - description = "The EventBridge Bus Name" - value = var.bus_name -} +# EventBridge Bus + output "eventbridge_bus_arn" { description = "The EventBridge Bus Arn" @@ -29,3 +27,8 @@ output "eventbridge_rule_arns" { for p in sort(keys(var.rules)) : p => aws_cloudwatch_event_rule.this[p].arn } } + +output "tags" { + value = module.labels.tags + description = "A mapping of tags to assign to the resource." +} diff --git a/variable.tf b/variable.tf index a80a6af..a1ebe5a 100644 --- a/variable.tf +++ b/variable.tf @@ -41,13 +41,6 @@ variable "create_archives" { } ####################### - -variable "bus_name" { - description = "A unique name for your EventBridge Bus" - type = string - default = "default" -} - variable "rules" { description = "A map of objects with EventBridge Rule definitions." type = map(any) From 0b8a2e0ca13ca31c7aed27f559559e56f96f5a07 Mon Sep 17 00:00:00 2001 From: Sohan Date: Wed, 7 Jul 2021 19:34:35 +0530 Subject: [PATCH 4/4] standardised the module --- .github/workflows/readme.yml | 2 +- .github/workflows/terraform.yml | 18 +++++++++--------- .github/workflows/terratest.yml | 19 ++++++++++++++----- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/.github/workflows/readme.yml b/.github/workflows/readme.yml index 3edbae3..5f0038e 100644 --- a/.github/workflows/readme.yml +++ b/.github/workflows/readme.yml @@ -47,7 +47,7 @@ jobs: with: status: ${{ job.status }} fields: repo,author - author_name: 'CloudDrove' + author_name: 'CloudDrove Inc.' env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # required SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_TERRAFORM }} # required diff --git a/.github/workflows/terraform.yml b/.github/workflows/terraform.yml index ef84b50..4c534a4 100644 --- a/.github/workflows/terraform.yml +++ b/.github/workflows/terraform.yml @@ -13,11 +13,11 @@ jobs: uses: actions/checkout@v2.3.4 - name: 'Terraform Format' - uses: 'clouddrove/github-actions@v7.0' + uses: 'clouddrove/github-actions@v8.0' with: actions_subcommand: 'fmt' - name: 'Terraform Format' - uses: 'clouddrove/github-actions@v7.0' + uses: 'clouddrove/github-actions@v8.0' with: actions_subcommand: 'fmt' @@ -36,19 +36,19 @@ jobs: aws-region: us-east-2 - name: 'Terraform init' - uses: 'clouddrove/github-actions@v7.0' + uses: 'clouddrove/github-actions@v8.0' with: actions_subcommand: 'init' tf_actions_working_dir: ./_example/event_bus_archive - name: 'Terraform validate for event_bus_archive' - uses: 'clouddrove/github-actions@v7.0' + uses: 'clouddrove/github-actions@v8.0' with: actions_subcommand: 'validate' tf_actions_working_dir: ./_example/event_bus_archive - name: 'Terraform plan for event_bus_archive' - uses: 'clouddrove/github-actions@v7.0' + uses: 'clouddrove/github-actions@v8.0' with: actions_subcommand: 'plan' tf_actions_working_dir: ./_example/event_bus_archive @@ -68,19 +68,19 @@ jobs: aws-region: us-east-2 - name: 'Terraform init' - uses: 'clouddrove/github-actions@v7.0' + uses: 'clouddrove/github-actions@v8.0' with: actions_subcommand: 'init' tf_actions_working_dir: ./_example/event_rule_target - name: 'Terraform validate for event_rule_target' - uses: 'clouddrove/github-actions@v7.0' + uses: 'clouddrove/github-actions@v8.0' with: actions_subcommand: 'validate' tf_actions_working_dir: ./_example/event_rule_target - name: 'Terraform plan for event_rule_target' - uses: 'clouddrove/github-actions@v7.0' + uses: 'clouddrove/github-actions@v8.0' with: actions_subcommand: 'plan' tf_actions_working_dir: ./_example/event_rule_target @@ -109,7 +109,7 @@ jobs: with: status: ${{ job.status }} fields: repo,author - author_name: 'CloudDrove Inc' + author_name: 'CloudDrove Inc.' env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # required SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_TERRAFORM }} # required diff --git a/.github/workflows/terratest.yml b/.github/workflows/terratest.yml index d39bc81..9f2e6c6 100644 --- a/.github/workflows/terratest.yml +++ b/.github/workflows/terratest.yml @@ -12,7 +12,7 @@ jobs: steps: - name: 'Checkout' - uses: actions/checkout@master + uses: actions/checkout@v2.3.4 - name: Configure AWS Credentials uses: clouddrove/configure-aws-credentials@v1 @@ -21,12 +21,21 @@ jobs: aws-secret-access-key: ${{ secrets.TEST_AWS_ACCESS_SECRET_KEY }} aws-region: us-east-2 - - name: 'Terratest' + - name: 'Terratest for event_bus_archive' if: ${{ github.event.label.name == 'terratest' }} - uses: 'clouddrove/github-actions@v4.0' + uses: 'clouddrove/github-actions@v8.0' with: actions_subcommand: 'terratest' - tf_actions_working_dir: '_test' + tf_actions_working_dir: '_test/event_bus_archive' + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: 'Terratest for event_rule_target' + if: ${{ github.event.label.name == 'terratest' }} + uses: 'clouddrove/github-actions@v8.0' + with: + actions_subcommand: 'terratest' + tf_actions_working_dir: '_test/event_rule_target' env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} @@ -35,7 +44,7 @@ jobs: with: status: ${{ job.status }} fields: repo,author - author_name: 'CloudDrove' + author_name: 'CloudDrove Inc.' env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # required SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_TERRAFORM }} # required