├── .gitignore ├── LICENSE ├── README.md ├── fabfile.py ├── fabfile_config_template.py ├── lambda_functions ├── combine_dicts.py ├── copy_keys.py ├── delete_orphaned_keys.py ├── get_bucket_location.py ├── list_bucket.py └── validate_input.py ├── requirements.txt └── state_machines └── sync_buckets_state_machine.yaml /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | fabfile_config.py 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SyncBucketsStateMachine 2 | 3 | ## Legal notice 4 | 5 | Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. 6 | Licensed under the Apache License, Version 2.0 (the "License"). 7 | You may not use this file except in compliance with the License. 8 | A copy of the License is located at 9 | 10 | http://aws.amazon.com/apache2.0/ 11 | 12 | or in the "license" file accompanying this file. 13 | This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | See the License for the specific language governing permissions and limitations under the License. 15 | 16 | ## Introduction 17 | 18 | This AWS Step Functions (SFN) state machine is designed to one-way synchronize an Amazon S3 source bucket 19 | into an Amazon S3 destination bucket as follows: 20 | 21 | * All files in the source bucket that are not present in the destination bucket or don't match their destination ETag 22 | are copied from source to destination. 23 | * All files in the destination bucket that are not present in the source bucket are deleted. 24 | 25 | This code is purely meant for illustration/demonstration purposes, please use it at your own risk. Although it has been 26 | developed carefully and with the best intentions in mind, there is no guarantee and there may be bugs. It **will copy 27 | real files** and it **will delete real files** in your Amazon S3 buckets in case it deems so necessary. To avoid any 28 | damage, please use it only with Amazon S3 buckets that contain purely test and demonstration data. 29 | 30 | ## Prerequisites 31 | 32 | You will need a system with Python 2.7 and virtualenv (https://virtualenv.pypa.io/en/stable/installation/) installed, 33 | and an AWS account that is configured on your system to be ready to use with the AWS CLI. 34 | 35 | (We won't use the AWS CLI but will use the AWS credentials stored in its configuration files.) 36 | 37 | ## How to install 38 | 39 | > sudo yum install -y gcc libffi-devel openssl-devel # Make sure some prerequisites are installed. 40 | > virtualenv env # Create a Python virtual environment. 41 | > cd env; . ./bin/activate # Activate the Python virtual environment. 42 | > git clone https://github.com/awslabs/sync-buckets-state-machine # Clone the software from this Git repository. 43 | > cd sync-buckets-state-machine 44 | > pip install -r requirements.txt # This will also install the "fab" utility from http://www.fabfile.org. 45 | > cp fabfile_config_template.py fabfile_config.py 46 | > vi fabfile_config.py # Fill in your own values. 47 | > fab # Install everything into your AWS account. 48 | 49 | ## How to use 50 | 51 | Start the Amazon Step Functions console in your chosen region and start a new execution with an input like: 52 | 53 | ```json 54 | { 55 | "source": "your-source-bucket-name", 56 | "destination": "your-destination-bucket-name" 57 | } 58 | ``` 59 | 60 | Optionally sync based on a prefix: 61 | 62 | ```json 63 | { 64 | "source": "...", 65 | "destination": "...", 66 | "prefix": "images/" 67 | } 68 | ``` 69 | 70 | Optionally sync after a specific key: 71 | 72 | ```json 73 | { 74 | "source": "...", 75 | "destination": "...", 76 | "startAfter": "images/1000" 77 | } 78 | ``` 79 | 80 | ## How to uninstall 81 | 82 | This assumes that you're still working from the sync-buckets-state-machine that you installed into in the steps above. 83 | 84 | > fab delete # Delete the CloudFormation stack and its resources. 85 | > deactivate # Deactivate the Python virtual environment 86 | > cd ../..; /bin/rm -rf env # Clean up. 87 | 88 | ## Files/directories 89 | 90 | * *lambda_functions*: All AWS Lambda functions are stored here. They contain YAML front matter with their configuration. 91 | * *state_machines*: All AWS Step Functions state machine definitions are stored here in YAML. 92 | * *fabfile.py*: Python fabric file that builds a CloudFormation stack with all Lambda functions and their configuration. 93 | It extracts configuration information from each Lambda function source file's YAML front matter and uses it to 94 | generate AWS CloudFormation snippets for the AWS Serverless Application Model (SAM) to simplify deployment. 95 | It also creates an IAM Role resource in the CloudFormation template for the Step Functions state machine. After 96 | creating or updating the CloudFormation stack, it proceeds to create/update the Step Functions state machine, using 97 | a timestamp suffix to distinguish different state machine versions from each other. 98 | * *README*: This file. 99 | * *requirements.txt*: Python requirements for this project. 100 | 101 | ## Feedback 102 | 103 | Please send feedback, suggestions, etc. to glez@amazon.de (Constantin Gonzalez) 104 | -------------------------------------------------------------------------------- /fabfile.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # coding: utf-8 3 | 4 | # Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. 5 | # Licensed under the Apache License, Version 2.0 (the "License"). 6 | # You may not use this file except in compliance with the License. 7 | # A copy of the License is located at 8 | # 9 | # http://aws.amazon.com/apache2.0/ 10 | # 11 | # or in the "license" file accompanying this file. 12 | # This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and limitations under the License. 14 | 15 | # 16 | # Fabfile for running state machines that synchronize Amazon S3 bucket contents. 17 | # 18 | 19 | # Imports 20 | 21 | # All user configurable options are kept in the fabfile_config.py file that is imported here. 22 | from fabfile_config import * 23 | 24 | from fabric.api import task 25 | import logging 26 | import json 27 | import boto3 28 | from botocore.exceptions import ClientError 29 | import time 30 | from io import BytesIO 31 | from zipfile import ZipFile, ZIP_DEFLATED 32 | import os 33 | from datetime import datetime 34 | import yaml 35 | from hashlib import md5 36 | 37 | # Constants 38 | 39 | USER_HASH = md5(USER_EMAIL).hexdigest() 40 | CONSTANTS_TO_ENVIRONMENT = [ 41 | 'AWS_DEFAULT_REGION', 42 | 'AWS_DEFAULT_PROFILE' 43 | ] 44 | for c in CONSTANTS_TO_ENVIRONMENT: 45 | if c in globals(): 46 | os.environ[c] = globals()[c] 47 | 48 | SLEEP_TIME = 5 # seconds 49 | 50 | # Lambda 51 | LAMBDA_FUNCTION_DEPLOYMENT_BUCKET = USER_HASH + '-' + AWS_DEFAULT_REGION + '-ld' 52 | LAMBDA_FUNCTION_CODE_URI_PREFIX = 's3://' + LAMBDA_FUNCTION_DEPLOYMENT_BUCKET + '/' 53 | LAMBDA_FUNCTION_DIRECTORY = 'lambda_functions' 54 | LAMBDA_DEFAULT_RUNTIME = 'python2.7' 55 | LAMBDA_DEFAULT_DESCRIPTION = 'An AWS Lambda function.' 56 | LAMBDA_DEFAULT_MEMORY_SIZE = 128 # MB 57 | LAMBDA_DEFAULT_TIMEOUT = 30 # seconds 58 | LAMBDA_DEFAULT_POLICY = 'AWSLambdaBasicExecutionRole' 59 | LAMBDA_DEFAULT_PARAMETERS = { 60 | 'Description': LAMBDA_DEFAULT_DESCRIPTION, 61 | 'Runtime': LAMBDA_DEFAULT_RUNTIME, 62 | 'MemorySize': LAMBDA_DEFAULT_MEMORY_SIZE, 63 | 'Timeout': LAMBDA_DEFAULT_TIMEOUT, 64 | 'Policies': [LAMBDA_DEFAULT_POLICY], 65 | } 66 | 67 | 68 | # Step Functions 69 | 70 | STATE_MACHINE_DIRECTORY = 'state_machines' 71 | 72 | # Use the first state machine definition name as the overall APP_NAME. Feel free to override in fabfile_config.py 73 | if os.path.exists(STATE_MACHINE_DIRECTORY): 74 | MAIN_APP_DIRECTORY = STATE_MACHINE_DIRECTORY 75 | else: 76 | MAIN_APP_DIRECTORY = LAMBDA_FUNCTION_DIRECTORY 77 | 78 | APP_NAME = globals().get( 79 | 'APP_NAME', 80 | str([os.path.splitext(sm)[0] for sm in os.listdir(MAIN_APP_DIRECTORY)][0]) # type check wants str(). 81 | ) 82 | APP_NAME = APP_NAME.replace('_', '-') # Make Cloudformation naming conventions happy. 83 | 84 | STATE_MACHINE_ROLE_POSTFIX = 'Role' 85 | STATE_MACHINE_TRUSTED_ENTITY = 'states.' + AWS_DEFAULT_REGION + '.amazonaws.com' 86 | STATE_MACHINE_DEFAULT_POLICIES = ['AWSLambdaRole'] 87 | 88 | 89 | # CloudFormation 90 | 91 | CFN_STACK_NAME = APP_NAME + '-stack' 92 | CFN_STACK_CHANGE_SET_NAME = APP_NAME + '-change-set' 93 | 94 | CFN_TEMPLATE = { 95 | 'AWSTemplateFormatVersion': '2010-09-09', 96 | 'Transform': 'AWS::Serverless-2016-10-31', 97 | 'Resources': {}, 98 | 'Outputs': {} 99 | } 100 | 101 | CFN_TEMPLATE_LAMBDA_FUNCTION = { 102 | 'Type': 'AWS::Serverless::Function', 103 | 'Properties': LAMBDA_DEFAULT_PARAMETERS 104 | } 105 | 106 | CFN_TEMPLATE_ROLE = { 107 | 'Type': 'AWS::IAM::Role', 108 | 'Properties': { 109 | 'AssumeRolePolicyDocument': { 110 | 'Version': '2012-10-17', 111 | 'Statement': [ 112 | { 113 | 'Sid': 'TrustPolicy', 114 | 'Effect': 'Allow', 115 | 'Principal': { 116 | 'Service': '' 117 | }, 118 | 'Action': 'sts:AssumeRole' 119 | } 120 | ] 121 | }, 122 | 'ManagedPolicyArns': [], 123 | } 124 | } 125 | 126 | CFN_TEMPLATE_OUTPUT = { 127 | 'Description': '', 128 | 'Value': { 129 | 'Fn::GetAtt': [ 130 | '', 131 | 'Arn' 132 | ] 133 | } 134 | } 135 | 136 | CFN_TEMPLATE_STATE_MACHINE = { 137 | 'Type': 'AWS::StepFunctions::StateMachine', 138 | 'Properties': { 139 | 'DefinitionString': None, 140 | 'RoleArn': None 141 | } 142 | } 143 | 144 | 145 | # Globals 146 | 147 | logging.basicConfig() 148 | logger = logging.getLogger() 149 | logger.setLevel(logging.WARNING) 150 | 151 | lambda_functions = {} 152 | state_machines = {} 153 | 154 | 155 | # Functions 156 | 157 | def dict_to_normalized_json(d): 158 | return json.dumps(d, sort_keys=True, indent=4) 159 | 160 | 161 | def compare_json_or_dicts(o1, o2): 162 | if isinstance(o1, basestring): 163 | json1 = dict_to_normalized_json(json.loads(o1)) 164 | else: 165 | json1 = dict_to_normalized_json(o1) 166 | 167 | if isinstance(o2, basestring): 168 | json2 = dict_to_normalized_json(json.loads(o2)) 169 | else: 170 | json2 = dict_to_normalized_json(o2) 171 | 172 | return json1 == json2 173 | 174 | 175 | def to_camel_case(s): 176 | components = s.replace('-', '_').split('_') 177 | return ''.join([i[0].upper() + i[1:].lower() for i in components]) 178 | 179 | 180 | # S3 181 | 182 | def check_bucket(bucket): 183 | s3 = boto3.client('s3', region_name=AWS_DEFAULT_REGION) 184 | 185 | print('Checking bucket: ' + bucket) 186 | try: 187 | s3.head_bucket(Bucket=bucket) 188 | except ClientError: 189 | print('Creating bucket: ' + bucket) 190 | args = { 191 | 'Bucket': bucket 192 | } 193 | if AWS_DEFAULT_REGION != 'us-east-1': 194 | args['CreateBucketConfiguration'] = { 195 | 'LocationConstraint': AWS_DEFAULT_REGION 196 | } 197 | s3.create_bucket(**args) 198 | waiter = s3.get_waiter('bucket_exists') 199 | waiter.wait(Bucket=bucket) 200 | 201 | 202 | def upload_object_to_s3(bucket, key, o): 203 | s3 = boto3.client('s3', region_name=AWS_DEFAULT_REGION) 204 | 205 | s3.put_object( 206 | Bucket=bucket, 207 | Key=key, 208 | Body=o 209 | ) 210 | 211 | 212 | def get_timestamp_from_s3_object(bucket, key): 213 | s3 = boto3.client('s3', region_name=AWS_DEFAULT_REGION) 214 | 215 | try: 216 | response = s3.get_object( 217 | Bucket=bucket, 218 | Key=key 219 | ) 220 | timestamp = response['LastModified'] # We assume this is UTC. 221 | except ClientError: 222 | timestamp = datetime(1970, 1, 1, tzinfo=None) 223 | 224 | return (timestamp.replace(tzinfo=None) - datetime(1970, 1, 1, tzinfo=None)).total_seconds() 225 | 226 | 227 | # IAM 228 | 229 | def get_arn_from_policy_name(policy_name): 230 | iam = boto3.client('iam', region_name=AWS_DEFAULT_REGION) 231 | 232 | args = { 233 | 'Scope': 'All' 234 | } 235 | while True: 236 | response = iam.list_policies(**args) 237 | for p in response['Policies']: 238 | if p['PolicyName'] == policy_name: 239 | return p['Arn'] 240 | if response['IsTruncated']: 241 | args['Marker'] = response['Marker'] 242 | else: 243 | return None 244 | 245 | 246 | # Lambda 247 | 248 | def populate_lambda_functions_dict(): 249 | global lambda_functions 250 | 251 | if not os.path.exists(LAMBDA_FUNCTION_DIRECTORY): # nothing to do. 252 | return 253 | 254 | for file_name in [ 255 | i for i in os.listdir(LAMBDA_FUNCTION_DIRECTORY) 256 | if os.path.isfile(os.path.join(LAMBDA_FUNCTION_DIRECTORY, i)) and i.endswith('.py') 257 | ]: 258 | lambda_function_name = os.path.splitext(file_name)[0] 259 | lambda_function_parameters = json.loads(json.dumps(LAMBDA_DEFAULT_PARAMETERS)) 260 | 261 | with open(os.path.join(LAMBDA_FUNCTION_DIRECTORY, file_name)) as f: 262 | yaml_lines = None 263 | while True: 264 | line = f.readline() 265 | if line == '': 266 | break 267 | if line.startswith('#'): 268 | line = line[2:] # Cut comment and one space after it. 269 | if yaml_lines is None: 270 | if line.strip() == '---': 271 | yaml_lines = [] 272 | else: 273 | if line.strip() == '---': 274 | break 275 | else: 276 | yaml_lines.append(line) 277 | 278 | if len(yaml_lines) > 0: 279 | yaml_dict = yaml.load(''.join(yaml_lines)) 280 | lambda_function_parameters.update(yaml_dict) 281 | 282 | lambda_functions[lambda_function_name] = lambda_function_parameters 283 | 284 | 285 | def generate_code_uri_for_lambda_function(lambda_function_name): 286 | return LAMBDA_FUNCTION_CODE_URI_PREFIX + lambda_function_name + '_' + str(int(time.time())) + '.zip' 287 | 288 | 289 | def find_latest_code_uri_for_lambda_function(lambda_function_name): 290 | s3 = boto3.client('s3', region_name=AWS_DEFAULT_REGION) 291 | 292 | response = s3.list_objects_v2(Bucket=LAMBDA_FUNCTION_DEPLOYMENT_BUCKET, Prefix=lambda_function_name + '_') 293 | if response['KeyCount'] == 0: 294 | return None 295 | else: 296 | sorted_result = sorted(response['Contents'], key=lambda k: k['LastModified']) 297 | latest_key = sorted_result[-1]['Key'] 298 | return LAMBDA_FUNCTION_CODE_URI_PREFIX + latest_key 299 | 300 | 301 | def generate_lambda_function_cfn_template(lambda_function_name): 302 | lambda_function_template = json.loads(json.dumps(CFN_TEMPLATE_LAMBDA_FUNCTION)) # implements deep copy. 303 | 304 | properties = lambda_function_template['Properties'] 305 | lambda_function_definition = lambda_functions[lambda_function_name] 306 | 307 | handler = lambda_function_name + '.handler' 308 | properties['Handler'] = handler 309 | 310 | code_uri = find_latest_code_uri_for_lambda_function(lambda_function_name) 311 | properties['CodeUri'] = code_uri 312 | 313 | # Overwrite the CloudFormation properties with selected properties from the function definition. 314 | for key in LAMBDA_DEFAULT_PARAMETERS.keys(): 315 | if key in lambda_function_definition and lambda_function_definition[key] is not None: 316 | properties[key] = lambda_function_definition[key] 317 | 318 | # Make sure Policies make sense. 319 | if 'Policies' not in properties or properties['Policies'] is None: 320 | properties['Policies'] = [LAMBDA_DEFAULT_POLICY] 321 | if LAMBDA_DEFAULT_POLICY not in properties['Policies']: 322 | properties['Policies'].append(LAMBDA_DEFAULT_POLICY) 323 | 324 | logical_name = to_camel_case(lambda_function_name) 325 | output_template = generate_cfn_output_template(logical_name) 326 | 327 | result = { 328 | 'Resources': { 329 | logical_name: lambda_function_template 330 | }, 331 | 'Outputs': { 332 | logical_name + 'Output': output_template 333 | } 334 | } 335 | return result 336 | 337 | 338 | def create_lambda_deployment_package(lambda_function_name): 339 | print('Creating Lambda deployment package for: ' + lambda_function_name) 340 | 341 | zip_file = BytesIO() 342 | 343 | lambda_function_file_name = lambda_function_name + '.py' 344 | with ZipFile(zip_file, 'w', ZIP_DEFLATED) as z: 345 | print('Adding: ' + lambda_function_file_name + ' to ZIP archive.') 346 | z.write(os.path.join(LAMBDA_FUNCTION_DIRECTORY, lambda_function_file_name), lambda_function_file_name) 347 | 348 | return zip_file.getvalue() 349 | 350 | 351 | def get_lambda_function_info(lambda_function_name): 352 | lambda_region = lambda_functions[lambda_function_name].get('region', AWS_DEFAULT_REGION) 353 | lambda_client = boto3.client('lambda', region_name=lambda_region) 354 | 355 | # noinspection PyBroadException 356 | try: 357 | response = lambda_client.get_function(FunctionName=lambda_function_name) 358 | except: 359 | response = None 360 | 361 | return response 362 | 363 | 364 | def update_lambda_function_package(lambda_function_name): 365 | lambda_function_path = os.path.join(LAMBDA_FUNCTION_DIRECTORY, lambda_function_name + '.py') 366 | latest_code_uri = find_latest_code_uri_for_lambda_function(lambda_function_name) 367 | if latest_code_uri is not None: 368 | latest_code_key = latest_code_uri.split('/')[-1] 369 | latest_code_uri_timestamp = get_timestamp_from_s3_object(LAMBDA_FUNCTION_DEPLOYMENT_BUCKET, latest_code_key) 370 | local_code_timestamp = os.path.getmtime(lambda_function_path) 371 | 372 | if local_code_timestamp < latest_code_uri_timestamp: 373 | print('Lambda function deployment package for: ' + lambda_function_name + ' on S3 is current.') 374 | return 375 | 376 | new_code_uri = generate_code_uri_for_lambda_function(lambda_function_name) 377 | _, _, _, new_code_key = new_code_uri.split('/') 378 | 379 | print('Creating Lambda function deployment package.') 380 | lambda_function_deployment_package = create_lambda_deployment_package(lambda_function_name) 381 | print('Uploading Lambda function deployment package as: ' + new_code_key) 382 | upload_object_to_s3(LAMBDA_FUNCTION_DEPLOYMENT_BUCKET, new_code_key, lambda_function_deployment_package) 383 | 384 | 385 | def update_lambda_function_packages(): 386 | for lambda_function_name in lambda_functions.keys(): 387 | update_lambda_function_package(lambda_function_name) 388 | 389 | 390 | # Step Functions 391 | 392 | def populate_state_machines_dict(): 393 | global state_machines 394 | 395 | if not os.path.exists(STATE_MACHINE_DIRECTORY): 396 | return 397 | 398 | for file_name in [ 399 | i for i in os.listdir(STATE_MACHINE_DIRECTORY) 400 | if os.path.isfile(os.path.join(STATE_MACHINE_DIRECTORY, i)) and i.endswith('.yaml') 401 | ]: 402 | state_machine_name = os.path.splitext(file_name)[0] 403 | with open(os.path.join(STATE_MACHINE_DIRECTORY, file_name)) as f: 404 | state_machine_dict = yaml.load(f) 405 | 406 | state_machines[state_machine_name] = state_machine_dict 407 | 408 | 409 | def generate_state_machine_cfn_template(state_machine_name): 410 | result_template = { 411 | 'Resources': {}, 412 | 'Outputs': {} 413 | } 414 | 415 | state_machine_logical_name = to_camel_case(state_machine_name) 416 | 417 | state_machine_role_name = state_machine_logical_name + STATE_MACHINE_ROLE_POSTFIX 418 | state_machine_role_template = generate_role_cfn_template( 419 | STATE_MACHINE_DEFAULT_POLICIES, STATE_MACHINE_TRUSTED_ENTITY 420 | ) 421 | result_template['Resources'][state_machine_role_name] = state_machine_role_template 422 | 423 | state_machine_definition = state_machines[state_machine_name] 424 | state_machine_json = json.dumps(state_machine_definition, indent=4) 425 | state_machine_json_lines = state_machine_json.splitlines() 426 | 427 | definition_lines = [] 428 | for line in state_machine_json_lines: 429 | if line.lstrip().startswith('"Resource":'): 430 | prefix, resource = line.split(':', 1) 431 | resource_name = resource.strip(' ",') 432 | if resource_name in lambda_functions.keys(): 433 | line = { 434 | 'Fn::Join': [ 435 | '', 436 | [ 437 | prefix, 438 | ': "', 439 | { 440 | 'Fn::GetAtt': [ 441 | to_camel_case(resource_name), 442 | 'Arn' 443 | ] 444 | }, 445 | '",' 446 | ] 447 | ] 448 | } 449 | definition_lines.append(line) 450 | 451 | definition_string_value = { 452 | 'Fn::Join': [ 453 | '\n', 454 | definition_lines 455 | ] 456 | } 457 | 458 | state_machine_template = json.loads(json.dumps(CFN_TEMPLATE_STATE_MACHINE)) 459 | state_machine_template['Properties']['DefinitionString'] = definition_string_value 460 | state_machine_template['Properties']['RoleArn'] = { 461 | 'Fn::GetAtt': [ 462 | state_machine_logical_name + 'Role', 463 | 'Arn' 464 | ] 465 | } 466 | 467 | result_template['Resources'][state_machine_logical_name] = state_machine_template 468 | 469 | output_name = state_machine_logical_name + 'Name' 470 | output_template = json.loads(json.dumps(CFN_TEMPLATE_OUTPUT)) 471 | output_template['Description'] = 'Name for State Machine: ' + state_machine_logical_name 472 | output_template['Value']['Fn::GetAtt'][0] = state_machine_logical_name 473 | output_template['Value']['Fn::GetAtt'][1] = 'Name' 474 | 475 | result_template['Outputs'][output_name] = output_template 476 | 477 | return result_template 478 | 479 | 480 | # CloudFormation 481 | 482 | def generate_cfn_output_template(resource_name): 483 | result = json.loads(json.dumps(CFN_TEMPLATE_OUTPUT)) 484 | result['Description'] = resource_name + ' ARN' 485 | result['Value']['Fn::GetAtt'][0] = resource_name 486 | 487 | return result 488 | 489 | 490 | def generate_role_cfn_template(policies, trusted_entities): 491 | result = json.loads(json.dumps(CFN_TEMPLATE_ROLE)) 492 | policy_arns = [get_arn_from_policy_name(p) for p in policies] 493 | 494 | result['Properties']['AssumeRolePolicyDocument']['Statement'][0]['Principal']['Service'] = trusted_entities 495 | result['Properties']['ManagedPolicyArns'] = policy_arns 496 | 497 | return result 498 | 499 | 500 | def combine_templates(t1, t2): # Combines t2 into t1, modifies t1. 501 | for i in t2.keys(): 502 | if i in t1: 503 | t1[i].update(json.loads(json.dumps(t2[i]))) # deep copy. 504 | else: 505 | t1[i] = json.loads(json.dumps(t2[i])) 506 | return t1 507 | 508 | 509 | def generate_cfn_template(): 510 | result = json.loads(json.dumps(CFN_TEMPLATE)) 511 | 512 | for lambda_function_name in lambda_functions.keys(): 513 | print('Generating CloudFormation template for Lambda function: ' + lambda_function_name) 514 | lambda_function_template = generate_lambda_function_cfn_template(lambda_function_name) 515 | combine_templates(result, lambda_function_template) 516 | 517 | for state_machine_name in state_machines.keys(): 518 | print('Generating CloudFormation template for state machine: ' + state_machine_name) 519 | state_machine_template = generate_state_machine_cfn_template(state_machine_name) 520 | combine_templates(result, state_machine_template) 521 | 522 | return result 523 | 524 | 525 | def get_cfn_stack_info(): 526 | cfn = boto3.client('cloudformation', region_name=AWS_DEFAULT_REGION) 527 | 528 | try: 529 | result = cfn.describe_stacks(StackName=CFN_STACK_NAME) 530 | return result['Stacks'][0] 531 | except ClientError: 532 | result = None 533 | 534 | return result 535 | 536 | 537 | def create_cfn_change_set(): 538 | stack_info = get_cfn_stack_info() 539 | print('Generating CloudFormation template.') 540 | template = json.dumps(generate_cfn_template(), sort_keys=True, indent=4) 541 | 542 | cfn = boto3.client('cloudformation', region_name=AWS_DEFAULT_REGION) 543 | args = { 544 | 'StackName': CFN_STACK_NAME, 545 | 'TemplateBody': template, 546 | 'Capabilities': ['CAPABILITY_NAMED_IAM'], 547 | 'ChangeSetName': CFN_STACK_CHANGE_SET_NAME, 548 | 'ChangeSetType': 'UPDATE' 549 | } 550 | if stack_info is None or stack_info['StackStatus'] == 'REVIEW_IN_PROGRESS': 551 | print('CloudFormation stack: ' + CFN_STACK_NAME + ' does not exist. Creating change set for a new stack.') 552 | args['ChangeSetType'] = 'CREATE' 553 | elif stack_info['StackStatus'] == 'ROLLBACK_COMPLETE': 554 | print('CloudFormation stack: ' + CFN_STACK_NAME + ' is in ROLLBACK_COMPLETE state.') 555 | print('Deleting stack...') 556 | cfn.delete_stack(StackName=CFN_STACK_NAME) 557 | while True: 558 | print('Waiting for stack delete to complete.') 559 | stack_info = get_cfn_stack_info() 560 | if stack_info is None or stack_info['StackStatus'] == 'DELETE_COMPLETE': 561 | break 562 | time.sleep(SLEEP_TIME) 563 | args['ChangeSetType'] = 'CREATE' 564 | else: 565 | print( 566 | 'CloudFormation stack: ' + CFN_STACK_NAME + ' exists already. Creating change set for updating the stack.' 567 | ) 568 | 569 | try: 570 | cfn.delete_change_set( 571 | ChangeSetName=CFN_STACK_CHANGE_SET_NAME, 572 | StackName=CFN_STACK_NAME 573 | ) 574 | while True: 575 | response = cfn.describe_change_set(ChangeSetName=CFN_STACK_CHANGE_SET_NAME, StackName=CFN_STACK_NAME) 576 | status = response['Status'] 577 | print('Status: ' + status) 578 | if status == 'FAILED': 579 | exit(1) 580 | elif status.endswith('COMPLETED'): 581 | break 582 | except ClientError: 583 | pass 584 | 585 | response = cfn.create_change_set(**args) 586 | change_set_id = response['Id'] 587 | 588 | print('Waiting for CloudFormation change set creation to complete...') 589 | while True: 590 | time.sleep(SLEEP_TIME) 591 | response = cfn.describe_change_set(ChangeSetName=change_set_id) 592 | status = response['Status'] 593 | if status == 'FAILED': 594 | reason = response['StatusReason'] 595 | if reason == 'No updates are to be performed.': 596 | print('No changes to the CloudFormation stack necessary.') 597 | return None 598 | else: 599 | print('Reason: ' + reason) 600 | exit(1) 601 | print('Status: ' + status) 602 | if status.endswith('COMPLETE'): 603 | return change_set_id 604 | 605 | 606 | def execute_cfn_change_set(change_set_id): 607 | cfn = boto3.client('cloudformation', region_name=AWS_DEFAULT_REGION) 608 | 609 | print('Executing CloudFormation change set...') 610 | cfn.execute_change_set(ChangeSetName=change_set_id) 611 | 612 | while True: 613 | response = get_cfn_stack_info() 614 | if response is None: 615 | status = 'UNKNOWN' 616 | else: 617 | status = response.get('StackStatus', 'UNKNOWN') 618 | 619 | print('Status: ' + status) 620 | if 'StatusReason' in response: 621 | print('Reason: ' + response['StatusReason']) 622 | if status.endswith('FAILED') or status == 'ROLLBACK_COMPLETE': 623 | exit(1) 624 | elif status == 'UNKNOWN': 625 | print('Stack info:\n' + json.dumps(response, sort_keys=True, indent=4, default=str)) 626 | elif status.endswith('COMPLETE'): 627 | return 628 | 629 | time.sleep(SLEEP_TIME) 630 | 631 | 632 | def update_cfn_stack(): 633 | change_set_id = create_cfn_change_set() 634 | if change_set_id is not None: 635 | execute_cfn_change_set(change_set_id) 636 | 637 | 638 | def delete_cfn_stack(): 639 | info = get_cfn_stack_info() 640 | 641 | if info is None: 642 | status = 'NOT_EXIST' 643 | else: 644 | status = info['StackStatus'] 645 | 646 | if status == 'DELETE_IN_PROGRESS': 647 | print('CloudFormation stack ' + CFN_STACK_NAME + ' is already being deleted.') 648 | return 649 | elif status == 'DELETE_FAILED': 650 | print( 651 | 'CloudFormation stack ' + CFN_STACK_NAME + ' deletion failed. Please investigate through the AWS console.' 652 | ) 653 | return 654 | elif status in ['NOT_EXIST', 'CREATE_FAILED', 'DELETE_COMPLETE']: 655 | print('CloudFormation stack ' + CFN_STACK_NAME + ' does not exist (status: ' + status + '). Nothing to delete.') 656 | return 657 | else: 658 | print('Warning: This will delete the ' + CFN_STACK_NAME + ' CloudFormation stack and all its resources.') 659 | response = raw_input('To continue, please type "delete": ') 660 | if response == 'delete': 661 | print('Deleting CloudFormation stack: ' + CFN_STACK_NAME) 662 | cfn = boto3.client('cloudformation', region_name=AWS_DEFAULT_REGION) 663 | cfn.delete_stack(StackName=CFN_STACK_NAME) 664 | else: 665 | print('Aborting...') 666 | return 667 | 668 | 669 | def get_state_machine_names(): 670 | stack_output_list = get_cfn_stack_info()['Outputs'] 671 | stack_output_dict = {} 672 | for output in stack_output_list: 673 | stack_output_dict[output['OutputKey']] = output['OutputValue'] 674 | 675 | result = [] 676 | for state_machine_name in state_machines.keys(): 677 | result.append(stack_output_dict[to_camel_case(state_machine_name) + 'Name']) 678 | 679 | return result 680 | 681 | 682 | def print_state_machine_names(): 683 | names = get_state_machine_names() 684 | if len(names) == 0: 685 | return 686 | elif len(names) == 1: 687 | print('State machine name: ' + names[0]) 688 | else: 689 | print('State machine names:') 690 | for name in get_state_machine_names(): 691 | print ' ' + name 692 | 693 | 694 | # Main 695 | 696 | @task(default=True) 697 | def deploy(): 698 | check_bucket(LAMBDA_FUNCTION_DEPLOYMENT_BUCKET) 699 | populate_lambda_functions_dict() 700 | populate_state_machines_dict() 701 | update_lambda_function_packages() 702 | update_cfn_stack() 703 | print_state_machine_names() 704 | 705 | 706 | @task() 707 | def delete(): 708 | delete_cfn_stack() 709 | -------------------------------------------------------------------------------- /fabfile_config_template.py: -------------------------------------------------------------------------------- 1 | # 2 | # Please customize the constants below and save as 'fabfile_config.py'. 3 | # 4 | USER_EMAIL = 'your_email@domain.com' # This is used to generate a unique world-wide Amazon S3 bucket name. 5 | AWS_DEFAULT_PROFILE = 'default' # May be omitted, in which case the environment variable of same name is used. 6 | AWS_DEFAULT_REGION = 'us-east-1' # May be omitted, in which case the environment variable of same name is used. 7 | -------------------------------------------------------------------------------- /lambda_functions/combine_dicts.py: -------------------------------------------------------------------------------- 1 | # Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | # Licensed under the Apache License, Version 2.0 (the "License"). 3 | # You may not use this file except in compliance with the License. 4 | # A copy of the License is located at 5 | # 6 | # http://aws.amazon.com/apache2.0/ 7 | # 8 | # or in the "license" file accompanying this file. 9 | # This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | # See the License for the specific language governing permissions and limitations under the License. 11 | 12 | # 13 | # YAML front matter with parameters for deployment as a Lambda function. 14 | # 15 | # --- 16 | # Description: "Combine a list of dicts into a single output dict. Useful for AWS Step Functions parallel tasks." 17 | # MemorySize: 128 18 | # Timeout: 10 19 | # Policies: 20 | # --- 21 | # 22 | # Input event: A list of dicts. 23 | # Output: A single dict with all attributes of the input dicts merged in. 24 | # 25 | 26 | 27 | def handler(event, _): 28 | assert(isinstance(event, list)) 29 | 30 | result = {} 31 | for i in event: 32 | result.update(i) 33 | 34 | return result 35 | -------------------------------------------------------------------------------- /lambda_functions/copy_keys.py: -------------------------------------------------------------------------------- 1 | # Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | # Licensed under the Apache License, Version 2.0 (the "License"). 3 | # You may not use this file except in compliance with the License. 4 | # A copy of the License is located at 5 | # 6 | # http://aws.amazon.com/apache2.0/ 7 | # 8 | # or in the "license" file accompanying this file. 9 | # This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | # See the License for the specific language governing permissions and limitations under the License. 11 | 12 | # 13 | # YAML front matter with parameters for deployment as a Lambda function. 14 | # 15 | # --- 16 | # Description: "Copy the given keys from source to destination. Omit already existing keys." 17 | # MemorySize: 128 18 | # Timeout: 300 19 | # Policies: 20 | # - AmazonS3FullAccess 21 | # --- 22 | # 23 | # Input event: A dict like: 24 | # { 25 | # 'source': 'source-bucket', 26 | # 'sourceRegion': 'eu-west-1', 27 | # 'destination': 'destination-bucket', 28 | # 'destinationRegion': 'eu-west-1', 29 | # 'keys': [ ... ] 30 | # } 31 | # 32 | 33 | # Imports 34 | 35 | import logging 36 | import boto3 37 | from threading import Thread 38 | from botocore.exceptions import ClientError 39 | from Queue import Queue, Empty 40 | import json 41 | 42 | 43 | # Constants 44 | 45 | DEBUG = False 46 | THREAD_PARALLELISM = 10 # Empirical value for now. Should find good way to measure/auto-scale this. 47 | METADATA_KEYS = [ 48 | 'CacheControl', 49 | 'ContentDisposition', 50 | 'ContentEncoding', 51 | 'ContentLanguage', 52 | 'ContentType', 53 | 'Expires', 54 | 'Metadata' 55 | ] 56 | 57 | 58 | # Globals 59 | 60 | logger = logging.getLogger() 61 | if DEBUG: 62 | logger.setLevel(logging.DEBUG) 63 | else: 64 | logger.setLevel(logging.INFO) 65 | 66 | 67 | # Utility functions 68 | 69 | def collect_metadata(response): 70 | metadata = {} 71 | for key in METADATA_KEYS: 72 | if key in response: 73 | metadata[key] = response[key] 74 | metadata_json = json.dumps(metadata, sort_keys=True, default=str) 75 | return metadata_json 76 | 77 | 78 | # Classes 79 | 80 | class KeySynchronizer(Thread): 81 | def __init__(self, job_queue=None, source=None, destination=None, region=None): 82 | super(KeySynchronizer, self).__init__() 83 | self.job_queue = job_queue 84 | self.source = source 85 | self.destination = destination 86 | self.s3 = boto3.client('s3', region_name=region) 87 | 88 | def copy_redirect(self, key, target): 89 | logger.info( 90 | 'Copying redirect: ' + key + ' from bucket: ' + self.source + 91 | ' to destination bucket: ' + self.destination 92 | ) 93 | self.s3.put_object( 94 | Bucket=self.destination, 95 | Key=key, 96 | WebsiteRedirectLocation=target 97 | ) 98 | 99 | def copy_object(self, key): 100 | logger.info( 101 | 'Copying key: ' + key + ' from bucket: ' + self.source + 102 | ' to destination bucket: ' + self.destination 103 | ) 104 | self.s3.copy_object( 105 | CopySource={ 106 | 'Bucket': self.source, 107 | 'Key': key 108 | }, 109 | Bucket=self.destination, 110 | Key=key, 111 | MetadataDirective='COPY', 112 | TaggingDirective='COPY' 113 | ) 114 | 115 | def run(self): 116 | while not self.job_queue.empty(): 117 | try: 118 | key = self.job_queue.get(True, 1) 119 | except Empty: 120 | return 121 | 122 | source_response = self.s3.head_object(Bucket=self.source, Key=key) 123 | try: 124 | destination_response = self.s3.head_object(Bucket=self.destination, Key=key) 125 | except ClientError as e: 126 | if int(e.response['Error']['Code']) == 404: # 404 = we need to copy this. 127 | if 'WebsiteRedirectLocation' in source_response: 128 | self.copy_redirect(key, source_response['WebsiteRedirectLocation']) 129 | else: 130 | self.copy_object(key) 131 | continue 132 | else: # All other return codes are unexpected. 133 | raise e 134 | 135 | if 'WebsiteRedirectLocation' in source_response: 136 | if ( 137 | source_response['WebsiteRedirectLocation'] != 138 | destination_response.get('WebsiteRedirectLocation', None) 139 | ): 140 | self.copy_redirect(key, source_response['WebsiteRedirectLocation']) 141 | continue 142 | 143 | source_etag = source_response.get('ETag', None) 144 | destination_etag = destination_response.get('ETag', None) 145 | if source_etag != destination_etag: 146 | self.copy_object(key) 147 | continue 148 | 149 | source_metadata = collect_metadata(source_response) 150 | destination_metadata = collect_metadata(destination_response) 151 | if source_metadata == destination_metadata: 152 | logger.info( 153 | 'Key: ' + key + ' from bucket: ' + self.source + 154 | ' is already current in destination bucket: ' + self.destination 155 | ) 156 | continue 157 | else: 158 | self.copy_object(key) 159 | 160 | 161 | # Functions 162 | 163 | def sync_keys(source=None, destination=None, region=None, keys=None): 164 | job_queue = Queue() 165 | worker_threads = [] 166 | 167 | for i in range(THREAD_PARALLELISM): 168 | worker_threads.append(KeySynchronizer( 169 | job_queue=job_queue, 170 | source=source, 171 | destination=destination, 172 | region=region, 173 | )) 174 | 175 | for key in keys: 176 | logger.info('Queuing: ' + key + ' for synchronization.') 177 | job_queue.put(key) 178 | 179 | logger.info( 180 | 'Starting ' + str(THREAD_PARALLELISM) + ' key synchronization processes for buckets: ' + source + 181 | ' and ' + destination + '.' 182 | ) 183 | for t in worker_threads: 184 | t.start() 185 | 186 | for t in worker_threads: 187 | t.join() 188 | 189 | 190 | def handler(event, context): 191 | assert(isinstance(event, dict)) 192 | 193 | source = event['source'] 194 | destination = event['destination'] 195 | keys = event['listResult']['keys'] 196 | 197 | function_region = context.invoked_function_arn.split(':')[3] 198 | region = event.get('sourceRegion', function_region) 199 | 200 | logger.info('Copying ' + str(len(keys)) + ' keys from bucket: ' + source + ' to bucket: ' + destination) 201 | 202 | sync_keys(source=source, destination=destination, keys=keys, region=region) 203 | 204 | return 205 | -------------------------------------------------------------------------------- /lambda_functions/delete_orphaned_keys.py: -------------------------------------------------------------------------------- 1 | # Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | # Licensed under the Apache License, Version 2.0 (the "License"). 3 | # You may not use this file except in compliance with the License. 4 | # A copy of the License is located at 5 | # 6 | # http://aws.amazon.com/apache2.0/ 7 | # 8 | # or in the "license" file accompanying this file. 9 | # This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | # See the License for the specific language governing permissions and limitations under the License. 11 | 12 | # 13 | # YAML front matter with parameters for deployment as a Lambda function. 14 | # 15 | # --- 16 | # Description: "Delete keys present in the given destination bucket that are not present in the source bucket." 17 | # MemorySize: 128 18 | # Timeout: 300 19 | # Policies: 20 | # - AmazonS3FullAccess 21 | # --- 22 | # 23 | # Input event: A dict like: 24 | # { 25 | # 'source': 'source-bucket', 26 | # 'sourceRegion': 'eu-west-1', 27 | # 'destination': 'destination-bucket', 28 | # 'destinationRegion': 'eu-west-1', 29 | # 'keys': [ ... ] 30 | # } 31 | # 32 | 33 | # Imports 34 | 35 | import logging 36 | import boto3 37 | from threading import Thread 38 | from botocore.exceptions import ClientError 39 | from Queue import Queue, Empty 40 | 41 | 42 | # Constants 43 | 44 | DEBUG = False 45 | THREAD_PARALLELISM = 10 46 | 47 | 48 | # Globals 49 | 50 | logger = logging.getLogger() 51 | if DEBUG: 52 | logger.setLevel(logging.DEBUG) 53 | else: 54 | logger.setLevel(logging.INFO) 55 | 56 | 57 | # Classes 58 | 59 | class ObsoleteKeyDeleter(Thread): 60 | def __init__(self, job_queue=None, source=None, destination=None, region=None): 61 | super(ObsoleteKeyDeleter, self).__init__() 62 | self.job_queue = job_queue 63 | self.source = source 64 | self.destination = destination 65 | self.s3 = boto3.client('s3', region_name=region) 66 | 67 | def run(self): 68 | while not self.job_queue.empty(): 69 | try: 70 | key = self.job_queue.get(True, 1) 71 | except Empty: 72 | return 73 | 74 | try: 75 | self.s3.head_object(Bucket=self.source, Key=key) 76 | logger.info('Key: ' + key + ' is present in source bucket, nothing to do.') 77 | except ClientError as e: 78 | if int(e.response['Error']['Code']) == 404: # The key was not found. 79 | logger.info('Key: ' + key + ' is not present in source bucket. Deleting orphaned key.') 80 | self.s3.delete_object(Bucket=self.destination, Key=key) 81 | else: 82 | raise e 83 | 84 | 85 | # Functions 86 | 87 | def delete_obsolete_keys(source=None, destination=None, region=None, keys=None): 88 | job_queue = Queue() 89 | worker_threads = [] 90 | 91 | for i in range(THREAD_PARALLELISM): 92 | worker_threads.append(ObsoleteKeyDeleter( 93 | job_queue=job_queue, 94 | source=source, 95 | destination=destination, 96 | region=region, 97 | )) 98 | 99 | for key in keys: 100 | logger.info('Queuing: ' + key + ' for orphan detection.') 101 | job_queue.put(key) 102 | 103 | logger.info('Starting orphan detection for buckets: ' + source + ' and ' + destination + '.') 104 | for t in worker_threads: 105 | t.start() 106 | 107 | for t in worker_threads: 108 | t.join() 109 | 110 | 111 | def handler(event, context): 112 | assert(isinstance(event, dict)) 113 | 114 | source = event['source'] 115 | destination = event['destination'] 116 | keys = event['listResult']['keys'] 117 | 118 | function_region = context.invoked_function_arn.split(':')[3] 119 | region = event.get('sourceRegion', function_region) 120 | 121 | logger.info('Synchronizing ' + str(len(keys)) + ' between bucket: ' + source + ' and: ' + destination) 122 | 123 | delete_obsolete_keys(source=source, destination=destination, keys=keys, region=region) 124 | 125 | return 126 | -------------------------------------------------------------------------------- /lambda_functions/get_bucket_location.py: -------------------------------------------------------------------------------- 1 | # Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | # Licensed under the Apache License, Version 2.0 (the "License"). 3 | # You may not use this file except in compliance with the License. 4 | # A copy of the License is located at 5 | # 6 | # http://aws.amazon.com/apache2.0/ 7 | # 8 | # or in the "license" file accompanying this file. 9 | # This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | # See the License for the specific language governing permissions and limitations under the License. 11 | 12 | # 13 | # YAML front matter with parameters for deployment as a Lambda function. 14 | # 15 | # --- 16 | # Description: "Get the location of the given AWS bucket, return its region name." 17 | # MemorySize: 128 18 | # Timeout: 10 19 | # Policies: 20 | # - AmazonS3ReadOnlyAccess 21 | # --- 22 | # 23 | # Input event: A string with the bucket name to query the region name for. 24 | # 25 | 26 | # Imports 27 | 28 | import logging 29 | import boto3 30 | 31 | 32 | # Constants 33 | 34 | DEBUG = False 35 | 36 | 37 | # Globals 38 | 39 | logger = logging.getLogger() 40 | if DEBUG: 41 | logger.setLevel(logging.DEBUG) 42 | else: 43 | logger.setLevel(logging.INFO) 44 | 45 | 46 | # Functions 47 | 48 | def handler(event, context): 49 | if isinstance(event, (str, unicode)): 50 | bucket = event 51 | else: # Find the first attribute in the dict that contains somehow the string 'bucket'. 52 | assert(isinstance(event, dict)) 53 | 54 | bucket_keys = [i for i in event.keys() if 'bucket' in i.lower()] 55 | if len(bucket_keys) > 0: 56 | bucket = event[bucket_keys[0]] 57 | else: 58 | bucket = event[event.keys()[0]] # Give up and just go for the first key. 59 | 60 | assert(bucket is not None and isinstance(bucket, (str, unicode)) and bucket != '') 61 | function_region = context.invoked_function_arn.split(':')[3] 62 | 63 | logger.info('Looking up bucket location for bucket: ' + bucket) 64 | 65 | s3 = boto3.client('s3', region_name=function_region) 66 | response = s3.get_bucket_location(Bucket=bucket) 67 | location_constraint = response.get('LocationConstraint', None) 68 | if location_constraint is None: 69 | return 'us-east-1' 70 | elif location_constraint == 'EU': 71 | return 'eu-west-1' 72 | else: 73 | return location_constraint 74 | -------------------------------------------------------------------------------- /lambda_functions/list_bucket.py: -------------------------------------------------------------------------------- 1 | # Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | # Licensed under the Apache License, Version 2.0 (the "License"). 3 | # You may not use this file except in compliance with the License. 4 | # A copy of the License is located at 5 | # 6 | # http://aws.amazon.com/apache2.0/ 7 | # 8 | # or in the "license" file accompanying this file. 9 | # This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | # See the License for the specific language governing permissions and limitations under the License. 11 | 12 | # 13 | # YAML front matter with parameters for deployment as a Lambda function. 14 | # 15 | # --- 16 | # Description: "Prepare for next copy cycle: List source bucket contents, updates token for longer lists if necessary." 17 | # MemorySize: 128 18 | # Timeout: 60 19 | # Policies: 20 | # - AmazonS3ReadOnlyAccess 21 | # --- 22 | # 23 | # Input event: A string with the source bucket name and optional region and token (for s3.list_objects_v2()). 24 | # 25 | 26 | # Imports 27 | 28 | import logging 29 | import boto3 30 | import json 31 | 32 | 33 | # Constants 34 | 35 | DEBUG = False 36 | MAX_KEYS = 1024 # Should be a power of two since it may get divided by two a couple of times. 37 | MAX_DATA_SIZE = 32000 # Max. result size: https://docs.aws.amazon.com/step-functions/latest/dg/service-limits.html 38 | SAFETY_MARGIN = 10.0 # Percent 39 | MAX_RESULT_LENGTH = int(MAX_DATA_SIZE * (1.0 - (SAFETY_MARGIN / 100.0))) 40 | PREFIX = '' # Copy objects based on a provided prefix e.g. '/images/' 41 | START_AFTER = '' # List objects after a specific key e.g. '/images/1000' 42 | 43 | 44 | # Globals 45 | 46 | logger = logging.getLogger() 47 | if DEBUG: 48 | logger.setLevel(logging.DEBUG) 49 | else: 50 | logger.setLevel(logging.INFO) 51 | 52 | 53 | # Functions 54 | 55 | def handler(event, context): 56 | assert(isinstance(event, dict)) 57 | 58 | bucket_to_list = event['listBucket'] 59 | bucket = event[bucket_to_list] 60 | 61 | function_region = context.invoked_function_arn.split(':')[3] 62 | region = event.get('sourceRegion', function_region) 63 | 64 | token = event.get('listResult', {}).get('token', '') 65 | max_keys = event.get('maxKeys', MAX_KEYS) 66 | prefix = event.get('prefix', PREFIX) 67 | start_after = event.get('startAfter', START_AFTER) 68 | 69 | args = { 70 | 'Bucket': bucket, 71 | 'MaxKeys': max_keys, 72 | 'Prefix': prefix, 73 | 'StartAfter': start_after 74 | } 75 | 76 | result = {} 77 | s3 = boto3.client('s3', region_name=region) 78 | 79 | while True: 80 | logger_string = 'Listing contents of bucket: ' + bucket + ' in: ' + region + ' (' 81 | if token is not None and token != '': 82 | logger_string += 'continuation token: ' + token + ', ' 83 | args['ContinuationToken'] = token 84 | logger_string += 'may_keys: ' + str(max_keys) + ')' 85 | 86 | response = s3.list_objects_v2(**args) 87 | 88 | keys = [k['Key'] for k in response.get('Contents', [])] 89 | logger.info('Got ' + str(len(keys)) + ' result keys.') 90 | 91 | result['keys'] = keys 92 | result['token'] = response.get('NextContinuationToken', '') 93 | result_length = len(json.dumps(result)) 94 | if result_length <= MAX_RESULT_LENGTH: 95 | return result 96 | else: 97 | # Try again with a smaller may_keys size. 98 | logger.warning( 99 | 'Result size: ' + str(result_length) + ' is larger than maximum of: ' + str(MAX_RESULT_LENGTH) + '. ' 100 | ) 101 | 102 | max_keys = int(len(keys) / 2) # ask for half the number of keys we got. 103 | if max_keys == 0: 104 | raise Exception('Something is wrong: Downsized max_keys all the way to 0 ...') 105 | args['MaxKeys'] = max_keys 106 | logger.info('Trying again with max_keys value: ' + str(max_keys)) 107 | -------------------------------------------------------------------------------- /lambda_functions/validate_input.py: -------------------------------------------------------------------------------- 1 | # Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | # Licensed under the Apache License, Version 2.0 (the "License"). 3 | # You may not use this file except in compliance with the License. 4 | # A copy of the License is located at 5 | # 6 | # http://aws.amazon.com/apache2.0/ 7 | # 8 | # or in the "license" file accompanying this file. 9 | # This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | # See the License for the specific language governing permissions and limitations under the License. 11 | 12 | # 13 | # YAML front matter with parameters for deployment as a Lambda function. 14 | # 15 | # --- 16 | # Description: "Check if the sourceRegion and destinationRegion attributes of the event dict are equal." 17 | # MemorySize: 128 18 | # Timeout: 10 19 | # Policies: 20 | # --- 21 | # 22 | # Input event: A dict with the sourceRegion and destinationRegion attributes. 23 | # 24 | 25 | 26 | def handler(event, _): 27 | return event['sourceRegion'] == event['destinationRegion'] 28 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | Fabric>=1.12.0 2 | boto3>=1.4.3 3 | PyYAML>=3.12 4 | -------------------------------------------------------------------------------- /state_machines/sync_buckets_state_machine.yaml: -------------------------------------------------------------------------------- 1 | # Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | # Licensed under the Apache License, Version 2.0 (the "License"). 3 | # You may not use this file except in compliance with the License. 4 | # A copy of the License is located at 5 | # 6 | # http://aws.amazon.com/apache2.0/ 7 | # 8 | # or in the "license" file accompanying this file. 9 | # This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | # See the License for the specific language governing permissions and limitations under the License. 11 | 12 | # 13 | # Note: This Amazon Step Functions state machine definition file is not complete yet: All resources are Lambda function 14 | # names that need to be resolved into their ARNs. This is done by the fabfile.py Python script in the main directory. 15 | # 16 | 17 | Comment: Synchronize two Amazon S3 buckets. 18 | StartAt: FindBucketRegions 19 | TimeoutSeconds: 1800 # 30 minutes, in seconds. 20 | States: 21 | FindBucketRegions: 22 | Type: Parallel 23 | Branches: 24 | - 25 | StartAt: FindRegionForSourceBucket 26 | States: 27 | FindRegionForSourceBucket: 28 | Type: Task 29 | Resource: get_bucket_location 30 | InputPath: '$.source' 31 | ResultPath: '$.sourceRegion' 32 | OutputPath: '$' 33 | TimeoutSeconds: 15 # Lambda function has 10 seconds, add 5 to be sure. 34 | End: true 35 | - 36 | StartAt: FindRegionForDestinationBucket 37 | States: 38 | FindRegionForDestinationBucket: 39 | Type: Task 40 | Resource: get_bucket_location 41 | InputPath: '$.destination' 42 | ResultPath: '$.destinationRegion' 43 | OutputPath: '$' 44 | TimeoutSeconds: 15 # Lambda function has 10 seconds, add 5 to be sure. 45 | End: true 46 | InputPath: '$' 47 | ResultPath: '$' 48 | OutputPath: '$' 49 | Next: CombineRegionOutputs 50 | CombineRegionOutputs: 51 | Type: Task 52 | Resource: combine_dicts 53 | InputPath: '$' 54 | ResultPath: '$' 55 | OutputPath: '$' 56 | TimeoutSeconds: 15 57 | Next: ValidateInput 58 | ValidateInput: 59 | Type: Task 60 | Resource: validate_input 61 | InputPath: '$' 62 | ResultPath: '$.regionsAreSame' 63 | OutputPath: '$' 64 | TimeoutSeconds: 15 65 | Next: ConfirmInputValid 66 | ConfirmInputValid: 67 | Type: Choice 68 | Choices: 69 | - 70 | Variable: "$.regionsAreSame" 71 | BooleanEquals: true 72 | Next: ProcessBuckets 73 | Default: BucketRegionsNotEqualFailure 74 | BucketRegionsNotEqualFailure: 75 | Type: Fail 76 | Error: BucketRegionsNotEqualError 77 | Cause: "The source and destination buckets have different regions. This is currently not supported." 78 | ProcessBuckets: 79 | Type: Parallel 80 | Branches: 81 | - 82 | StartAt: InjectSourceBucket 83 | States: 84 | InjectSourceBucket: 85 | Type: Pass 86 | Result: 'source' 87 | ResultPath: '$.listBucket' 88 | OutputPath: '$' 89 | Next: UpdateSourceKeyList 90 | UpdateSourceKeyList: 91 | Type: Task 92 | Resource: list_bucket 93 | InputPath: '$' 94 | ResultPath: '$.listResult' 95 | OutputPath: '$' 96 | TimeoutSeconds: 65 97 | Next: CopySourceKeys 98 | CopySourceKeys: 99 | Type: Task 100 | Resource: copy_keys 101 | InputPath: '$' 102 | ResultPath: null 103 | OutputPath: '$' 104 | TimeoutSeconds: 305 105 | Retry: 106 | - 107 | ErrorEquals: ["Lambda.Unknown", "States.Timeout"] 108 | IntervalSeconds: 0 109 | MaxAttempts: 3 110 | 111 | Next: EvaluateCopyListToken 112 | EvaluateCopyListToken: 113 | Type: Choice 114 | Choices: 115 | - 116 | Not: 117 | Variable: '$.listResult.token' 118 | StringEquals: '' 119 | Next: UpdateSourceKeyList 120 | Default: FinishCopyBranch 121 | FinishCopyBranch: 122 | InputPath: null 123 | Type: Pass 124 | End: true 125 | - 126 | StartAt: InjectDestinationBucket 127 | States: 128 | InjectDestinationBucket: 129 | Type: Pass 130 | Result: 'destination' 131 | ResultPath: '$.listBucket' 132 | OutputPath: '$' 133 | Next: UpdateDestinationKeyList 134 | UpdateDestinationKeyList: 135 | Type: Task 136 | Resource: list_bucket 137 | InputPath: '$' 138 | ResultPath: '$.listResult' 139 | OutputPath: '$' 140 | TimeoutSeconds: 65 141 | Next: DeleteOrphanedKeys 142 | DeleteOrphanedKeys: 143 | Type: Task 144 | Resource: delete_orphaned_keys 145 | InputPath: '$' 146 | ResultPath: null 147 | OutputPath: '$' 148 | TimeoutSeconds: 305 149 | Next: EvaluateDestinationListToken 150 | EvaluateDestinationListToken: 151 | Type: Choice 152 | Choices: 153 | - 154 | Not: 155 | Variable: '$.listResult.token' 156 | StringEquals: '' 157 | Next: UpdateDestinationKeyList 158 | Default: FinishDeleteBranch 159 | FinishDeleteBranch: 160 | InputPath: null 161 | Type: Pass 162 | End: true 163 | Next: Success 164 | Success: 165 | Type: Succeed 166 | --------------------------------------------------------------------------------