1+ import boto3
2+ import argparse
3+
4+
5+ VERSION = "v1.3.0"
6+ AWS_PROFILE = "prod"
7+
8+ REGION_MAPPING = {
9+ "us-east-1" : "appdevzipfiles-us-east-1" ,
10+ "us-east-2" : "appdevzipfiles-us-east-2" ,
11+ "us-west-1" : "appdevzipfiles-us-west-1" ,
12+ "us-west-2" : "appdevzipfiles-us-west-2" ,
13+ "ap-south-1" : "appdevzipfiles-ap-south-1" ,
14+ "ap-northeast-2" : "appdevzipfiles-ap-northeast-2" ,
15+ "ap-southeast-1" : "appdevzipfiles-ap-southeast-1" ,
16+ "ap-southeast-2" : "appdevzipfiles-ap-southeast-2" ,
17+ "ap-northeast-1" : "appdevzipfiles-ap-northeast-1" ,
18+ "ca-central-1" : "appdevzipfiles-ca-central-1" ,
19+ "eu-central-1" : "appdevzipfiles-eu-central-1" ,
20+ "eu-west-1" : "appdevzipfiles-eu-west-1" ,
21+ "eu-west-2" : "appdevzipfiles-eu-west-2" ,
22+ "eu-west-3" : "appdevzipfiles-eu-west-3" ,
23+ "eu-north-1" : "appdevzipfiles-eu-north-1s" ,
24+ "sa-east-1" : "appdevzipfiles-sa-east-1" ,
25+ "ap-east-1" : "appdevzipfiles-ap-east-1s" ,
26+ "af-south-1" : "appdevzipfiles-af-south-1s" ,
27+ "eu-south-1" : "appdevzipfiles-eu-south-1" ,
28+ "me-south-1" : "appdevzipfiles-me-south-1s" ,
29+ "me-central-1" : "appdevzipfiles-me-central-1" ,
30+ "eu-central-2" : "appdevzipfiles-eu-central-2ss" ,
31+ "ap-northeast-3" : "appdevzipfiles-ap-northeast-3s" ,
32+ "ap-southeast-3" : "appdevzipfiles-ap-southeast-3"
33+ }
34+
35+ def get_bucket_name (region ):
36+ return REGION_MAPPING .get (region , None )
37+
38+
39+ def create_bucket (region ):
40+ """Create an S3 bucket in the specified region."""
41+ s3 = boto3 .client ("s3" , region_name = region )
42+ bucket_name = get_bucket_name (region )
43+
44+ if not bucket_name :
45+ print (f"No bucket mapping found for region: { region } " )
46+ return
47+
48+ try :
49+ if region == "us-east-1" :
50+ response = s3 .create_bucket (Bucket = bucket_name )
51+ else :
52+ response = s3 .create_bucket (
53+ Bucket = bucket_name ,
54+ CreateBucketConfiguration = {"LocationConstraint" : region },
55+ )
56+ print (f"Bucket created: { bucket_name } in { region } " , response )
57+ except Exception as e :
58+ print (f"Error creating bucket { bucket_name } : { e } " )
59+
60+
61+ def upload_code_to_s3 (region ):
62+ """Upload the zip file to the specified S3 bucket."""
63+ filename = "cloudwatchlogs-with-dlq.zip"
64+ boto3 .setup_default_session (profile_name = AWS_PROFILE )
65+ s3 = boto3 .client ("s3" , region_name = region )
66+ bucket_name = get_bucket_name (region )
67+
68+ if not bucket_name :
69+ print (f"No bucket mapping found for region: { region } " )
70+ return
71+
72+ try :
73+ s3 .upload_file (
74+ filename , bucket_name , f"cloudwatchLogsDLQ/{ VERSION } /{ filename } " ,
75+ ExtraArgs = {"ACL" : "public-read" }
76+ )
77+ print (f"Uploaded { filename } to S3 bucket ({ bucket_name } ) in region ({ region } )" )
78+ except Exception as e :
79+ print (f"Error uploading { filename } to { bucket_name } : { e } " )
80+
81+
82+ def upload_code_in_multiple_regions (regions ):
83+ """Upload code to all or specified regions."""
84+ # for region in regions:
85+ # create_bucket(region)
86+
87+ for region in regions :
88+ upload_code_to_s3 (region )
89+
90+
91+ def deploy (args ):
92+ """Deploy production artifacts to S3."""
93+ if args .region == "all" :
94+ upload_code_in_multiple_regions (REGION_MAPPING .keys ())
95+ elif args .region in REGION_MAPPING .keys ():
96+ upload_code_to_s3 (args .region )
97+ else :
98+ print ("Invalid region. Please provide a valid AWS region or use 'all'." )
99+
100+ boto3 .setup_default_session (profile_name = AWS_PROFILE )
101+ s3 = boto3 .client ("s3" , region_name = "us-east-1" )
102+ bucket_name = "appdev-cloudformation-templates"
103+
104+ template_files = [
105+ "DLQLambdaCloudFormation.json" ,
106+ "DLQLambdaCloudFormationWithSecuredEndpoint.json"
107+ ]
108+
109+ for filename in template_files :
110+ try :
111+ s3 .upload_file (
112+ filename , bucket_name , filename ,
113+ ExtraArgs = {"ACL" : "public-read" }
114+ )
115+ print (f"Uploaded { filename } to { bucket_name } " )
116+ except Exception as e :
117+ print (f"Error uploading { filename } : { e } " )
118+
119+ print ("Deployment Successful: All files copied to Sumocontent" )
120+
121+
122+ def main ():
123+ parser = argparse .ArgumentParser (description = "Deploy files to S3" )
124+ parser .add_argument (
125+ "-r" , "--region" , type = str , help = "Specify a region or use 'all' to deploy to all configured regions"
126+ )
127+ args = parser .parse_args ()
128+ deploy (args )
129+
130+
131+
132+ if __name__ == "__main__" :
133+ main ()
0 commit comments