4
4
import zipfile
5
5
from enum import Enum
6
6
from pathlib import Path
7
- from typing import Any
7
+ from typing import Dict , List
8
8
9
9
import boto3
10
10
import yaml
@@ -29,13 +29,13 @@ def __init__(self, stack_name: str, handlers_dir: str, config: dict, environment
29
29
self .cf_client = session .client ("cloudformation" )
30
30
self .s3_resource = session .resource ("s3" )
31
31
self .account_id = session .client ("sts" ).get_caller_identity ()["Account" ]
32
- self .region = boto3 . Session () .region_name
32
+ self .region = session .region_name
33
33
self .stack_name = stack_name
34
34
self .handlers_dir = handlers_dir
35
35
self .config = config
36
36
self .environment_variables = environment_variables
37
37
38
- def deploy (self ) -> dict [str , str ]:
38
+ def deploy (self ) -> Dict [str , str ]:
39
39
handlers = self ._find_files (directory = self .handlers_dir , only_py = True )
40
40
template , asset_root_dir = self .prepare_stack (
41
41
handlers = handlers ,
@@ -55,7 +55,7 @@ def delete(self):
55
55
56
56
# Create CDK cloud assembly code
57
57
def prepare_stack (
58
- self , handlers : list [str ], handlers_dir : str , stack_name : str , environment_variables : dict , ** config : dict
58
+ self , handlers : List [str ], handlers_dir : str , stack_name : str , environment_variables : dict , ** config : dict
59
59
):
60
60
integration_test_app = App ()
61
61
stack = Stack (integration_test_app , stack_name )
@@ -99,17 +99,16 @@ def _find_files(self, directory: str, only_py: bool = False) -> list:
99
99
file_paths .append (os .path .join (root , filename ))
100
100
return file_paths
101
101
102
- def _create_layer (self , stack ):
103
- output_dir = Path (AssetStaging .BUNDLING_OUTPUT_DIR , "python" )
104
- input_dir = Path (AssetStaging .BUNDLING_INPUT_DIR , "aws_lambda_powertools" )
102
+ def _create_layer (self , stack : Stack ):
103
+ output_dir = Path (str ( AssetStaging .BUNDLING_OUTPUT_DIR ) , "python" )
104
+ input_dir = Path (str ( AssetStaging .BUNDLING_INPUT_DIR ) , "aws_lambda_powertools" )
105
105
powertools_layer = LayerVersion (
106
106
stack ,
107
107
"aws-lambda-powertools" ,
108
108
layer_version_name = "aws-lambda-powertools" ,
109
109
compatible_runtimes = [PythonVersion [PYTHON_RUNTIME_VERSION ].value ["runtime" ]],
110
110
code = Code .from_asset (
111
111
path = "." ,
112
- exclude = ["*.pyc" ],
113
112
bundling = BundlingOptions (
114
113
image = DockerImage .from_build (
115
114
str (Path (__file__ ).parent ),
@@ -118,12 +117,14 @@ def _create_layer(self, stack):
118
117
command = [
119
118
"bash" ,
120
119
"-c" ,
121
- f"poetry export --with-credentials --format requirements.txt --output requirements.txt && pip install -r requirements.txt -t { output_dir } && cp -R { input_dir } { output_dir } " ,
120
+ f"poetry export --with-credentials --format requirements.txt --output /tmp/requirements.txt &&\
121
+ pip install -r /tmp/requirements.txt -t { output_dir } &&\
122
+ cp -R { input_dir } { output_dir } &&\
123
+ find { output_dir } / -regex '^.*\\ (__pycache__\\ |\\ .py[co]\\ )$' -delete" ,
122
124
],
123
125
),
124
126
),
125
127
)
126
-
127
128
return powertools_layer
128
129
129
130
def _upload_assets (self , template : dict , asset_root_dir : str ):
@@ -146,16 +147,16 @@ def _upload_assets(self, template: dict, asset_root_dir: str):
146
147
buf .seek (0 )
147
148
self .s3_client .upload_fileobj (Fileobj = buf , Bucket = bucket , Key = s3_key )
148
149
149
- def _deploy_stack (self , stack_name : str , template : Any ):
150
+ def _deploy_stack (self , stack_name : str , template : dict ):
150
151
response = self .cf_client .create_stack (
151
152
StackName = stack_name ,
152
153
TemplateBody = yaml .dump (template ),
153
154
TimeoutInMinutes = 10 ,
154
- OnFailure = "DO_NOTHING " ,
155
+ OnFailure = "ROLLBACK " ,
155
156
Capabilities = ["CAPABILITY_IAM" ],
156
157
)
157
158
waiter = self .cf_client .get_waiter ("stack_create_complete" )
158
- waiter .wait (StackName = stack_name , WaiterConfig = {"Delay" : 2 , "MaxAttempts" : 50 })
159
+ waiter .wait (StackName = stack_name , WaiterConfig = {"Delay" : 10 , "MaxAttempts" : 50 })
159
160
response = self .cf_client .describe_stacks (StackName = stack_name )
160
161
return response
161
162
0 commit comments