Created
July 12, 2020 14:17
-
-
Save fionn/ba6f61cf02e036c1ef20c64ad884866e to your computer and use it in GitHub Desktop.
Package Lambda
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data "external" "packaged_lambda" { | |
program = concat(["${path.module}/files/package_lambda.sh", var.source_folder], var.exclude_files) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
output "filename" { | |
value = data.external.packaged_lambda.result.filename | |
} | |
output "checksum" { | |
value = data.external.packaged_lambda.result.checksum | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -eo pipefail | |
source_folder=$1 | |
exclude_files=${@:2} | |
files=$(find $source_folder -type f) | |
declare -a included_files | |
while read -r line; do | |
relative_path=$(realpath --relative-to="${source_folder}" "$line") | |
include=1 | |
for exclude_path in ${exclude_files[@]}; do | |
if [[ $relative_path == $exclude_path* ]]; then | |
include=0 | |
fi | |
done | |
if [[ include -eq 1 ]]; then | |
included_files+=($relative_path) | |
fi | |
done <<< "$files" | |
pushd $PWD > /dev/null | |
cd ${source_folder} | |
checksums="" | |
for file in ${included_files[@]}; do | |
checksums+=$(sha256sum ${file} | cut -d ' ' -f1) | |
done | |
final_checksum=$(echo $checksums | sha256sum | cut -d ' ' -f1) | |
lambda_name=$(basename $source_folder) | |
package_name="/tmp/${lambda_name}-${final_checksum}.zip" | |
rm -f $package_name | |
file_string=$(IFS=' '; echo "${included_files[@]}") | |
zip -r -q $package_name $file_string | |
popd > /dev/null | |
echo "{ \"filename\": \"${package_name}\", \"checksum\": \"${final_checksum}\" }" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module "packaged_lambda" { | |
source = "../packaged_lambda" | |
source_folder = var.source_folder | |
exclude_files = var.exclude_files | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
variable "source_folder" { | |
type = string | |
} | |
variable "exclude_files" { | |
type = list(string) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment