Delete eks-terraform cluster #12
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
name: Delete eks-terraform cluster | |
on: | |
workflow_dispatch: | |
inputs: | |
name: | |
type: string | |
default: "" | |
description: Name of the cluster | |
region: | |
type: choice | |
description: AWS Region to deploy | |
default: "eu-central-2" | |
options: | |
- "eu-central-1" | |
- "eu-west-1" | |
- "eu-west-2" | |
- "eu-west-3" | |
- "eu-north-1" | |
- "sa-east-1" | |
env: | |
STATE_BUCKET: "grapesstate" | |
STATE_BUCKET_REGION: "eu-west-1" | |
jobs: | |
destroy: | |
runs-on: ubuntu-latest | |
permissions: | |
id-token: write | |
contents: read | |
steps: | |
- name: configure aws credentials | |
uses: aws-actions/configure-aws-credentials@v4 | |
with: | |
role-to-assume: arn:aws:iam::351425708426:role/grapes | |
role-session-name: grapes | |
aws-region: ${{ github.event.inputs.region }} | |
- name: Grab Terraform files from bucket | |
run: aws --region ${{ env.STATE_BUCKET_REGION }} s3 sync s3://${{ env.STATE_BUCKET }}/${{ github.event.inputs.name }} eks-terraform | |
- name: Save Terraform configuration to next job | |
uses: actions/upload-artifact@v3 | |
with: | |
name: state-${{ github.event.inputs.name }} | |
path: eks-terraform | |
- name: Setup terraform | |
uses: hashicorp/setup-terraform@v3 | |
with: | |
terraform_version: 1.6.5 | |
terraform_wrapper: false | |
- name: Terraform Init | |
id: init | |
run: terraform -chdir=eks-terraform init -backend-config=s3.tfbackend | |
- name: Terraform Destroy | |
run: terraform -chdir=eks-terraform destroy -auto-approve -input=false -var-file vars.tfvars | |
- name: Delete cluster user | |
if: always() | |
run: | | |
id=$(aws iam list-access-key --user-name ${{ github.event.inputs.name }} | jq '.AccessKeyMetadata[0].AccessKeyId' | tr -d "\"") | |
aws iam delete-access-key --user-name ${{ github.event.inputs.name }} --access-key-id $id | |
aws iam detach-user-policy --user-name ${{ github.event.inputs.name }} --policy-arn "arn:aws:iam::aws:policy/AdministratorAccess" | |
aws iam delete-user --user-name ${{ github.event.inputs.name }} | |
- name: Delete state + config | |
if: always() | |
run: | | |
aws --region ${{ env.STATE_BUCKET_REGION }} s3 rm s3://${{ env.STATE_BUCKET }}/${{ github.event.inputs.name }} --recursive | |
aws --region ${{ env.STATE_BUCKET_REGION }} s3 rm s3://${{ env.STATE_BUCKET }}/${{ github.event.inputs.name }}-state |