initial commit #1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This workflow is designed to be run as needed to remove costly resources | ||
name: Terraform Cleanup - Cost Savings | ||
run-name: Terraform Cleanup - Cost Savings (Dev, Test, Prod) | ||
on: | ||
workflow_dispatch: | ||
inputs: | ||
resource_targets: | ||
description: Comma-separated list of resources to target for destruction (optional). If omitted, all resources in the selected stack will be destroyed. | ||
required: false | ||
type: string | ||
stack_name: | ||
description: Name of Terraform stack. | ||
required: true | ||
type: choice | ||
options: | ||
- api | ||
- oidc | ||
- vpc | ||
default: vpc | ||
# schedule: | ||
# - cron: '0 0 * * *' # This represents daily at 5 PM PST | ||
permissions: | ||
contents: read | ||
id-token: write | ||
env: | ||
# Comma-separated list of resources to target for destruction | ||
VPC_TARGETS: aws_eip.nat,aws_nat_gateway.nat,aws_internet_gateway.default | ||
jobs: | ||
env-vars: | ||
name: Set Env Vars as Outputs | ||
runs-on: ubuntu-latest | ||
outputs: | ||
RESOURCE_TARGETS: ${{ steps.set-targets.outputs.resource_targets }} | ||
steps: | ||
- name: Set Targets for Manual Trigger | ||
if: ${{ github.event_name == 'workflow_dispatch' }} | ||
id: set-targets | ||
run: | | ||
echo "resource_targets=${{ github.event.inputs.resource_targets }}" >> $GITHUB_ENV | ||
- name: Set Targets for Scheduled Run | ||
if: ${{ github.event_name == 'schedule' }} | ||
id: set-targets | ||
run: | | ||
# Grab preset target values from environment variable | ||
echo "resource_targets=$VPC_TARGETS" >> $GITHUB_ENV | ||
tf-destroy: | ||
name: Terraform Cleanup | ||
needs: | ||
- env-vars | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
stack_name: | ||
- vpc | ||
environment: | ||
- [dev, '${{ vars.DEV_ACCOUNT_ID }}'] | ||
- [test, '${{ vars.TEST_ACCOUNT_ID }}'] | ||
- [prod, '${{ vars.PROD_ACCOUNT_ID }}'] | ||
uses: chrisba11/terraform-feature-stacks/.github/workflows/reusable-workflows/tf_destroy.yml@main | ||
with: | ||
aws_account_id: ${{ matrix.environment[1] }} | ||
aws_region: ${{ vars.AWS_REGION }} | ||
environment: ${{ matrix.environment[0] }} | ||
resource_targets: ${{ needs.env-vars.outputs.RESOURCE_TARGETS }} | ||
role_name: GithubActionsRole-ReadOnly | ||
terraform_version: =1.7.0 | ||
tf_backend_name: ${{ vars.TF_BACKEND_PREFIX }}-${{ matrix.environment[0] }} | ||
tf_backend_key: ${{ github.event.repository.name }}/${{ matrix.stack_name }} | ||
tfvars_path: ./environments/${{ matrix.environment[0] }}.tfvars | ||
working_directory: infra/tf/stacks/${{ matrix.stack_name }} |