forked from claranet/terraform-aws-lambda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
built.py
executable file
·37 lines (32 loc) · 1.33 KB
/
built.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
#!/usr/bin/env python3
import json
import os
import subprocess
import sys
# Parse the query.
query = json.load(sys.stdin)
build_command = query['build_command']
filename_old = query['filename_old']
filename_new = query['filename_new']
# If the old filename (from the Terraform state) matches the new filename
# (from hash.py) then the source code has not changed and thus the zip file
# should not have changed.
if filename_old == filename_new:
if os.path.exists(filename_new):
# Update the file time so it doesn't get cleaned up,
# which would result in an unnecessary rebuild.
os.utime(filename_new, None)
else:
# If the file is missing, then it was probably generated on another
# machine, or it was created a long time ago and cleaned up. This is
# expected behaviour. However if Terraform needs to upload the file
# (e.g. someone manually deleted the Lambda function via the AWS
# console) then it is possible that Terraform will try to upload
# the missing file. I don't know how to tell if Terraform is going
# to try to upload the file or not, so always ensure the file exists.
subprocess.check_output(build_command, shell=True)
# Output the filename to Terraform.
json.dump({
'filename': filename_new,
}, sys.stdout, indent=2)
sys.stdout.write('\n')