Skip to content

Commit

Permalink
fix: Bump runtime to python3.9
Browse files Browse the repository at this point in the history
  • Loading branch information
baolsen committed Feb 9, 2023
1 parent 10f9f35 commit 1ad57a3
Show file tree
Hide file tree
Showing 16 changed files with 42 additions and 46 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Please note we have a code of conduct, please follow it in all your interactions

- [ ] Add [semantics prefix](#semantic-pull-requests) to your PR or Commits (at least one of your commit groups)
- [ ] CI tests are passing
- [ ] README.md has been updated after any changes to variables and outputs. See https://github.com/cloudandthings/terraform-aws-clickops-notifer/#doc-generation
- [ ] README.md has been updated after any changes to variables and outputs. See https://github.com/cloudandthings/terraform-aws-clickops-notifier/#doc-generation
- [ ] Run pre-commit hooks `pre-commit run -a`

## Semantic Pull Requests
Expand Down
15 changes: 9 additions & 6 deletions .github/workflows/check-deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ jobs:
# CHECK DEPLOYMENT
#--------------------------------------------------------------
check-deployment:
env:
FUNCTION: costnotifier
RUNTIME: python3.9
# For public repos use runs-on: ubuntu-latest
# For private repos use runs-on: self-hosted
runs-on: ${{ vars.RUNS_ON }}
Expand All @@ -27,16 +30,16 @@ jobs:
# To test whether the deployment package is up to date, we only check the application code (excluding dependencies).
- name: 🛠️ build deployment
run: |
cp myfunction-python3.9.zip myfunction-python3.9.zip_original
cd myfunction
zip -r ../myfunction-python3.9.zip .
cp $FUNCTION-$RUNTIME.zip $FUNCTION-$RUNTIME.zip_original
cd $FUNCTION
zip -r ../$FUNCTION-$RUNTIME.zip .
- name: 🔍 check deployment
run: |
# Compare length and names of files in Zip.
# Building in Docker doesn't work, some files are still different.
[[ -f myfunction-python3.9.zip ]] || { echo "Deployment file not found."; exit 1; }
[[ -f $FUNCTION-$RUNTIME.zip ]] || { echo "Deployment file not found."; exit 1; }
diff \
<(unzip -vqq myfunction-python3.9.zip | awk '{$2=""; $3=""; $4=""; $5=""; $6=""; print}' | sort -k3 -f) \
<(unzip -vqq myfunction-python3.9.zip_original | awk '{$2=""; $3=""; $4=""; $5=""; $6=""; print}' | sort -k3 -f)
<(unzip -vqq $FUNCTION-$RUNTIME.zip | awk '{$2=""; $3=""; $4=""; $5=""; $6=""; print}' | sort -k3 -f) \
<(unzip -vqq $FUNCTION-$RUNTIME.zip_original | awk '{$2=""; $3=""; $4=""; $5=""; $6=""; print}' | sort -k3 -f)
FILES_CHANGED=$?
echo FILES_CHANGED=$FILES_CHANGED
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ This terraform module sends a daily cost report and the cost delta in a 7 day ro

The module also reports on the top 5 services attributing to the daily cost. Its a very rudimentary means of managing the cost of your AWS bill, but it does provide a 1000 ft view of the current expenses for the previous day. More on this module can be found on our [blog](https://medium.com/cloudandthings/aws-cost-notifier-e437bd311c54) on Medium.

### Lambda deployment package

The Lambda function is deployed using a `.zip` deployment package. The package is contained within this module and also attached to the GitHub release.

You may choose one of the following options for the deployment:

1. **default:** If no `s3_*` variables are specified, then the package is used by the lambda directly.
2. **recommended**: `s3_bucket` can be specified to upload the package to the S3 bucket, for the lambda to use.
- `s3_prefix` can be specified to override the default location.
- `upload_deployment_to_s3=false` can be specified to disable the upload, in which case the package must be placed on s3 manually.


<!-- BEGIN_TF_DOCS -->
### Examples

Expand All @@ -31,13 +43,13 @@ See `examples` dropdown on Terraform Cloud, or [browse here](/examples/).
| <a name="input_notification_schedule"></a> [notification\_schedule](#input\_notification\_schedule) | CRON expression to schedule notification | `string` | `"cron(0 20 ? * MON-SUN *)"` | no |
| <a name="input_permissions_boundary"></a> [permissions\_boundary](#input\_permissions\_boundary) | ARN of the policy that is used to set the permissions boundary for the role. | `string` | `null` | no |
| <a name="input_red_threshold"></a> [red\_threshold](#input\_red\_threshold) | Percentage exceeded threshold to send a red alert and notify the slack channel | `string` | `"50"` | no |
| <a name="input_runtime"></a> [runtime](#input\_runtime) | The python runtime for the lambda. Currently only `python3.8` is supported. | `string` | `"python3.8"` | no |
| <a name="input_runtime"></a> [runtime](#input\_runtime) | The python runtime for the lambda. Currently only `python3.9` is supported. | `string` | `"python3.9"` | no |
| <a name="input_s3_bucket"></a> [s3\_bucket](#input\_s3\_bucket) | S3 bucket for deployment package. | `string` | `null` | no |
| <a name="input_s3_key"></a> [s3\_key](#input\_s3\_key) | S3 object key for deployment package. Otherwise, defaults to `var.naming_prefix/local.deployment_filename`. | `string` | `null` | no |
| <a name="input_security_group_ids"></a> [security\_group\_ids](#input\_security\_group\_ids) | List of VPC security group IDs associated with the Lambda function. | `list(string)` | `[]` | no |
| <a name="input_subnet_ids"></a> [subnet\_ids](#input\_subnet\_ids) | List of VPC subnet IDs associated with the Lambda function. | `list(string)` | `[]` | no |
| <a name="input_tags"></a> [tags](#input\_tags) | A mapping of tags to assign to the resources. | `map(string)` | `{}` | no |
| <a name="input_upload_deployment_to_s3"></a> [upload\_deployment\_to\_s3](#input\_upload\_deployment\_to\_s3) | If `true`, the deployment package within this module repo will be copied to S3. If `false` then the S3 object must be uploaded separately. | `bool` | `false` | no |
| <a name="input_upload_deployment_to_s3"></a> [upload\_deployment\_to\_s3](#input\_upload\_deployment\_to\_s3) | If `true`, the deployment package within this module repo will be copied to S3. If `false` then the S3 object must be uploaded separately. Ignored if `s3_bucket` is null. | `bool` | `true` | no |
| <a name="input_webhook_type"></a> [webhook\_type](#input\_webhook\_type) | Either "slack" or "teams". | `string` | `"slack"` | no |
| <a name="input_webhook_urls"></a> [webhook\_urls](#input\_webhook\_urls) | Webhook URLs to receive daily cost notifications on either Slack or Teams | `list(string)` | n/a | yes |

Expand Down
Binary file modified costnotifier-python3.9.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion costnotifier/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
AMBER_THRESHOLD = float(os.environ.get("AMBER_THRESHOLD", 20))
RED_THRESHOLD = float(os.environ.get("RED_THRESHOLD", 50))

WEBHOOK_URLS = json.loads(os.environ.get("WEBHOOK_URLS", []))
WEBHOOK_URLS = json.loads(os.environ.get("WEBHOOK_URLS", "[]"))
WEBHOOK_TYPE = os.environ.get("WEBHOOK_TYPE", "slack")

TOPIC_ARN = os.environ.get("SNS_ARN", "DISABLED")
Expand Down
4 changes: 1 addition & 3 deletions examples/email/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ module "example" {
emails_for_notifications = ["[email protected]", "[email protected]"] # Optional
### Uncomment to fetch the deployment package from S3
### Uncomment to upload and fetch the deployment package from S3
# s3_bucket = "my_s3_bucket"
### Uncomment to upload the local deployment package to S3
# upload_deployment_to_s3 = true
}
```
----
Expand Down
4 changes: 1 addition & 3 deletions examples/email/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ module "example" {

emails_for_notifications = ["[email protected]", "[email protected]"] # Optional

### Uncomment to fetch the deployment package from S3
### Uncomment to upload and fetch the deployment package from S3
# s3_bucket = "my_s3_bucket"
### Uncomment to upload the local deployment package to S3
# upload_deployment_to_s3 = true
}
4 changes: 1 addition & 3 deletions examples/slack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ module "example" {
notification_schedule = "cron(0 7 ? * MON-FRI *)"
### Uncomment to fetch the deployment package from S3
### Uncomment to upload and fetch the deployment package from S3
# s3_bucket = "my_s3_bucket"
### Uncomment to upload the local deployment package to S3
# upload_deployment_to_s3 = true
}
```
----
Expand Down
4 changes: 1 addition & 3 deletions examples/slack/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ module "example" {

notification_schedule = "cron(0 7 ? * MON-FRI *)"

### Uncomment to fetch the deployment package from S3
### Uncomment to upload and fetch the deployment package from S3
# s3_bucket = "my_s3_bucket"
### Uncomment to upload the local deployment package to S3
# upload_deployment_to_s3 = true
}
4 changes: 1 addition & 3 deletions examples/teams/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ module "billing_notifier_root_account_teams" {
notification_schedule = "cron(0 20 * * ? *)"
### Uncomment to fetch the deployment package from S3
### Uncomment to upload and fetch the deployment package from S3
# s3_bucket = "my_s3_bucket"
### Uncomment to upload the local deployment package to S3
# upload_deployment_to_s3 = true
}
```
----
Expand Down
4 changes: 1 addition & 3 deletions examples/teams/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ module "billing_notifier_root_account_teams" {

notification_schedule = "cron(0 20 * * ? *)"

### Uncomment to fetch the deployment package from S3
### Uncomment to upload and fetch the deployment package from S3
# s3_bucket = "my_s3_bucket"
### Uncomment to upload the local deployment package to S3
# upload_deployment_to_s3 = true
}
6 changes: 2 additions & 4 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,14 @@ locals {
deployment_path = "${path.module}/${local.deployment_filename}"

use_s3 = (
var.upload_deployment_to_s3 ||
var.s3_bucket != null ||
var.s3_key != null
var.upload_deployment_to_s3 && var.s3_bucket != null
)

s3_key = coalesce(var.s3_key, join("/", [var.naming_prefix, local.deployment_filename]))
}

resource "aws_s3_object" "deployment" {
count = var.upload_deployment_to_s3 ? 1 : 0
count = local.use_s3 && var.upload_deployment_to_s3 ? 1 : 0
bucket = var.s3_bucket
key = local.s3_key
source = local.deployment_path
Expand Down
1 change: 1 addition & 0 deletions tests/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
-r ../costnotifier/requirements.txt

# Additional packages to install when doing testing
boto3
pytest
tftest
10 changes: 2 additions & 8 deletions tests/test_lambda_handler.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import sys

from costnotifer import app
from costnotifier import app


def test_lambda_handler():
"""
This test ensures that the lambda handler function is callable.
"""
test_event = None
result = app.lambda_handler(test_event)

expected_result = "Hello from AWS Lambda using Python" + sys.version + "! 200"

assert result == expected_result
app.lambda_handler(test_event, None)
12 changes: 6 additions & 6 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ variable "cloudwatch_logs_retention_in_days" {
}

variable "runtime" {
description = "The python runtime for the lambda. Currently only `python3.8` is supported."
description = "The python runtime for the lambda. Currently only `python3.9` is supported."
type = string
default = "python3.8"
default = "python3.9"

validation {
condition = contains(["python3.8"], lower(var.runtime))
error_message = "Must be one of: \"python3.8\"."
condition = contains(["python3.9"], lower(var.runtime))
error_message = "Must be one of: \"python3.9\"."
}
}

Expand Down Expand Up @@ -155,7 +155,7 @@ variable "s3_key" {
}

variable "upload_deployment_to_s3" {
description = "If `true`, the deployment package within this module repo will be copied to S3. If `false` then the S3 object must be uploaded separately."
description = "If `true`, the deployment package within this module repo will be copied to S3. If `false` then the S3 object must be uploaded separately. Ignored if `s3_bucket` is null."
type = bool
default = false
default = true
}

0 comments on commit 1ad57a3

Please sign in to comment.