-
-
Notifications
You must be signed in to change notification settings - Fork 699
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add control to use timestamp to trigger the package creation or…
… not (useful for CI/CD) (#521) Co-authored-by: Samuel Phan <samuel.phan@yahooinc.com> Co-authored-by: Anton Babenko <anton@antonbabenko.com>
- Loading branch information
1 parent
fd5a4c8
commit 57dbfc1
Showing
12 changed files
with
235 additions
and
2 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
def lambda_handler(event, context): | ||
print("Hello from app1!") | ||
|
||
return event |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/src |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Simple CI/CD example | ||
|
||
Configuration in this directory creates AWS Lambda Function as it would run in a context of CICD executions, where the Terraform working directory is empty and there is no `builds` directory, that: | ||
|
||
- `terraform plan` doesn't trigger a diff if the source code of the lambda function didn't change. | ||
- `terraform plan` does trigger a diff if the source code of the lambda function has changed. | ||
- `terraform apply` works if the code has changed. | ||
|
||
## Usage | ||
|
||
To run this example you need to execute: | ||
|
||
```bash | ||
./test.sh | ||
``` | ||
|
||
Note that this example may create resources which cost money. Run `terraform destroy` when you don't need these resources. | ||
|
||
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK --> | ||
## Requirements | ||
|
||
| Name | Version | | ||
|------|---------| | ||
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 | | ||
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 4.63 | | ||
| <a name="requirement_random"></a> [random](#requirement\_random) | >= 2.0 | | ||
|
||
## Providers | ||
|
||
| Name | Version | | ||
|------|---------| | ||
| <a name="provider_random"></a> [random](#provider\_random) | >= 2.0 | | ||
|
||
## Modules | ||
|
||
| Name | Source | Version | | ||
|------|--------|---------| | ||
| <a name="module_lambda_function"></a> [lambda\_function](#module\_lambda\_function) | ../../ | n/a | | ||
|
||
## Resources | ||
|
||
| Name | Type | | ||
|------|------| | ||
| [random_pet.this](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet) | resource | | ||
|
||
## Inputs | ||
|
||
No inputs. | ||
|
||
## Outputs | ||
|
||
No outputs. | ||
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK --> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
provider "aws" { | ||
region = "eu-west-1" | ||
# region = "us-east-1" | ||
|
||
# Make it faster by skipping something | ||
skip_metadata_api_check = true | ||
skip_region_validation = true | ||
skip_credentials_validation = true | ||
} | ||
|
||
resource "random_pet" "this" { | ||
length = 2 | ||
} | ||
|
||
module "lambda_function" { | ||
source = "../../" | ||
|
||
function_name = "${random_pet.this.id}-lambda-simple" | ||
handler = "index.lambda_handler" | ||
runtime = "python3.10" | ||
|
||
source_path = [ | ||
"${path.module}/src/python3.10-app1", | ||
] | ||
trigger_on_package_timestamp = false | ||
} |
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
#!/usr/bin/env bash | ||
# vim:ts=4:sw=4:noet | ||
|
||
set -eo pipefail | ||
|
||
trap ctrl_c INT | ||
|
||
ctrl_c() { | ||
echo "** Trapped CTRL-C" | ||
exit 1 | ||
} | ||
|
||
failed=0 | ||
|
||
:echo() { | ||
local color=${2:-"33;1"} | ||
echo -e "\e[${color}m$1\e[0m" | ||
} | ||
|
||
:note() { | ||
:echo "$1" "35;1" | ||
} | ||
|
||
:case() { | ||
if [ $? -ne 0 ] | ||
then failed=1 | ||
fi | ||
|
||
if [ "$failed" -eq 1 ] | ||
then :echo "SKIPPED: $1"; return 1 | ||
else echo; :echo "CASE: $1" | ||
fi | ||
} | ||
|
||
:check_diff() { | ||
expected="$1" | ||
|
||
set +e | ||
terraform plan -detailed-exitcode | ||
status=$? | ||
set -e | ||
# ${status} possible values: | ||
# 0 - Succeeded, diff is empty (no changes) | ||
# 1 - Errored | ||
# 2 - Succeeded, there is a diff | ||
if [ "${status}" -ne "${expected}" ]; then | ||
case "${expected}" in | ||
0) | ||
:echo "Error: we don't expect any diff here!" | ||
return 1 | ||
;; | ||
2) | ||
echo "Error: we DO expect some diff here!" | ||
return 1 | ||
;; | ||
esac | ||
fi | ||
} | ||
|
||
terraform=$(which terraform) | ||
terraform() { | ||
$terraform "$@" < <(yes yes) | ||
} | ||
|
||
:note "Preparing ..." | ||
rm -rf src | ||
mkdir -p src | ||
cp -r "../fixtures/python3.10-app1" src | ||
terraform init | ||
:echo "Destroy / Remove ZIP files" | ||
terraform destroy | ||
rm -rf builds 2>/dev/null || true | ||
|
||
############################################################# | ||
# Part 1: Check that CICD environment won't detect any diff # | ||
############################################################# | ||
|
||
:echo | ||
:note "Starting Part 1: Check that CICD environment won't detect any diff" | ||
|
||
:case "Apply / No diff" && { | ||
terraform apply | ||
:check_diff 0 | ||
} | ||
|
||
:case "Remove 'builds' dir / No diff" && { | ||
rm -rf builds | ||
:check_diff 0 | ||
} | ||
|
||
############################################################################### | ||
# Part 2: Check that CICD environment will detect diff if lambda code changes # | ||
############################################################################### | ||
|
||
:echo | ||
:note "Starting Part 2: Check that CICD environment will detect diff if lambda code changes" | ||
|
||
:note "Change the source code / Remove 'builds' dir" | ||
echo "" >> src/python3.10-app1/index.py | ||
rm -rf builds | ||
|
||
:case "Plan / Expect diff" && { | ||
terraform plan | ||
:check_diff 2 | ||
} | ||
|
||
:case "Apply / No diff" && { | ||
terraform apply | ||
:check_diff 0 | ||
} | ||
|
||
:note "Remove 'builds' dir" | ||
rm -rf builds | ||
|
||
:case "Plan / No diff" && { | ||
terraform plan | ||
:check_diff 0 | ||
} | ||
|
||
#:case "Destroy / Remove ZIP files" && { | ||
# terraform plan -destroy | ||
# terraform destroy -auto-approve | ||
# rm builds/*.zip | ||
#} | ||
|
||
:note "All tests have passed successfully." |
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
terraform { | ||
required_version = ">= 1.0" | ||
|
||
required_providers { | ||
aws = { | ||
source = "hashicorp/aws" | ||
version = ">= 4.63" | ||
} | ||
random = { | ||
source = "hashicorp/random" | ||
version = ">= 2.0" | ||
} | ||
} | ||
} |
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
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
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