forked from 56kcloud/aws-iot-reference-architecture
-
Notifications
You must be signed in to change notification settings - Fork 0
82 lines (71 loc) · 2.97 KB
/
destroy-infra.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
name: Reference architecture - destroy infrastructure
# Enable to run this workflow manually
on: workflow_dispatch
env:
# Must be set
OIDC_ROLE_AWS: ${{ secrets.OIDC_ROLE_AWS }}
REGION: ${{ vars.AWS_REGION }}
IAC_STACK_NAME: ${{ vars.IAC_STACK_NAME }}
IAC_BUCKET_NAME: ${{ vars.IAC_BUCKET_NAME }}
CERT_BUCKET_NAME: ${{ vars.CERT_BUCKET_NAME }}
IMAGE_BUCKET_NAME: ${{ vars.IMAGE_BUCKET_NAME }}
# Optional
THING_POLICY_NAME: GreengrassV2IoTThingPolicy
# Don't change
provisioning-directory: ./cloud-infrastructure
jobs:
# Destroy infrastructure
destroy-infra:
name: Destroy infrastructure
runs-on: ubuntu-latest
defaults:
run:
working-directory: ${{ env.provisioning-directory }}
env:
PULUMI_CONFIG_PASSPHRASE: ${{ secrets.PULUMI_CONFIG_PASSPHRASE }}
permissions:
id-token: write
contents: read
steps:
- name: Check out repository code
uses: actions/checkout@v4
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ env.OIDC_ROLE_AWS }} # This is required for requesting the JWT
aws-region: ${{ env.REGION }} # This is required for actions/checkout
- name: Empty ${{ env.IMAGE_BUCKET_NAME }} bucket
run: |
if aws s3api head-bucket --bucket ${{ env.IMAGE_BUCKET_NAME }} 2> /dev/null; then
aws s3 rm s3://${{ env.IMAGE_BUCKET_NAME }} --recursive
else
echo "Bucket does not exist."
fi
- name: Empty ${{ env.CERT_BUCKET_NAME }} bucket
run: |
if aws s3api head-bucket --bucket ${{ env.CERT_BUCKET_NAME }} 2> /dev/null; then
aws s3 rm s3://${{ env.CERT_BUCKET_NAME }}/config_parameters.txt
aws s3 rm s3://${{ env.CERT_BUCKET_NAME }}/allowlist.txt
else
echo "Bucket does not exist."
fi
- name: Detach thing policy from certificates
run: |
certificates_arn=$(aws iot list-certificates --output text --no-paginate --query "certificates[].certificateArn")
for certificate_arn in $certificates_arn; do
aws iot detach-policy --policy-name ${{ env.THING_POLICY_NAME }} --target $certificate_arn
done
- name: Pulumi Login
run: pulumi login --cloud-url s3://${{ env.IAC_BUCKET_NAME }}
- name: Destroy infrastructure
run: pulumi destroy -s ${{ env.IAC_STACK_NAME }} --yes
- name: Pulumi remove stack
run: pulumi stack rm ${{ env.IAC_STACK_NAME }} --yes
- name: Delete S3 bucket from Pulumi stack
run: |
if aws s3api head-bucket --bucket ${{ env.IAC_BUCKET_NAME }} 2> /dev/null; then
aws s3 rm s3://${{ env.IAC_BUCKET_NAME }} --recursive
aws s3api delete-bucket --bucket ${{ env.IAC_BUCKET_NAME }}
else
echo "Bucket does not exist."
fi