├── .checkov.yaml ├── .docs ├── CI4Terraform.drawio └── CI4Terraform.png ├── .gitignore ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md └── terraform ├── examples └── cicd_account │ ├── inventories │ └── variables.tfvars │ ├── main.tf │ ├── outputs.tf │ ├── run_terraform.sh │ └── variables.tf └── modules ├── buildspec.yaml ├── cicd ├── README.md ├── codebuild.tf ├── codebuild_roles_policies.tf ├── codepipeline-role-policy.tf ├── codepipeline.tf ├── data.tf ├── kms.tf ├── locals.tf ├── outputs.tf ├── providers.tf ├── s3.tf ├── templates │ ├── cb_cross_account_dep_policy.json.tpl │ ├── codebuild-role-policy.json.tpl │ ├── codepipeline-role-policy.json.tpl │ └── key-policy.json.tpl ├── test │ ├── cicd_test.go │ ├── go.mod │ └── test.sh └── variables.tf ├── install.sh ├── prebuild.sh └── test.sh /.checkov.yaml: -------------------------------------------------------------------------------- 1 | branch: master 2 | download-external-modules: false 3 | evaluate-variables: true 4 | external-modules-download-path: .external_modules 5 | framework: terraform 6 | # skip-path: 7 | output: cli 8 | skip-check: 9 | - CKV_AWS_144 # Ensure S3 bucket has cross-region replication enabled 10 | - CKV_AWS_18 # "Ensure the S3 bucket has access logging enabled" 11 | -------------------------------------------------------------------------------- /.docs/CI4Terraform.drawio: -------------------------------------------------------------------------------- 1 | 7Vxbd9o4EP41PIYjyTf8yCXsdpvttk3Otn3KEVjY3hqLChOgv34lW8YXiUsbjEmbJG3QWNZlZr7RzHicjjGcb/5geBH8TT0SdRDwNh1j1EEIQmDzX4KylRQAjIzis9CTtIJwH34neUdJXYUeWVY6JpRGSbioEqc0jsk0qdAwY3Rd7TajUXXWBfaJQrif4kilfgq9JMioPeQU9D9J6Af5zNB2sytznHeWO1kG2KPrEsm47RhDRmmSfZpvhiQS3Mv5kt033nN1tzBG4uSUG/ruX9+89fotC1axc/ch/PD2C7rJ2fyEo5Xc8QNZJpzSn07pio+crT3Z5gxZ0DBOUqZaA/7D7x+CjsWvDE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9XEkaHm8V5w3UoTuNI4ApfFldsGrhrHR27Qj+PDbBMf5m/wcs9B3/N4vq+hl/54s/hrIlmNfvFHWYzb/wE= -------------------------------------------------------------------------------- /.docs/CI4Terraform.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aws-samples/ci-4-terraform-aws-developer-tools-terratest/e7181b643fcdca25292e06b935cf01ef11821b4a/.docs/CI4Terraform.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .idea 3 | .terraform 4 | *.tfstate* 5 | plan.out 6 | .terraform.lock.hcl 7 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | ## Code of Conduct 2 | This project has adopted the [Amazon Open Source Code of Conduct](https://aws.github.io/code-of-conduct). 3 | For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq) or contact 4 | opensource-codeofconduct@amazon.com with any additional questions or comments. 5 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing Guidelines 2 | 3 | Thank you for your interest in contributing to our project. Whether it's a bug report, new feature, correction, or additional 4 | documentation, we greatly value feedback and contributions from our community. 5 | 6 | Please read through this document before submitting any issues or pull requests to ensure we have all the necessary 7 | information to effectively respond to your bug report or contribution. 8 | 9 | 10 | ## Reporting Bugs/Feature Requests 11 | 12 | We welcome you to use the GitHub issue tracker to report bugs or suggest features. 13 | 14 | When filing an issue, please check existing open, or recently closed, issues to make sure somebody else hasn't already 15 | reported the issue. Please try to include as much information as you can. Details like these are incredibly useful: 16 | 17 | * A reproducible test case or series of steps 18 | * The version of our code being used 19 | * Any modifications you've made relevant to the bug 20 | * Anything unusual about your environment or deployment 21 | 22 | 23 | ## Contributing via Pull Requests 24 | Contributions via pull requests are much appreciated. Before sending us a pull request, please ensure that: 25 | 26 | 1. You are working against the latest source on the *main* branch. 27 | 2. You check existing open, and recently merged, pull requests to make sure someone else hasn't addressed the problem already. 28 | 3. You open an issue to discuss any significant work - we would hate for your time to be wasted. 29 | 30 | To send us a pull request, please: 31 | 32 | 1. Fork the repository. 33 | 2. Modify the source; please focus on the specific change you are contributing. If you also reformat all the code, it will be hard for us to focus on your change. 34 | 3. Ensure local tests pass. 35 | 4. Commit to your fork using clear commit messages. 36 | 5. Send us a pull request, answering any default questions in the pull request interface. 37 | 6. Pay attention to any automated CI failures reported in the pull request, and stay involved in the conversation. 38 | 39 | GitHub provides additional document on [forking a repository](https://help.github.com/articles/fork-a-repo/) and 40 | [creating a pull request](https://help.github.com/articles/creating-a-pull-request/). 41 | 42 | 43 | ## Finding contributions to work on 44 | Looking at the existing issues is a great way to find something to contribute on. As our projects, by default, use the default GitHub issue labels (enhancement/bug/duplicate/help wanted/invalid/question/wontfix), looking at any 'help wanted' issues is a great place to start. 45 | 46 | 47 | ## Code of Conduct 48 | This project has adopted the [Amazon Open Source Code of Conduct](https://aws.github.io/code-of-conduct). 49 | For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq) or contact 50 | opensource-codeofconduct@amazon.com with any additional questions or comments. 51 | 52 | 53 | ## Security issue notifications 54 | If you discover a potential security issue in this project we ask that you notify AWS/Amazon Security via our [vulnerability reporting page](http://aws.amazon.com/security/vulnerability-reporting/). Please do **not** create a public github issue. 55 | 56 | 57 | ## Licensing 58 | 59 | See the [LICENSE](LICENSE) file for our project's licensing. We will ask you to confirm the licensing of your contribution. 60 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of 4 | this software and associated documentation files (the "Software"), to deal in 5 | the Software without restriction, including without limitation the rights to 6 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 7 | the Software, and to permit persons to whom the Software is furnished to do so. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 10 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 11 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 12 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 13 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 14 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 15 | 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## ci-4-terraform-aws-developer-tools-terratest 2 | 3 | # Table of Contents 4 | - [Table of Contents](#table-of-contents) 5 | - [Introduction](#introduction) 6 | - [Scope of the artifact:](#scope-of-the-artifact) 7 | - [Target Audience](#target-audience) 8 | - [UseCase](#usecase) 9 | - [Architecture](#architecture) 10 | - [Repository Structure](#repository-structure) 11 | - [`/terraform`](#terraform) 12 | - [`/terraform/examples`](#terraformexamples) 13 | - [`/terraform/modules`](#terraformmodules) 14 | - [Guide](#guide) 15 | - [Prerequisites](#prerequisites) 16 | - [1. Deploy CI Pipeline.](#1-deploy-ci-pipeline) 17 | - [2. Review and tune the test code](#2-review-and-tune-the-test-code) 18 | - [3. Push the code inside your new CodeCommit repository.](#3-push-the-code-inside-your-new-codecommit-repository) 19 | - [4. Clean Up](#4-clean-up) 20 | - [Troubleshooting](#troubleshooting) 21 | - [Error deleting S3 Bucket (BucketNotEmpty)](#error-deleting-s3-bucket-bucketnotempty) 22 | - [Security](#security) 23 | - [License](#license) 24 | 25 | # Introduction 26 | 27 | [Terraform](https://www.terraform.io/) is widely used among customers to provision and manage infrastructure and services on AWS.
28 | A Terraform module is a container for multiple resources that are used together. Modules can, and should be used to create lightweight 29 | abstractions, so that you can describe your infrastructure in terms of its architecture, rather than directly in terms of 30 | physical objects.
31 | 32 | 33 | ## Scope of the artifact: 34 | 35 | In order to provide high quality and reliable modules each of these modules should be tested. 36 | 37 | Due to the nature of the Infrastructure as Code automated tests on the described resources have been always complicated often forcing the developers to perform manual tests on their solutions after the deployment. 38 | 39 | The module presented in this repository contains code to deploy Continuous Integration (CI) solution based on CodePipeline, CodeBuild and [Terratest](https://terratest.gruntwork.io/) library, offering the ability to run E2E tests on Terraform modules in an automated way. 40 | 41 | ## Target Audience 42 | 43 | The main target audience for this artifact are Infrastructure as Code developers with experience with Terraform. 44 | 45 | For simple basic tests no experience with `golang` is required. 46 | 47 | ## UseCase 48 | 49 | This artifact is build out of our experience in many projects when Terraform is used as main IaC tool. 50 | 51 | We observed on the customer side an increment of the demand of re-usable modules across teams and projects. 52 | 53 | As a consequence the customers are requiring high quality and reliable Terraform code. 54 | Moreover in such scenarios those modules are continuously developed. 55 | 56 | Automated E2E tests becomes then necessary to ensure the delivery of new feature, ensure the stability of the system and enforce high standards. 57 | 58 | ## Architecture 59 | 60 | The following architecture diagram simplifies the proposed solution. Note that an independent account is used to run the mentioned E2E 61 | tests based on the provided Terraform modules. 62 | 63 | ![pic1](./.docs/CI4Terraform.png) 64 | 65 | 66 | # Repository Structure 67 | 68 | ```bash 69 | . 70 | |-- README.md 71 | `-- terraform 72 | |-- examples 73 | `-- modules 74 | 75 | ``` 76 | 77 | ## `/terraform` 78 | 79 | This directory contains: 80 | - `./examples`: reusable examples for the modules. 81 | - `./modules`: Terraform modules for the CI pipeline, including the `CodeBuild` build scripts. 82 | 83 | The content of the repository is structured providing reusable Terraform modules. 84 | These modules and their content can be extracted and reused. 85 | 86 | ### `/terraform/examples` 87 | 88 | It is a good practice when releasing modules to provide also working examples. 89 | 90 | Those examples can be stored in a dedicated directory at the same level of the modules directory. 91 | 92 | In this repository the example [`cicd_account`](./terraform/examples/cicd_account/main.tf) is provided. It uses the module located in `/terraform/modules/cicd` 93 | . This example will deliver a CI based on `CodePipeline`, `CodeBuild` and `CodeCommit` and uses this same repository as source. 94 | 95 | The proposed behavior can be changed and adapted to the specific needs. 96 | 97 | ### `/terraform/modules` 98 | 99 | This directory contains the *Terraform* modules, the tests and the code run by `CodeBuild`. 100 | 101 | Information about `CodeBuild` and how to interpret the `buildspec.yaml` file can be found 102 | [here](https://docs.aws.amazon.com/codebuild/latest/userguide/build-spec-ref.html). 103 | It is not in the scope of this document to explain it. 104 | 105 | CodeBuild workflow: 106 | 107 | 1. Runs the tests described in `modules/cicd/test` 108 | 2. For each run the test reports can be accessed via AWS CodeBuild Console under `Reports` tab. 109 | 110 | # Guide 111 | 112 | ## Prerequisites 113 | 114 | The following prerequisites are necessary in order to run the example. 115 | 116 | - [Terraform version >= 1.3.0](https://learn.hashicorp.com/tutorials/terraform/install-cli) 117 | - [aws cli v2](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) 118 | - [python >= 3.9](https://www.python.org/downloads/) 119 | - [go >= 1.16](https://golang.org/doc/install) 120 | - [terratest >= 0.41.11](https://terratest.gruntwork.io/) 121 | 122 | ## 1. Deploy CI Pipeline. 123 | 124 | Using the provided code example the CI pipeline can be deployed. One has the choice of deploying the cross account setup, 125 | where the CI runs on one AWS account and the tests are performed on another AWS account. For the sake of simplicity this 126 | guide walks you through setting up the solution within a single AWS account. 127 | 128 | To deploy the CI follow the these steps: 129 | 1. Modify the [terraform/examples/cicd_account/inventories/variables.tfvars](./terraform/examples/cicd_account/inventories/variables.tfvars) 130 | accordingly with your needs. A list of all the available variables with a short description of their scope can be found [terraform/examples/cicd_account/variables.tf](./terraform/examples/cicd_account/variables.tf) 131 | 2. Configure your AWS CLI, [here](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html) for more information 132 | 3. Deploy the resources: 133 | 134 | ```sh 135 | cd terraform/examples/cicd_account 136 | ./run_terraform.sh 137 | ``` 138 | 139 | and follow the instructions provided. 140 | 141 | In case of errors look at the [Troubleshooting](#troubleshooting) section. 142 | 143 | The [README.md](./terraform/modules/cicd/README.md) inside the module provides information about the resources provisioned 144 | 145 | ## 2. Review and tune the test code 146 | 147 | A test example called [cicd_test.go](./terraform/modules/cicd/test/cicd_test.go) is found inside the module directory. It 148 | can be tuned to meet the desired requirements. 149 | 150 | A complete guide can be found in the [README.md](./terraform/modules/cicd/README.md) of the module. 151 | 152 | ## 3. Push the code inside your new CodeCommit repository. 153 | 154 | For this test you can push the same code you are using right now. 155 | 156 | 1. This first step is optional, but it is recommended: create a `.gitignore` file in the root level of the repository 157 | to avoid git tracking Terraform state files. 158 | 159 | ```sh 160 | echo "*.tfstate* 161 | .terraform 162 | plan.out" >> .gitignore 163 | ``` 164 | 165 | 2. Retrieve repository remote address: 166 | 1. on the AWS console got in `CodeCommit` 167 | 2. click on your repository name 168 | 3. Click on `Clone URL` and select your preferred way (I would suggest `HTTPS (GRC)`). 169 | Documentation about how to set up it can be found by clicking on `connection steps`. 170 | 171 | 3. Setup your git repo: 172 | 173 | ```sh 174 | git remote add origin 175 | ## By default the CodePipeline monitors the dev branch 176 | git checkout -b dev 177 | ## Add files and commit. Finally push to repository using the command: 178 | git push --set-upstream origin dev 179 | ``` 180 | 181 | 4. Push the repo against the **dev** branch this will trigger the start of the Pipeline 182 | 5. Follow the Pipeline Run connecting to the `CodePipeline` console. 183 | 184 | ## 4. Clean Up 185 | 186 | When you finished, in order to do not incur in extra costs, you should clean up your environment. 187 | 188 | You can do that by running: 189 | 190 | ```sh 191 | cd terraform/examples/cicd_account 192 | ./run_terraform.sh -d 193 | ``` 194 | 195 | The procedure will stop asking for confirming to have removed every object from the S3 bucket. 196 | 197 | The names of the buckets are showned as output from the previous command. 198 | 199 | ```sh 200 | # e.g. 201 | CodeBuild_S3_bucket = "artifact-cicd-test-12345678910-codebuild" 202 | CodePipeline_S3_bucket = "artifact-cicd-test-12345678910-codepipeline" 203 | Confirm that you have empty the S3 bucket... 204 | ``` 205 | 206 | This procedure needs to be done manually, in order to do that: 207 | 1. Access to AWS Console and go to S3 208 | 2. Select the buckets and click on `Empty` 209 | 210 | ## Troubleshooting 211 | 212 | ### Error deleting S3 Bucket (BucketNotEmpty) 213 | 214 | Terraform can exit with this error when you are cleaning or update a deployment. 215 | 216 | The reason resides to the fact that Terraform is not able to remove a non-empty S3 bucket. 217 | 218 | To fix the issue you need to manually remove the object in the bucket: 219 | 1. Access to AWS Console and go to S3 220 | 2. Select the failing bucket and click on `Empty` 221 | 222 | ## Security 223 | 224 | See [CONTRIBUTING](CONTRIBUTING.md#security-issue-notifications) for more information. 225 | 226 | ## License 227 | 228 | This library is licensed under the MIT-0 License. See the LICENSE file. 229 | -------------------------------------------------------------------------------- /terraform/examples/cicd_account/inventories/variables.tfvars: -------------------------------------------------------------------------------- 1 | git_repository_name = "artifact-cicd-test" 2 | 3 | -------------------------------------------------------------------------------- /terraform/examples/cicd_account/main.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_providers { 3 | aws = { 4 | version = "~> 4.2.0" 5 | } 6 | } 7 | } 8 | 9 | provider "aws" { 10 | region = "eu-west-1" 11 | } 12 | 13 | data "aws_caller_identity" "current" {} 14 | data "aws_region" "current" {} 15 | 16 | 17 | module "setup_cicd_account" { 18 | source = "../../modules/cicd" 19 | 20 | custom_tags = var.custom_tags 21 | account_type = "Demo" 22 | pipeline_deployment_bucket_name = "${var.git_repository_name}-${data.aws_caller_identity.current.account_id}" 23 | account_id = data.aws_caller_identity.current.account_id 24 | region = data.aws_region.current.name 25 | roles = var.cross_account_roles 26 | code_pipeline_build_stages = var.code_pipeline_build_stages 27 | git_repository_name = var.git_repository_name 28 | } 29 | -------------------------------------------------------------------------------- /terraform/examples/cicd_account/outputs.tf: -------------------------------------------------------------------------------- 1 | output "CodePipeline_S3_bucket" { 2 | description = "S3 bucket storing CodePipeline artefacts" 3 | value = module.setup_cicd_account.codepipeline_s3_bucket 4 | } 5 | 6 | output "CodeBuild_S3_bucket" { 7 | description = "S3 bucket storing CodeBuild artefacts" 8 | value = module.setup_cicd_account.codebuild_s3_bucket 9 | } 10 | 11 | output "CodePipeline_Name" { 12 | description = "Name of the CodePipeline" 13 | value = module.setup_cicd_account.codepipeline_name[0] 14 | } 15 | 16 | output "CodeCommit_Repository_Name" { 17 | description = "CodeCommit Repository Name" 18 | value = module.setup_cicd_account.codecommit_repo_name 19 | } -------------------------------------------------------------------------------- /terraform/examples/cicd_account/run_terraform.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | CLEAN_UP="No" 4 | 5 | while getopts ":dh" opt; do 6 | case ${opt} in 7 | h ) 8 | echo "Usage:" 9 | echo " ./run_terraform Deploy Terraform resources." 10 | echo " ./run_terraform -d Destroy deployed resources." 11 | exit 0 12 | ;; 13 | d ) # process option t 14 | echo "Destroy the current deployed resources" 15 | CLEAN_UP="Yes" 16 | ;; 17 | \? ) echo "Usage: ./run_terraform [-h] [-d]" 18 | ;; 19 | esac 20 | done 21 | 22 | 23 | 24 | if ( [[ -z ${AWS_ACCESS_KEY_ID} ]] || [[ -z ${AWS_SECRET_ACCESS_KEY} ]] ) && [[ -z ${AWS_PROFILE} ]]; then 25 | echo "[ERROR] Missing AWS credentials variables" 26 | exit 1 27 | fi 28 | 29 | TFVARS_FILE="inventories/variables.tfvars" 30 | # To use the env var AWS_PROFILE 31 | export AWS_SDK_LOAD_CONFIG="true" 32 | 33 | terraform init -input=false 34 | terraform validate 35 | retcode=$? 36 | if [[ $retcode != 0 ]]; then 37 | exit $retcode 38 | fi 39 | 40 | if [[ ${CLEAN_UP} == "Yes" ]]; then 41 | terraform output | grep -i S3_bucket 42 | read -p "Confirm that you have empty the S3 bucket" 43 | echo "[INFO] destroy all" 44 | terraform destroy -var-file ${TFVARS_FILE} 45 | exit 0 46 | fi 47 | 48 | echo "terraform plan -var-file ${TFVARS_FILE} -out plan.out" 49 | terraform plan -var-file ${TFVARS_FILE} -out plan.out 50 | retcode=$? 51 | if [[ $retcode != 0 ]]; then 52 | exit $retcode 53 | fi 54 | 55 | read -p "Do you want to apply? Yes/No: " answer 56 | 57 | if [[ $answer == "Yes" ]]; then 58 | terraform apply plan.out 59 | else 60 | echo "Exiting..." 61 | exit 0 62 | fi 63 | -------------------------------------------------------------------------------- /terraform/examples/cicd_account/variables.tf: -------------------------------------------------------------------------------- 1 | variable "custom_tags" { 2 | description = "Resources tags" 3 | type = object({ 4 | Environment = string 5 | TargetAccounts = string 6 | DeploymentType = string 7 | }) 8 | default = { 9 | Environment = "Deployment" 10 | TargetAccounts = "Demo" 11 | DeploymentType = "Terraform" 12 | } 13 | } 14 | 15 | variable "cross_account_roles" { 16 | description = "ARN of the roles to be assumed by CodeBuild in case of Cross Account Deployment" 17 | type = list(any) 18 | default = [] 19 | } 20 | 21 | variable "git_repository_name" { 22 | description = "Name of the remote git repository to be created" 23 | type = string 24 | } 25 | 26 | variable "code_pipeline_build_stages" { 27 | description = "maps of build type stages configured in CodePipeline" 28 | default = { 29 | "build" = "terraform/modules/buildspec.yaml" 30 | } 31 | } -------------------------------------------------------------------------------- /terraform/modules/buildspec.yaml: -------------------------------------------------------------------------------- 1 | version: 0.2 2 | 3 | env: 4 | variables: 5 | ENVIRONMENT: dev 6 | TF_IN_AUTOMATION: 1 7 | TERRAFORM_VERSION: 1.3.0 8 | TERRATEST_VERSION: v0.41.11 9 | BASE_PATH: terraform/modules 10 | #TEST_ACCOUNT_ASSUMED_ROLE: "" In case of cross deployment this is the role in the target account. 11 | 12 | phases: 13 | install: 14 | runtime-versions: 15 | golang: 1.16 16 | python: 3.9 17 | commands: 18 | - ./${BASE_PATH}/install.sh 19 | pre_build: 20 | commands: 21 | - echo Starting pre-build... 22 | - ./${BASE_PATH}/prebuild.sh 23 | build: 24 | commands: 25 | - ./${BASE_PATH}/test.sh 26 | post_build: 27 | commands: 28 | - echo Test Performed completed on `date` 29 | reports: 30 | cicd-reports: 31 | files: 32 | - "**/*.xml" 33 | - "**/*.log" 34 | base-directory: "terraform/modules/cicd/test/reports" 35 | discard-paths: no 36 | artifacts: 37 | files: 38 | - "**/*.log" 39 | - "**/*.xml" 40 | name: reports 41 | discard-paths: no 42 | base-directory: "terraform/modules/cicd/test/reports" -------------------------------------------------------------------------------- /terraform/modules/cicd/README.md: -------------------------------------------------------------------------------- 1 | # Table of Contents 2 | 3 | - [Table of Contents](#table-of-contents) 4 | - [Introduction](#introduction) 5 | - [Main features:](#main-features) 6 | - [Required Variables:](#required-variables) 7 | - [Tests](#tests) 8 | 9 | # Introduction 10 | 11 | This module will deploy CI pipeline based on `CodePipeline`, `CodeBuild` and `CodeCommit`. 12 | 13 | Documentation, about how to deploy it, is provided as an example [here](../../examples/cicd_account/main.tf). 14 | 15 | The optional and required parameters to be passed are documented directly in the [variables.tf](./variables.tf) file. 16 | 17 | ## Main features: 18 | 19 | - `CodeBuild` can be deployed in Public and Private Subnet (this needs to be provided apart) 20 | Private Subnet can be useful in case of connectivity with on-prem resources. 21 | - Support cross account deployments. As a pre-requisite a role thrusting the account where the CI is provided 22 | and with the right permissions should be provided. 23 | 24 | ## Required Variables: 25 | 26 | - `custom_tags`: tags to be attached to the deployed resources 27 | - `account_type`: Human readable name for the target account. 28 | The target account can be either the same account where `CodeBuild` is running or a remote account. 29 | This value will be exposed as environment variable inside `CodeBuild`. 30 | - `pipeline_deployment_bucket_name`: Bucket name for storing the artifacts, this will be created during 31 | `Terraform` run 32 | - `git_repository_name`: The `CodeCommit` repository name that you want to create and will be monitored by `CodePipeline` 33 | 34 | # Tests 35 | 36 | A basic Terraform test is provided for educational purpose using golang `Terratest` library 37 | and automatically performed by `CodeBuild`. 38 | 39 | The test will: 40 | 41 | 1. Try to deploy the all the resources described in the module 42 | 2. Check and compare CodePipeline and CodeBuild names 43 | 3. Destroy all the resources either is the test failed or exited successfully. 44 | 4. Save report and logs locally in order to be retrieved by the CI. 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /terraform/modules/cicd/codebuild.tf: -------------------------------------------------------------------------------- 1 | resource "aws_codebuild_project" "codebuild_deployment" { 2 | for_each = var.code_pipeline_build_stages 3 | name = "${var.git_repository_name}-${each.key}" 4 | description = "Code build project for ${var.git_repository_name} ${each.key} stage" 5 | build_timeout = "120" 6 | service_role = aws_iam_role.codebuild_role.arn 7 | encryption_key = aws_kms_key.codebuild-key.key_id 8 | 9 | artifacts { 10 | type = "CODEPIPELINE" 11 | } 12 | 13 | cache { 14 | type = "LOCAL" 15 | modes = ["LOCAL_DOCKER_LAYER_CACHE"] 16 | } 17 | 18 | environment { 19 | image = var.codebuild_image 20 | type = "LINUX_CONTAINER" 21 | image_pull_credentials_type = "CODEBUILD" 22 | privileged_mode = var.cb_priviledged_mode 23 | compute_type = var.codebuild_node_size 24 | 25 | # Generate block if variable is set 26 | dynamic "environment_variable" { 27 | for_each = var.proxy_config["HTTP_PROXY"] != "" ? var.proxy_config : {} 28 | content { 29 | name = environment_variable.key 30 | value = environment_variable.value 31 | } 32 | } 33 | } 34 | 35 | # Generate block if variable is set, this is a workaround to have a dynamic block set 36 | # At the moment is necessary to keep split to have a same type in the object 37 | # otherwise dynamic block cannot work 38 | dynamic "vpc_config" { 39 | for_each = var.priv_vpc_config["vpc_id"] != "" ? [var.priv_vpc_config["vpc_id"]] : [] 40 | content { 41 | vpc_id = var.priv_vpc_config["vpc_id"] 42 | subnets = split(",", var.priv_vpc_config["subnet_ids"]) 43 | security_group_ids = split(",", var.priv_vpc_config["security_group_ids"]) 44 | } 45 | } 46 | 47 | logs_config { 48 | cloudwatch_logs { 49 | group_name = "log-group" 50 | stream_name = "log-stream" 51 | } 52 | 53 | s3_logs { 54 | status = "ENABLED" 55 | location = "${aws_s3_bucket.codebuild_bucket.id}/${each.key}/build_logs" 56 | } 57 | } 58 | 59 | source { 60 | type = "CODEPIPELINE" 61 | buildspec = each.value 62 | } 63 | 64 | tags = var.custom_tags 65 | } 66 | 67 | -------------------------------------------------------------------------------- /terraform/modules/cicd/codebuild_roles_policies.tf: -------------------------------------------------------------------------------- 1 | resource "aws_iam_role" "codebuild_role" { 2 | name = "${var.git_repository_name}_codebuild_deploy_role" 3 | 4 | assume_role_policy = < /tmp/role 11 | export AWS_ACCESS_KEY_ID=$(cat /tmp/role|jq -r '.Credentials.AccessKeyId') 12 | export AWS_SECRET_ACCESS_KEY=$(cat /tmp/role|jq -r '.Credentials.SecretAccessKey') 13 | export AWS_SESSION_TOKEN=$(cat /tmp/role|jq -r '.Credentials.SessionToken') -------------------------------------------------------------------------------- /terraform/modules/test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | export AWS_SDK_LOAD_CONFIG="true" 6 | export AWS_REGION="eu-west-1" 7 | 8 | # Run terratest 9 | for dir in $(find ${BASE_PATH} -maxdepth 1 -mindepth 1 -type d -printf '%f\n'); do 10 | if [[ -f "${BASE_PATH}/${dir}/test/test.sh" ]]; then 11 | ./${BASE_PATH}/${dir}/test/test.sh 12 | fi 13 | done --------------------------------------------------------------------------------