-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathservice_bootstrap.py
160 lines (144 loc) · 5.72 KB
/
service_bootstrap.py
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You may
# not use this file except in compliance with the License. A copy of the
# License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is distributed
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
# express or implied. See the License for the specific language governing
# permissions and limitations under the License.
"""Bootstraps the resources required to run the Lambda integration tests.
"""
import os
import boto3
import logging
from zipfile import ZipFile
from e2e import bootstrap_directory
from e2e.bootstrap_resources import BootstrapResources
from botocore.exceptions import ClientError
from acktest.bootstrapping import Resources, BootstrapFailureException
from acktest.bootstrapping.s3 import Bucket
from acktest.bootstrapping.dynamodb import Table
from acktest.bootstrapping.signer import SigningProfile
from acktest.bootstrapping.sqs import Queue
from acktest.bootstrapping.iam import Role
LAMBDA_IAM_ROLE_POLICY = '{"Version": "2012-10-17","Statement": [{ "Effect": "Allow", "Principal": {"Service": '\
'"lambda.amazonaws.com"}, "Action": "sts:AssumeRole"}]} '
LAMBDA_BASIC_EXECUTION_ARN = 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole'
LAMBDA_DYNAMODB_EXECUTION_ROLE = 'arn:aws:iam::aws:policy/service-role/AWSLambdaDynamoDBExecutionRole'
LAMBDA_SQS_QUEUE_EXECUTION_ROLE = 'arn:aws:iam::aws:policy/AmazonSQSFullAccess'
BASIC_ROLE_POLICIES = [ LAMBDA_BASIC_EXECUTION_ARN ]
ESM_ROLE_POLICIES = [ LAMBDA_BASIC_EXECUTION_ARN, LAMBDA_DYNAMODB_EXECUTION_ROLE, LAMBDA_SQS_QUEUE_EXECUTION_ROLE ]
EIC_ROLE_POLICIES = [LAMBDA_BASIC_EXECUTION_ARN, LAMBDA_SQS_QUEUE_EXECUTION_ROLE]
LAMBDA_FUNCTION_FILE = "main.py"
LAMBDA_FUNCTION_FILE_ZIP = "main.zip"
LAMBDA_FUNCTION_FILE_PATH = f"./resources/lambda_function/{LAMBDA_FUNCTION_FILE}"
LAMBDA_FUNCTION_FILE_PATH_ZIP = f"./resources/lambda_function/{LAMBDA_FUNCTION_FILE_ZIP}"
LAMBDA_FUNCTION_UPDATED_FILE = "updated_main.py"
LAMBDA_FUNCTION_UPDATED_FILE_ZIP = "updated_main.zip"
LAMBDA_FUNCTION_UPDATED_FILE_PATH = f"./resources/lambda_function/{LAMBDA_FUNCTION_UPDATED_FILE}"
LAMBDA_FUNCTION_UPDATED_FILE_PATH_ZIP = f"./resources/lambda_function/{LAMBDA_FUNCTION_UPDATED_FILE_ZIP}"
AWS_SIGNING_PLATFORM_ID = "AWSLambda-SHA384-ECDSA"
def zip_function_file(src: str, dst: str):
with ZipFile(dst, 'w') as zipf:
zipf.write(src, arcname=src)
def upload_function_to_bucket(file_path: str, bucket_name: str):
object_name = os.path.basename(file_path)
s3_client = boto3.client('s3')
try:
s3_client.upload_file(
file_path,
bucket_name,
object_name,
)
except ClientError as e:
logging.error(e)
logging.info(f"Uploaded {file_path} to bucket {bucket_name}")
def service_bootstrap() -> Resources:
logging.getLogger().setLevel(logging.INFO)
resources = BootstrapResources(
FunctionsBucket=Bucket(
"ack-lambda-controller-tests",
),
SigningProfile=SigningProfile(
"ack_testing_signer",
signing_platform_id=AWS_SIGNING_PLATFORM_ID,
),
BasicRole=Role(
"ack-lambda-controller-basic-role",
principal_service="lambda.amazonaws.com",
managed_policies=BASIC_ROLE_POLICIES,
),
ESMRole=Role(
"ack-lambda-controller-esm-role",
principal_service="lambda.amazonaws.com",
managed_policies=ESM_ROLE_POLICIES,
),
ESMTable=Table(
"ack-lambda-controller-table",
attribute_definitions=[
{
'AttributeName': 'id',
'AttributeType': 'N'
},
{
'AttributeName': 'createdAt',
'AttributeType': 'S'
},
],
key_schema=[
{
'AttributeName': 'id',
'KeyType': 'HASH'
},
{
'AttributeName': 'createdAt',
'KeyType': 'RANGE'
}
],
stream_specification={
'StreamEnabled': True,
'StreamViewType': 'NEW_IMAGE'
},
provisioned_throughput={
'ReadCapacityUnits': 5,
'WriteCapacityUnits': 5
},
),
ESMQueue=Queue(
"ack-lambda-controller-queue",
),
EICRole=Role(
"ack-lambda-controller-eic-role",
principal_service="lambda.amazonaws.com",
managed_policies=EIC_ROLE_POLICIES,
),
EICQueueOnSuccess=Queue(
"ack-lambda-controller-function-queue-eic-onsuccess"
),
EICQueueOnFailure=Queue(
"ack-lambda-controller-function-queue-eic-onfailure"
)
)
try:
resources.bootstrap()
zip_function_file(LAMBDA_FUNCTION_FILE_PATH, LAMBDA_FUNCTION_FILE_PATH_ZIP)
upload_function_to_bucket(
LAMBDA_FUNCTION_FILE_PATH_ZIP,
resources.FunctionsBucket.name,
)
zip_function_file(LAMBDA_FUNCTION_UPDATED_FILE_PATH, LAMBDA_FUNCTION_UPDATED_FILE_PATH_ZIP)
upload_function_to_bucket(
LAMBDA_FUNCTION_UPDATED_FILE_PATH_ZIP,
resources.FunctionsBucket.name,
)
except BootstrapFailureException as ex:
exit(254)
return resources
if __name__ == "__main__":
config = service_bootstrap()
# Write config to current directory by default
config.serialize(bootstrap_directory)