├── .github └── workflows │ ├── master.yml │ └── pr.yml ├── .gitignore ├── README.md └── main.tf /.github/workflows/master.yml: -------------------------------------------------------------------------------- 1 | 2 | name: Deploy 3 | 4 | on: 5 | push: 6 | branches: 7 | - master 8 | 9 | jobs: 10 | Terraform: 11 | name: Terraform Plan & Apply 12 | runs-on: ubuntu-latest 13 | steps: 14 | 15 | - name: Checkout Repo 16 | uses: actions/checkout@v2 17 | 18 | - name: Terraform Setup 19 | uses: hashicorp/setup-terraform@v1 20 | 21 | - name: Terraform Init 22 | run: terraform init 23 | env: 24 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 25 | TF_ACTION_WORKING_DIR: '.' 26 | AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} 27 | AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} 28 | 29 | - name: Terraform validate 30 | run: terraform validate 31 | 32 | - name: Terraform Apply 33 | run: terraform apply -auto-approve 34 | env: 35 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 36 | TF_ACTION_WORKING_DIR: '.' 37 | AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} 38 | AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} 39 | -------------------------------------------------------------------------------- /.github/workflows/pr.yml: -------------------------------------------------------------------------------- 1 | 2 | name: Pull Request 3 | 4 | on: 5 | pull_request: 6 | branches: 7 | - master 8 | 9 | jobs: 10 | Terraform: 11 | name: Terraform Plan 12 | runs-on: ubuntu-latest 13 | steps: 14 | 15 | - name: Checkout Repo 16 | uses: actions/checkout@v2 17 | 18 | - name: Terraform Setup 19 | uses: hashicorp/setup-terraform@v1 20 | 21 | - name: Terraform Init 22 | run: terraform init 23 | env: 24 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 25 | TF_ACTION_WORKING_DIR: '.' 26 | AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} 27 | AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} 28 | 29 | - name: Terraform Validate 30 | run: terraform validate 31 | 32 | - name: Terraform Plan 33 | run: terraform plan 34 | env: 35 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 36 | TF_ACTION_WORKING_DIR: '.' 37 | AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} 38 | AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} 39 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .terraform 2 | terraform.tfstate* 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # Terraform Github Actions Bootstrap 3 | 4 | Allows you to bootstrap a Terraform project on AWS using Github Actions. The purpose of the project is to make a simple sandbox for experimenting with Terraform resources using a CI pipeline. 5 | 6 | For the companion article, check out: https://www.thedevcoach.co.uk/setup-terraform-aws-github-actions/ 7 | 8 | ## Setup Steps 9 | 10 | Pre-requisites: 11 | * A setup AWS account 12 | * Git installed on your machine 13 | 14 | ### Step 1: Create the backend bucket 15 | 16 | 1. Clone the repo `git@github.com:loujaybee/terraform-aws-github-action-bootstrap.git` 17 | 2. Install the [Terraform](https://www.terraform.io/downloads.html) binary 18 | 3. Set your bash variables locally 19 | * `export AWS_ACCESS_KEY_ID=[your-key]` 20 | * `export AWS_SECRET_ACCESS_KEY=[your-key]` 21 | 4. `terraform init` to initialise Terraform 22 | 5. Update the `main.tf` file and set `bucket` property of the backend and s3 resource blocks (yes, even the one that's commented out, we'll need it as part of step 8) 23 | 6. Execute `terraform apply` (type `yes`) 24 | 25 | ### Step 2: Run Terrafrom on Github Actions 26 | 27 | 7. Uncomment the backend configuration in `main.tf` 28 | 8. Execute `terraform init` (type `yes` to move your state) 29 | 9. Set your AWS `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` as repo secrets @ github.com/[your-username]/[your-repo]/settings/secrets/new 30 | 10. `git add .` and `git commit -m "First commit"` to commit any changes 31 | 11. `git push` to push to github 32 | 33 | ## Having Problems? 34 | 35 | [Raise an issue](https://github.com/loujaybee/terraform-aws-github-action-bootstrap/issues) 36 | -------------------------------------------------------------------------------- /main.tf: -------------------------------------------------------------------------------- 1 | provider "aws" { 2 | version = "~> 2.0" 3 | region = "eu-central-1" 4 | } 5 | 6 | # terraform { 7 | # backend "s3" { 8 | # bucket = "example-terraform-project-name-bootstrap-terraform-state" 9 | # key = "default-infrastructure" 10 | # region = "eu-central-1" 11 | # } 12 | # } 13 | 14 | resource "aws_s3_bucket" "terraform_state" { 15 | bucket = "example-terraform-project-name-bootstrap-terraform-state" 16 | 17 | versioning { 18 | enabled = true 19 | } 20 | } 21 | --------------------------------------------------------------------------------