├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── backend.tf ├── buildspec-apply.yaml ├── buildspec-plan.yaml ├── iam.tf ├── lambda ├── main.py └── main.zip ├── main.tf ├── provider.tf └── variable.tf /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 | MIT No Attribution 2 | 3 | Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so. 10 | 11 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 12 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 13 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 14 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 15 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 16 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 17 | 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # aws-codebuild-terraform 2 | 3 | This repo was created as part of the blog - "Best practices for managing Terraform State files in AWS CI/CD Pipeline" 4 | 5 | ## Best Practices for handling state files 6 | 7 | The recommended practice for managing state files is to use terraform’s built-in support for remote backends. 8 | 9 | Remote backend on Amazon Simple Storage Service (Amazon S3): 10 | You can configure terraform to store state files in an Amazon S3 bucket which provides a durable and scalable storage solution for your state files. Storing on Amazon S3 also enables collaboration that allows you to share state file with others. 11 | 12 | Remote backend on Amazon S3 with Amazon DynamoDB: 13 | In addition to using an Amazon S3 bucket for managing the files, you can use an Amazon DynamoDB table to lock the state file so that only one person can modify a particular state file at any given time. This will help to avoid conflicts and enable safe concurrent access to the state file. 14 | 15 | When deploying terraform on AWS, the preferred choice of managing state is using Amazon S3 with Amazon DynamoDB. 16 | 17 | ## AWS configurations for managing state files 18 | 19 | 1. Create an Amazon S3 bucket using terraform. Implement security measures for Amazon S3 bucket by creating an AWS Identity and Access Management (AWS IAM) policy or Amazon S3 Bucket Policy for restricting access, configuring object versioning for data protection and recovery, and enabling AES256 encryption with SSE-KMS for encryption control. 20 | 21 | 2. Next create an Amazon DynamoDB table using terraform with Primary key set to LockID and any additional configuration options such as read/write capacity units. Once the table is created, you will configure the terraform backend to use it for state locking by specifying the table name in the terraform block of your configuration. 22 | 23 | 3. For a single AWS account with multiple environments and projects, you can use a single Amazon S3 bucket. If you have multiple applications in multiple environments across multiple AWS accounts, you can create one Amazon S3 bucket for each account. In that Amazon S3 bucket, you can create appropriate folders for each environment, storing project state files with specific prefixes. 24 | 25 | This repo will explain how you can manage terraform state files efficiently in your Continuous Integration pipeline in AWS when used with AWS Developer Tools like AWS CodeCommit and AWS CodeBuild. 26 | 27 | 28 | -------------------------------------------------------------------------------- /backend.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | backend "s3" { 3 | bucket = "tfbackend-bucket" 4 | key = "terraform.tfstate" 5 | region = "eu-central-1" 6 | dynamodb_table = "tfstate-lock" 7 | encrypt = true 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /buildspec-apply.yaml: -------------------------------------------------------------------------------- 1 | version: 0.2 2 | phases: 3 | pre_build: 4 | commands: 5 | - terraform init 6 | - terraform validate 7 | 8 | build: 9 | commands: 10 | - terraform apply -auto-approve 11 | 12 | post_build: 13 | commands: 14 | - echo "Terraform apply completed on `date`" 15 | -------------------------------------------------------------------------------- /buildspec-plan.yaml: -------------------------------------------------------------------------------- 1 | version: 0.2 2 | phases: 3 | pre_build: 4 | commands: 5 | - terraform init 6 | - terraform validate 7 | 8 | build: 9 | commands: 10 | - terraform plan 11 | 12 | post_build: 13 | commands: 14 | - echo "Terraform plan completed on `date`" 15 | -------------------------------------------------------------------------------- /iam.tf: -------------------------------------------------------------------------------- 1 | #### CodeBuild permissions 2 | 3 | data "aws_iam_policy_document" "codebuild_policy_document" { 4 | statement { 5 | actions = ["logs:*"] 6 | resources = ["arn:aws:logs:*:*:*"] 7 | effect = "Allow" 8 | } 9 | statement { 10 | actions = [ 11 | "s3:List*", 12 | "s3:Get*", 13 | "s3:Put*", 14 | "s3:DeleteObject", 15 | "s3:DeleteObjectVersion" 16 | ] 17 | resources = [ 18 | "${aws_s3_bucket.s3_bucket_backend.arn}/*", 19 | "${aws_s3_bucket.s3_bucket_backend.arn}" 20 | ] 21 | effect = "Allow" 22 | } 23 | statement { 24 | effect = "Allow" 25 | actions = [ 26 | "dynamodb:BatchGetItem", 27 | "dynamodb:Query", 28 | "dynamodb:PutItem", 29 | "dynamodb:UpdateItem", 30 | "dynamodb:DeleteItem", 31 | "dynamodb:BatchWriteItem", 32 | "dynamodb:Describe*", 33 | "dynamodb:Get*", 34 | "dynamodb:List*" 35 | ] 36 | resources = ["${aws_dynamodb_table.dynamodb_tfstate_lock.arn}"] 37 | } 38 | } 39 | 40 | resource "aws_iam_policy" "codebuild_policy" { 41 | name = var.codebuild_policy_name 42 | path = "/" 43 | policy = data.aws_iam_policy_document.codebuild_policy_document.json 44 | } 45 | 46 | resource "aws_iam_role" "codebuild_role" { 47 | name = var.codebuild_role_name 48 | 49 | assume_role_policy = <