1- ########################################################################
2- # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3- # SPDX-License-Identifier: MIT-0
4- ########################################################################
1+ """Multi-account and region terraform deployment for AWS SRA code library.
52
6- import subprocess
3+ Version: 1.0
4+
5+ AWS SRA terraform edition in the repo, https://github.com/aws-samples/aws-security-reference-architecture-examples
6+
7+ Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
8+ SPDX-License-Identifier: MIT-0
9+ """
10+
11+ import subprocess # noqa: S404
712import argparse
813import boto3
914
10- SUPPORTED_REGIONS = []
15+ SUPPORTED_REGIONS : list = []
16+
17+
1118def init () -> None :
12- """Performs an init on the terraform project
13- """
14- subprocess . run ( f"terraform init -backend-config=backend.tfvars" , check = True , shell = True ) # nosec B602
19+ """Initialize the terraform project."""
20+ subprocess . run ( "terraform init -backend-config=backend.tfvars" , check = True , shell = True ) # nosec B602 # noqa: S602,S607
21+
1522
1623def set_supported_region () -> None :
17- """Sets The supported regions from parameter store
18- """
24+ """Set the supported regions from parameter store."""
1925 global SUPPORTED_REGIONS
2026
2127 ssm_client = boto3 .client ('ssm' )
@@ -43,30 +49,28 @@ def set_supported_region() -> None:
4349 SUPPORTED_REGIONS .remove (home_region )
4450 SUPPORTED_REGIONS .insert (0 , home_region )
4551
52+
4653def get_audit_account () -> str :
47- """Get audit account from AWS Organization
54+ """Get audit account from AWS Organization.
4855
4956 Returns:
50- string : audit account id
57+ str : audit account id
5158 """
52-
5359 ssm_client = boto3 .client ('ssm' )
5460 response = ssm_client .get_parameter (
5561 Name = "/sra/control-tower/audit-account-id" ,
5662 WithDecryption = True # Use this if the parameter is encrypted with KMS
5763 )
5864
59- audit_account = response ['Parameter' ]['Value' ]
65+ return response ['Parameter' ]['Value' ]
6066
61- return audit_account
6267
6368def get_accounts () -> list :
64- """Get all accounts from AWS Organization
69+ """Get all accounts from AWS Organization.
6570
6671 Returns:
6772 list: list of accounts in org
6873 """
69-
7074 organizations = boto3 .client ('organizations' )
7175 paginator = organizations .get_paginator ("list_accounts" )
7276
@@ -81,73 +85,84 @@ def get_accounts() -> list:
8185 if audit_account in accounts :
8286 accounts .remove (audit_account )
8387 accounts .append (audit_account )
84-
88+
8589 return accounts
8690
91+
8792def workspace_exists (account : str , region : str ) -> bool :
88- """Checks to see if workspace already exists for current terraform project
93+ """Check to see if workspace already exists for current terraform project.
8994
9095 Args:
91- account (int ): Account ID
92- region (string ): Region
96+ account (str ): Account ID
97+ region (str ): Region
9398
9499 Returns:
95- boolean : Returns true if workspace already exists, false otherwise
100+ bool : Returns true if workspace already exists, false otherwise.
96101 """
97- completed_process = subprocess .run (f"terraform workspace list | grep { account } -{ region } " , shell = True ) # nosec B602
102+ completed_process = subprocess .run (f"terraform workspace list | grep { account } -{ region } " , shell = True ) # nosec B602 # noqa: S602
98103 return completed_process .returncode == 0
99104
105+
100106def create_workspace (account : str , region : str ) -> None :
101- """Create new workspace for terraform and saves it into statefile
107+ """Create new workspace for terraform and saves it into state file.
102108
103109 Args:
104- account (int ): Account ID
105- region (string ): Region
110+ account (str ): Account ID
111+ region (str ): Region
106112 """
107- subprocess .run (f"terraform workspace new { account } -{ region } " , check = True , shell = True ) # nosec B602
113+ subprocess .run (f"terraform workspace new { account } -{ region } " , check = True , shell = True ) # nosec B602 # noqa: S602
114+
108115
109116def switch_to_workspace (account : str , region : str ) -> None :
110- """Switch to a created workspace in Terraform
117+ """Switch to a created workspace in Terraform.
111118
112119 Args:
113- account (int ): Account ID
114- region (string ): Region
120+ account (str ): Account ID
121+ region (str ): Region
115122 """
116- subprocess .run (f"terraform workspace select { account } -{ region } " , check = True , shell = True ) # nosec B602
123+ subprocess .run (f"terraform workspace select { account } -{ region } " , check = True , shell = True ) # nosec B602 # noqa: S602
124+
117125
118126def plan (account : str , region : str ) -> None :
119- """Performs a terraform plan operation on all stacks
127+ """Perform a terraform plan operation on all stacks.
120128
121129 Args:
122- account (int ): Account ID
123- region (string ): Region
130+ account (str ): Account ID
131+ region (str ): Region
124132 """
125- subprocess .run (f"terraform plan -var-file=config.tfvars -var account_id={ account } -var account_region={ region } " , check = True , shell = True ) # nosec B602
133+ subprocess .run (f"terraform plan -var-file=config.tfvars -var account_id={ account } -var account_region={ region } " ,
134+ check = True , shell = True ) # nosec B602 # noqa: S602
135+
126136
127137def apply (account : str , region : str ) -> None :
128- """Performs a terraform apply operation on all stacks
138+ """Perform a terraform apply operation on all stacks.
129139
130140 Args:
131- account (int ): Account ID
132- region (string ): Region
141+ account (str ): Account ID
142+ region (str ): Region
133143 """
134- subprocess .run (f"terraform apply -var-file=config.tfvars -var account_id={ account } -var account_region={ region } -auto-approve" , check = True , shell = True ) # nosec B602
144+ subprocess .run (f"terraform apply -var-file=config.tfvars -var account_id={ account } -var account_region={ region } -auto-approve" ,
145+ check = True , shell = True ) # nosec B602 # noqa: S602
146+
135147
136148def destroy (account : str , region : str ) -> None :
137- """Performs a terraform destroy operation on all stacks
149+ """Perform a terraform destroy operation on all stacks.
138150
139151 Args:
140- account (int ): Account ID
141- region (string ): Region
152+ account (str ): Account ID
153+ region (str ): Region
142154 """
143- subprocess .run (f"terraform destroy -var-file=config.tfvars -var account_id={ account } -var account_region={ region } -auto-approve" , check = True , shell = True ) # nosec B602
155+ subprocess .run (f"terraform destroy -var-file=config.tfvars -var account_id={ account } -var account_region={ region } -auto-approve" ,
156+ check = True , shell = True ) # nosec B602 # noqa: S602
157+
144158
145- def main () -> None :
159+ def main () -> None : # noqa: CCR001
160+ """Run the script."""
146161 # parse arguments
147162 parser = argparse .ArgumentParser (description = "Terraform Script to Deploy Stacksets" )
148163 parser .add_argument ("cmd" , help = "terraform command to run" )
149164 args = parser .parse_args ()
150-
165+
151166 set_supported_region ()
152167
153168 if args .cmd == "init" :
@@ -177,5 +192,6 @@ def main() -> None:
177192 switch_to_workspace (account , region )
178193 destroy (account , region )
179194
195+
180196if __name__ == "__main__" :
181- main ()
197+ main ()
0 commit comments