diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8c17ca4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +# ignored files +*.tfstate +*.tfstate.backup +.terraform +.idea +*.iml \ No newline at end of file diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..aa6e42c --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,13 @@ +repos: + - repo: git://github.com/antonbabenko/pre-commit-terraform + rev: v1.12.0 + hooks: + - id: terraform_fmt + + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v2.0.0 + hooks: + - id: check-merge-conflict + - id: trailing-whitespace + - id: check-yaml + - id: check-added-large-files diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..136d77d --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 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..d9e69c6 --- /dev/null +++ b/Makefile @@ -0,0 +1,3 @@ +export GENIE_PATH ?= $(shell 'pwd')/../../../genie + +include $(GENIE_PATH)/Makefile \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..6ceb839 --- /dev/null +++ b/README.md @@ -0,0 +1,146 @@ + + +

+ + +

+ Terraform AWS ECR +

+ +

+ This terraform module is used to create ECR on AWS. +

+ +

+ + + Terraform + + + Licence + + + +

+

+ + + + + + + + + + + +

+
+ + +We eat, drink, sleep and most importantly love **DevOps**. We are working towards stratergies for standardizing architecture while ensuring security for the infrastructure. We are strong believer of the philosophy Bigger problems are always solved by breaking them into smaller manageable problems. Resonating with microservices architecture, it is considered best-practice to run database, cluster, storage in smaller connected yet manageable pieces within the infrastructure. + +This module is basically combination of [Terraform open source](https://www.terraform.io/) and includes automatation tests and examples. It also helps to create and improve your infrastructure with minimalistic code instead of maintaining the whole infrastructure code yourself. + +We have [*fifty plus terraform modules*][terraform_modules]. A few of them are comepleted and are available for open source usage while a few others are in progress. + + + + +## Prerequisites + +This module has a few dependencies: + +- [Terraform 0.12](https://learn.hashicorp.com/terraform/getting-started/install.html) +- [Go](https://golang.org/doc/install) +- [github.com/stretchr/testify/assert](https://github.com/stretchr/testify) +- [github.com/gruntwork-io/terratest/modules/terraform](https://github.com/gruntwork-io/terratest) + + + + + + + +## Examples + + +**IMPORTANT:** Since the `master` branch used in `source` varies based on new modifications, we suggest that you use the release versions [here](https://github.com/clouddrove/terraform-aws-ecr/releases). + + +### Simple Example +Here is an example of how you can use this module in your inventory structure: +```hcl + module "ecr" { + source = "git::https://github.com/clouddrove/terraform-aws-ecr.git?ref=tags/0.12.0" + name = "ecr" + application = "clouddrove" + environment = "test" + label_order = ["environment", "name", "application"] +} +``` + + + + + + +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|:----:|:-----:|:-----:| +| application | Application (e.g. `cd` or `clouddrove`). | string | `` | no | +| attributes | Additional attributes (e.g. `1`). | list | `` | no | +| delimiter | Delimiter to be used between `organization`, `environment`, `name` and `attributes`. | string | `-` | no | +| enabled_ecr | Set to false to prevent the module from creating any resources. | bool | `true` | no | +| environment | Environment (e.g. `prod`, `dev`, `staging`). | string | `` | no | +| label_order | Label order, e.g. `name`,`application`. | list | `` | no | +| max_image_count | How many Docker Image versions AWS ECR will store. | number | `7` | no | +| name | Name (e.g. `app` or `cluster`). | string | `` | no | +| principals_full_access | Principal ARN to provide with full access to the ECR. | list | `` | no | +| principals_readonly_access | Principal ARN to provide with readonly access to the ECR. | list | `` | no | +| tags | Additional tags (e.g. map(`BusinessUnit`,`XYZ`). | map | `` | no | +| use_fullname | Set 'true' to use `namespace-stage-name` for ecr repository name, else `name`. | bool | `true` | no | + +## Outputs + +| Name | Description | +|------|-------------| +| arn | Registry ARN. | +| registry_id | Registry ID. | +| registry_url | Registry URL. | +| repository_name | Registry name. | +| tags | A mapping of tags to assign to the resource. | + + + + +## Testing +In this module testing is performed with [terratest](https://github.com/gruntwork-io/terratest) and it creates a small piece of infrastructure, matches the output like ARN, ID and Tags name etc and destroy infrastructure in your AWS account. This testing is written in GO, so you need a [GO environment](https://golang.org/doc/install) in your system. + +You need to run the following command in the testing folder: +```hcl + go test -run Test +``` + + + +## Feedback +If you come accross a bug or have any feedback, please log it in our [issue tracker](https://github.com/clouddrove/terraform-aws-ecr/issues), or feel free to drop us an email at [hello@clouddrove.com](mailto:hello@clouddrove.com). + +If you have found it worth your time, go ahead and give us a ★ on [our GitHub](https://github.com/clouddrove/terraform-aws-ecr)! + +## About us + +At [CloudDrove][website], we offer expert guidance, implementation support and services to help organisations accelerate their journey to the cloud. Our services include docker and container orchestration, cloud migration and adoption, infrastructure automation, application modernisation and remediation, and performance engineering. + +

We are The Cloud Experts!

+
+

We ❤️ Open Source and you can check out our other modules to get help with your new Cloud ideas.

+ + [website]: https://clouddrove.com + [github]: https://github.com/clouddrove + [linkedin]: https://cpco.io/linkedin + [twitter]: https://twitter.com/clouddrove/ + [email]: https://clouddrove.com/contact-us.html + [terraform_modules]: https://github.com/clouddrove?utf8=%E2%9C%93&q=terraform-&type=&language= \ No newline at end of file diff --git a/README.yaml b/README.yaml new file mode 100644 index 0000000..803c36a --- /dev/null +++ b/README.yaml @@ -0,0 +1,45 @@ +--- +# +# This is the canonical configuration for the `README.md` +# Run `make readme` to rebuild the `README.md` +# + +# Name of this project +name : Terraform AWS ECR + +# License of this project +license: "MIT" + +# Canonical GitHub repo +github_repo: clouddrove/terraform-aws-ecr + +# Badges to display +badges: + - name: "Terraform" + image: "https://img.shields.io/badge/Terraform-v0.12-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 +description: |- + This terraform module is used to create ECR on AWS. + +# extra content +include: + - "terraform.md" + +# How to use this project +usage : |- + ### Simple Example + Here is an example of how you can use this module in your inventory structure: + ```hcl + module "ecr" { + source = "git::https://github.com/clouddrove/terraform-aws-ecr.git?ref=tags/0.12.0" + name = "ecr" + application = "clouddrove" + environment = "test" + label_order = ["environment", "name", "application"] + } + ``` \ No newline at end of file diff --git a/_example/example.tf b/_example/example.tf new file mode 100644 index 0000000..883ecf1 --- /dev/null +++ b/_example/example.tf @@ -0,0 +1,11 @@ +provider "aws" { + region = "eu-west-1" +} + +module "ecr" { + source = "git::https://github.com/clouddrove/terraform-aws-ecr.git?ref=tags/0.12.0" + name = "ecr" + application = "clouddrove" + environment = "test" + label_order = ["environment", "name", "application"] +} \ No newline at end of file diff --git a/_example/outputs.tf b/_example/outputs.tf new file mode 100644 index 0000000..09440d7 --- /dev/null +++ b/_example/outputs.tf @@ -0,0 +1,9 @@ +output "arn" { + value = module.ecr.*.arn + description = "Registry name." +} + +output "tags" { + value = module.ecr.tags + description = "A mapping of tags to assign to the alb." +} \ No newline at end of file diff --git a/_test/ecr_test.go b/_test/ecr_test.go new file mode 100644 index 0000000..df06782 --- /dev/null +++ b/_test/ecr_test.go @@ -0,0 +1,34 @@ +// Managed By : CloudDrove +// Description : This Terratest is used to test the Terraform ECR module. +// Copyright @ CloudDrove. All Right Reserved. +package test + +import ( + "testing" + + "github.com/gruntwork-io/terratest/modules/terraform" + "github.com/stretchr/testify/assert" +) + +func Test(t *testing.T) { + t.Parallel() + + terraformOptions := &terraform.Options{ + // Source path of Terraform directory. + TerraformDir: "../_example", + } + + // 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' + Arn := terraform.Output(t, terraformOptions, "arn") + Tags := terraform.OutputMap(t, terraformOptions, "tags") + + // Check that we get back the outputs that we expect + assert.Equal(t, "test-ecr-clouddrove", Tags["Name"]) + assert.Contains(t, Arn, "arn:aws:ecr") +} \ No newline at end of file diff --git a/main.tf b/main.tf new file mode 100755 index 0000000..8cfb629 --- /dev/null +++ b/main.tf @@ -0,0 +1,136 @@ +## Managed By : CloudDrove +## Description : This Script is used to create Aws ECR repository and policy. +## Copyright @ CloudDrove. All Right Reserved. + +locals { + principals_readonly_access_non_empty = length(var.principals_readonly_access) > 0 ? true : false + principals_full_access_non_empty = length(var.principals_full_access) > 0 ? true : false + ecr_need_policy = length(var.principals_full_access) + length(var.principals_readonly_access) > 0 ? true : false +} + +#Module : label +#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 = "git::https://github.com/clouddrove/terraform-labels.git" + + name = var.name + application = var.application + environment = var.environment + label_order = var.label_order +} + +# Module : ECR REPOSITORY +# Description : Provides an Elastic Container Registry Repository. +resource "aws_ecr_repository" "default" { + count = var.enabled_ecr ? 1 : 0 + name = module.labels.id + tags = module.labels.tags +} + +resource "aws_ecr_lifecycle_policy" "default" { + count = var.enabled_ecr ? 1 : 0 + repository = join("", aws_ecr_repository.default.*.name) + + policy = <