├── .github └── workflows │ ├── terraform.yml │ └── your-fork.yml ├── README.md └── main.tf /.github/workflows/terraform.yml: -------------------------------------------------------------------------------- 1 | name: "Terraform" 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | pull_request: 8 | 9 | jobs: 10 | terraform: 11 | name: "Terraform" 12 | runs-on: ubuntu-latest 13 | permissions: 14 | pull-requests: write 15 | steps: 16 | - name: Checkout 17 | uses: actions/checkout@v3 18 | 19 | - name: Setup Terraform 20 | uses: hashicorp/setup-terraform@v1 21 | with: 22 | # terraform_version: 0.13.0: 23 | cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }} 24 | 25 | - name: Terraform Format 26 | id: fmt 27 | run: terraform fmt -check 28 | 29 | - name: Terraform Init 30 | id: init 31 | run: terraform init 32 | 33 | - name: Terraform Validate 34 | id: validate 35 | run: terraform validate -no-color 36 | 37 | - name: Terraform Plan 38 | id: plan 39 | if: github.event_name == 'pull_request' 40 | run: terraform plan -no-color -input=false 41 | continue-on-error: true 42 | 43 | - name: Update Pull Request 44 | uses: actions/github-script@v6 45 | if: github.event_name == 'pull_request' 46 | env: 47 | PLAN: ${{ steps.plan.outputs.stdout }} 48 | with: 49 | github-token: ${{ secrets.GITHUB_TOKEN }} 50 | script: | 51 | const output = `#### Terraform Format and Style 🖌\`${{ steps.fmt.outcome }}\` 52 | #### Terraform Initialization ⚙️\`${{ steps.init.outcome }}\` 53 | #### Terraform Validation 🤖\`${{ steps.validate.outcome }}\` 54 | #### Terraform Plan 📖\`${{ steps.plan.outcome }}\` 55 | 56 |
Show Plan 57 | 58 | \`\`\`terraform\n 59 | ${process.env.PLAN} 60 | \`\`\` 61 | 62 |
63 | 64 | *Pushed by: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`; 65 | 66 | github.rest.issues.createComment({ 67 | issue_number: context.issue.number, 68 | owner: context.repo.owner, 69 | repo: context.repo.repo, 70 | body: output 71 | }) 72 | 73 | - name: Terraform Plan Status 74 | if: steps.plan.outcome == 'failure' 75 | run: exit 1 76 | 77 | - name: Terraform Apply 78 | if: github.ref == 'refs/heads/main' && github.event_name == 'push' 79 | run: terraform apply -auto-approve -input=false 80 | -------------------------------------------------------------------------------- /.github/workflows/your-fork.yml: -------------------------------------------------------------------------------- 1 | name: Your Fork 2 | 3 | on: 4 | pull_request_target: 5 | types: [opened] 6 | 7 | jobs: 8 | close: 9 | runs-on: ubuntu-latest 10 | steps: 11 | - uses: superbrothers/close-pull-request@v3 12 | with: 13 | repo-token: ${{ secrets.GITHUB_TOKEN }} 14 | # Optional. Post a issue comment just before closing a pull request. 15 | comment: "Hi! If you are following the Terraform GitHub Actions tutorial, please open the PR against [your personal fork](https://learn.hashicorp.com/tutorials/terraform/github-actions?in=terraform/automation#set-up-a-github-repository) of this repository. We will automatically close this PR, but if you intended to edit the example itself please feel free to re-open it." 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Automate Terraform with GitHub Actions 2 | 3 | 4 | This repo is a companion repo to the [Automate Terraform with GitHub Actions](https://learn.hashicorp.com/tutorials/terraform/github-actions?in=terraform/automation). 5 | -------------------------------------------------------------------------------- /main.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_providers { 3 | aws = { 4 | source = "hashicorp/aws" 5 | version = "5.44.0" 6 | } 7 | random = { 8 | source = "hashicorp/random" 9 | version = "3.6.0" 10 | } 11 | } 12 | required_version = "~> 1.0" 13 | 14 | backend "remote" { 15 | organization = "ACG-Terraform-Demos78" 16 | 17 | workspaces { 18 | name = "demo-github-actions" 19 | } 20 | } 21 | } 22 | 23 | 24 | provider "aws" { 25 | region = "us-east-1" 26 | } 27 | 28 | 29 | 30 | resource "random_pet" "sg" {} 31 | 32 | resource "aws_instance" "web" { 33 | ami = "ami-09e67e426f25ce0d7" 34 | instance_type = "t3.micro" 35 | vpc_security_group_ids = [aws_security_group.web-sg.id] 36 | 37 | user_data = <<-EOF 38 | #!/bin/bash 39 | echo "Hello, Everyone!" > index.html 40 | nohup busybox httpd -f -p 8080 & 41 | EOF 42 | } 43 | 44 | resource "aws_security_group" "web-sg" { 45 | name = "${random_pet.sg.id}-sg" 46 | ingress { 47 | from_port = 8080 48 | to_port = 8080 49 | protocol = "tcp" 50 | cidr_blocks = ["0.0.0.0/0"] 51 | } 52 | } 53 | 54 | output "web-address" { 55 | value = "${aws_instance.web.public_dns}:8080" 56 | } 57 | --------------------------------------------------------------------------------