Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,10 @@ Q2: How to force recreate deployment package?

A2: Delete an existing zip-archive from `builds` directory, or make a change in your source code. If there is no zip-archive for the current content hash, it will be recreated during `terraform apply`.

Q3: `null_resource.archive[0] must be replaced`

A3: This probably mean that zip-archive has been deployed, but is currently absent locally, and it has to be recreated locally. When you run into this issue during CI/CD process (where workspace is clean), you can set environment variable `TF_RECREATE_MISSING_LAMBDA_PACKAGE=false` and run `terraform apply`.


## Notes

Expand Down
44 changes: 38 additions & 6 deletions package.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,26 @@ def source_code_hash(bytes):
return b64encode(hashlib.sha256(bytes).digest()).decode()


def yesno_bool(val):
if val is None:
return
if isinstance(val, bool):
return val
if isinstance(val, int):
return bool(val)
if isinstance(val, str):
if val.isnumeric():
return bool(int(val))
val = val.lower()
if val in ('true', 'yes', 'y'):
return True
elif val in ('false', 'no', 'n'):
return False
else:
raise ValueError("Unsupported value: %s" % val)
return False


################################################################################
# Packaging functions

Expand Down Expand Up @@ -412,6 +432,7 @@ def update_hash(hash_obj, file_root, file_path):
hash_extra_paths = query.hash_extra_paths
source_path = query.source_path
hash_extra = query.hash_extra
recreate_missing_package = yesno_bool(args.recreate_missing_package)

# Compacting docker vars
docker = query.docker
Expand Down Expand Up @@ -440,12 +461,16 @@ def update_hash(hash_obj, file_root, file_path):
# Compute timestamp trigger
was_missing = False
filename_path = os.path.join(os.getcwd(), filename)
if os.path.exists(filename_path):
st = os.stat(filename_path)
timestamp = st.st_mtime_ns
if recreate_missing_package:
if os.path.exists(filename_path):
st = os.stat(filename_path)
timestamp = st.st_mtime_ns
else:
timestamp = timestamp_now_ns()
was_missing = True
else:
timestamp = timestamp_now_ns()
was_missing = True
timestamp = "<WARNING: Missing lambda zip artifacts " \
"wouldn't be restored>"

# Replace variables in the build command with calculated values.
build_data = {
Expand Down Expand Up @@ -533,6 +558,11 @@ def create_zip_file(source_dir, target_file, timestamp):
artifacts_dir = query.artifacts_dir
docker = query.docker

if timestamp.isnumeric():
timestamp = int(timestamp)
else:
timestamp = 0

if os.path.exists(filename):
logger.info('Reused: %s', shlex.quote(filename))
return
Expand Down Expand Up @@ -674,7 +704,7 @@ def args_parser():
help='build and pack to a zip archive')
p.set_defaults(command=build_command)
p.add_argument('-t', '--timestamp',
dest='zip_file_timestamp', type=int, required=True,
dest='zip_file_timestamp', required=True,
help='A zip file timestamp generated by the prepare command')
p.add_argument('build_plan_file', metavar='PLAN_FILE',
help='A build plan file provided by the prepare command')
Expand All @@ -684,6 +714,8 @@ def args_parser():

def main():
ns = argparse.Namespace(
recreate_missing_package=os.environ.get(
'TF_RECREATE_MISSING_LAMBDA_PACKAGE'),
log_level=os.environ.get('TF_PACKAGE_LOG_LEVEL', 'INFO'),
dump_input=bool(os.environ.get('TF_DUMP_INPUT')),
dump_env=bool(os.environ.get('TF_DUMP_ENV')),
Expand Down