-
Notifications
You must be signed in to change notification settings - Fork 77
90 lines (76 loc) · 2.68 KB
/
tf_cloud_aws.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go
# It is reusable workflow that can be called in other workflows
name: AWS Infra Creation Using in TF Cloud
on:
workflow_call:
secrets:
TF_API_TOKEN:
required: true
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
workflow_dispatch:
env:
tfcode_path: tfcloud_samples/amazon_ec2
jobs:
aws_tfc_job:
name: Create AWS Infra Using TFC
runs-on: ubuntu-latest
steps:
- name: Checkout tf code in runner environment
uses: actions/[email protected]
# Configure Terraform cloud API token, since we are using Remote backend option of Terraform cloud in AWS code
- name: Setup Terraform CLI
uses: hashicorp/[email protected]
with:
cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}
# Add the AWS Creds as ENV variable in TF Cloud workspace, since the tf run happens in TF Cloud environment
# Invoke the Terraform commands
- name: Terraform init and validate
run: |
echo `pwd`
echo "** Running Terraform Init**"
terraform init
echo "** Running Terraform Validate**"
terraform validate
working-directory: ${{ env.tfcode_path }}
- name: Terraform Plan
run: |
echo "** Running Terraform Plan**"
# terraform plan -out=tfplan
terraform plan
working-directory: ${{ env.tfcode_path }}
- name: Upload Terraform Plan
uses: actions/upload-artifact@v2
with:
name: terraform-plan
path: ${{ env.tfcode_path }}/tfplan
# Once the user verifies the Terraform Plan, the user can run the Terraform Apply and Destroy commands
apply_terraform_plan:
needs: aws_tfc_job
if: github.event_name == 'workflow_dispatch'
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/[email protected]
- name: Setup Terraform CLI
uses: hashicorp/[email protected]
with:
cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}
- name: Download Terraform Plan
uses: actions/download-artifact@v2
with:
name: terraform-plan
path: ${{ env.tfcode_path }}
- name: Terraform Apply
run: |
echo "** Running Terraform Apply**"
terraform apply -auto-approve tfplan
working-directory: ${{ env.tfcode_path }}
- name: Terraform Destroy
run: |
echo "** Running Terraform Destroy**"
terraform destroy -auto-approve
working-directory: ${{ env.tfcode_path }}