|
14 | 14 | from pathlib import ( |
15 | 15 | Path, |
16 | 16 | ) |
| 17 | +import re |
17 | 18 | import subprocess |
18 | 19 | from typing import ( |
19 | 20 | Mapping, |
@@ -759,6 +760,10 @@ def tf_config(self, app_name): |
759 | 760 | for _, resource in functions: |
760 | 761 | assert 'layers' not in resource |
761 | 762 | resource['layers'] = ['${aws_lambda_layer_version.dependencies.arn}'] |
| 763 | + # Publishing a new Lambda function version each time lets us update |
| 764 | + # the Lambda functions atomically, avoiding a race condition between |
| 765 | + # the update of the function's configuration and its code. |
| 766 | + resource['publish'] = True |
762 | 767 | env = config.es_endpoint_env( |
763 | 768 | es_endpoint=( |
764 | 769 | aws.es_endpoint |
@@ -921,6 +926,29 @@ def tf_config(self, app_name): |
921 | 926 | sqs_name, _ = config.unqualified_resource_name(resource_name, suffix) |
922 | 927 | resource['event_source_arn'] = f'${{aws_sqs_queue.{sqs_name}.arn}}' |
923 | 928 |
|
| 929 | + # Replace Lambda function ARNs with qualified ARNs for an atomic update. |
| 930 | + # |
| 931 | + if app_name == 'indexer': |
| 932 | + resource_arguments = [ |
| 933 | + ('aws_cloudwatch_event_target', 'arn'), |
| 934 | + ('aws_lambda_permission', 'function_name'), |
| 935 | + ('aws_lambda_event_source_mapping', 'function_name'), |
| 936 | + ] |
| 937 | + elif app_name == 'service': |
| 938 | + resource_arguments = [ |
| 939 | + ('aws_cloudwatch_event_target', 'arn'), |
| 940 | + ('aws_lambda_permission', 'function_name'), |
| 941 | + ] |
| 942 | + else: |
| 943 | + assert False, app_name |
| 944 | + for resource_type, argument in resource_arguments: |
| 945 | + for _, resource in json_item_dicts(resources[resource_type]): |
| 946 | + value = resource[argument] |
| 947 | + assert value.endswith('.arn}') |
| 948 | + resource[argument] = value.replace('.arn', '.qualified_arn') |
| 949 | + locals[app_name] = re.sub(r'(aws_lambda_function\.[^\.\s]+)\.invoke_arn', |
| 950 | + r'\1.qualified_invoke_arn', |
| 951 | + locals[app_name]) |
924 | 952 | return { |
925 | 953 | 'resource': resources, |
926 | 954 | 'data': data, |
|
0 commit comments