From 146790434b31bd2e44ac329478b5a866da20d3f4 Mon Sep 17 00:00:00 2001 From: Simson Garfinkel Date: Thu, 8 Feb 2024 13:20:28 -0500 Subject: [PATCH 1/5] added a clarification note to the documentation. --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index cd5c79b87..3017c39b4 100644 --- a/README.md +++ b/README.md @@ -218,10 +218,10 @@ This creates a new archive, uploads it to S3 and updates the Lambda function to #### Docker Workflows -In [version 0.53.0](https://github.com/zappa/Zappa/blob/master/CHANGELOG.md), support was added to deploy & update Lambda functions using Docker. +In [version 0.53.0](https://github.com/zappa/Zappa/blob/master/CHANGELOG.md), support was added to deploy & update Lambda functions using Docker. You can specify an ECR image using the `--docker-image-uri` option to the zappa command on `deploy` and `update`. -Zappa expects that the image is built and pushed to a Amazon ECR repository. +Zappa expects that the image is built and pushed to a Amazon ECR repository. Deploy Example: @@ -492,6 +492,8 @@ Amazon provides their own free alternative to Let's Encrypt called [AWS Certific 4. Set your desired domain in the `domain` setting. 5. Call `$ zappa certify` to create and associate the API Gateway distribution using that certificate. +(Note: No matter which region you are using, your certificate must be placed in us-east-1, which is the region in which AWS places many resources that are used globally.) + #### Deploying to a Domain With a Let's Encrypt Certificate (DNS Auth) If you want to use Zappa on a domain with a free Let's Encrypt certificate using automatic Route 53 based DNS Authentication, you can follow [this handy guide](https://github.com/zappa/Zappa/blob/master/docs/domain_with_free_ssl_dns.md). From 09547ddf34d98cd7d99a80c842ff290438883063 Mon Sep 17 00:00:00 2001 From: Simson Garfinkel Date: Mon, 19 Feb 2024 08:10:30 -0500 Subject: [PATCH 2/5] documentation update for AWS Certificate Manager and the ephemeral_storage option --- README.md | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 3017c39b4..7cf52c794 100644 --- a/README.md +++ b/README.md @@ -486,11 +486,12 @@ to skip the confirmation prompt. Amazon provides their own free alternative to Let's Encrypt called [AWS Certificate Manager](https://aws.amazon.com/certificate-manager/) (ACM). To use this service with Zappa: -1. Verify your domain in the AWS Certificate Manager console. -2. In the console, select the N. Virginia (us-east-1) region and request a certificate for your domain or subdomain (`sub.yourdomain.tld`), or request a wildcard domain (`*.yourdomain.tld`). -3. Copy the entire ARN of that certificate and place it in the Zappa setting `certificate_arn`. -4. Set your desired domain in the `domain` setting. -5. Call `$ zappa certify` to create and associate the API Gateway distribution using that certificate. +1. Add `'route53_enabled" : false` to your *zappa_settings.json* file. +2. Verify your domain in the AWS Certificate Manager console. +3. In the console, select the N. Virginia (us-east-1) region and request a certificate for your domain or subdomain (`sub.yourdomain.tld`), or request a wildcard domain (`*.yourdomain.tld`). +4. Copy the entire ARN of that certificate and place it in the Zappa setting `certificate_arn`. +5. Set your desired domain in the `domain` setting. +6. Call `$ zappa certify` to create and associate the API Gateway distribution using that certificate. (Note: No matter which region you are using, your certificate must be placed in us-east-1, which is the region in which AWS places many resources that are used globally.) @@ -1063,7 +1064,7 @@ You can also simply handle CORS directly in your application. Your web framework ### Large Projects -AWS currently limits Lambda zip sizes to 50 megabytes. If your project is larger than that, set `slim_handler: true` in your `zappa_settings.json`. In this case, your fat application package will be replaced with a small handler-only package. The handler file then pulls the rest of the large project down from S3 at run time! The initial load of the large project may add to startup overhead, but the difference should be minimal on a warm lambda function. Note that this will also eat into the storage space of your application function. Note that AWS [supports](https://aws.amazon.com/blogs/compute/using-larger-ephemeral-storage-for-aws-lambda/) custom `/tmp` directory storage size in a range of 512 - 10240 MB. Use `ephemeral_storage` in `zappa_settings.json` to adjust to your needs if your project is larger than default 512 MB. +AWS currently limits Lambda zip sizes to 50 megabytes. If your project is larger than that, set `slim_handler: true` in your `zappa_settings.json`. In this case, your fat application package will be replaced with a small handler-only package. The handler file then pulls the rest of the large project down from S3 at run time! The initial load of the large project may add to startup overhead, but the difference should be minimal on a warm lambda function. Note that this will also eat into the storage space of your application function. Note that AWS [supports](https://aws.amazon.com/blogs/compute/using-larger-ephemeral-storage-for-aws-lambda/) custom `/tmp` directory storage size in a range of 512 - 10240 MB. Use `ephemeral_storage` in `zappa_settings.json` to adjust to your needs if your project is larger than default `{"Size":1024}`. ### Enabling Bash Completion From 0c473d84ac01c6ee044ab1e901f2af1792fc8136 Mon Sep 17 00:00:00 2001 From: Simson Garfinkel Date: Mon, 19 Feb 2024 09:04:08 -0500 Subject: [PATCH 3/5] whoops! typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7cf52c794..c1574265d 100644 --- a/README.md +++ b/README.md @@ -1064,7 +1064,7 @@ You can also simply handle CORS directly in your application. Your web framework ### Large Projects -AWS currently limits Lambda zip sizes to 50 megabytes. If your project is larger than that, set `slim_handler: true` in your `zappa_settings.json`. In this case, your fat application package will be replaced with a small handler-only package. The handler file then pulls the rest of the large project down from S3 at run time! The initial load of the large project may add to startup overhead, but the difference should be minimal on a warm lambda function. Note that this will also eat into the storage space of your application function. Note that AWS [supports](https://aws.amazon.com/blogs/compute/using-larger-ephemeral-storage-for-aws-lambda/) custom `/tmp` directory storage size in a range of 512 - 10240 MB. Use `ephemeral_storage` in `zappa_settings.json` to adjust to your needs if your project is larger than default `{"Size":1024}`. +AWS currently limits Lambda zip sizes to 50 megabytes. If your project is larger than that, set `slim_handler: true` in your `zappa_settings.json`. In this case, your fat application package will be replaced with a small handler-only package. The handler file then pulls the rest of the large project down from S3 at run time! The initial load of the large project may add to startup overhead, but the difference should be minimal on a warm lambda function. Note that this will also eat into the storage space of your application function. Note that AWS [supports](https://aws.amazon.com/blogs/compute/using-larger-ephemeral-storage-for-aws-lambda/) custom `/tmp` directory storage size in a range of 512 - 10240 MB. Use `ephemeral_storage` in `zappa_settings.json` to adjust to your needs if your project is larger than default `{"Size":512}`. ### Enabling Bash Completion From d6fa4086475aab464e092059c7a1c2178102a687 Mon Sep 17 00:00:00 2001 From: Simson Garfinkel Date: Fri, 19 Apr 2024 13:07:27 -0400 Subject: [PATCH 4/5] added support for setting cloudwatch_retention_days --- README.md | 7 ++++++- zappa/cli.py | 7 +++++++ zappa/core.py | 14 ++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c1574265d..22103208c 100644 --- a/README.md +++ b/README.md @@ -426,7 +426,11 @@ You can filter out the contents of the logs with `--filter`, like so: $ zappa tail production --http --filter "POST" # Only show POST HTTP requests -Note that this uses the [CloudWatch Logs filter syntax](http://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/FilterAndPatternSyntax.html). +Please note: + + 1. This uses the [CloudWatch Logs filter syntax](http://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/FilterAndPatternSyntax.html). + + 2. Cloudwatch logs are kept forever unless you specify a retention time. You can do this by setting the configuration variable `cloudwatch_retention_days` to something reasonable, like 7. To tail logs without following (to exit immediately after displaying the end of the requested logs), pass `--disable-keep-open`: @@ -916,6 +920,7 @@ to change Zappa's behavior. Use these at your own risk! "cloudwatch_log_level": "OFF", // Enables/configures a level of logging for the given staging. Available options: "OFF", "INFO", "ERROR", default "OFF". "cloudwatch_data_trace": false, // Logs all data about received events. Default false. "cloudwatch_metrics_enabled": false, // Additional metrics for the API Gateway. Default false. + "cloudwatch_retention_days": false, // Specify a positive number to limit cloudwatch logs to this many days. "cognito": { // for Cognito event triggers "user_pool": "user-pool-id", // User pool ID from AWS Cognito "triggers": [{ diff --git a/zappa/cli.py b/zappa/cli.py index a89f9816a..f1622d9c6 100755 --- a/zappa/cli.py +++ b/zappa/cli.py @@ -1061,6 +1061,13 @@ def update(self, source_zip=None, no_upload=False, docker_image_uri=None): wait=False, ) + # Set the cloudwatch retention days if specified in config. + # (default is to never delete cloudwatch logs, which can become expensive over time.) + cloudwatch_retention_days = self.stage_config.get("cloudwatch_retention_days",False) + if cloudwatch_retention_days: + log_group_name = '/aws/lambda/' + conf['project_name'] + name + self.zappa.set_cloudwatch_log_retention(log_group_name, int(cloudwatch_retention_days)) + # Finally, delete the local copy our zip package if not source_zip and not no_upload and not docker_image_uri: if self.stage_config.get("delete_local_zip", True): diff --git a/zappa/core.py b/zappa/core.py index f71134f16..c294d368d 100644 --- a/zappa/core.py +++ b/zappa/core.py @@ -3151,6 +3151,20 @@ def fetch_logs(self, lambda_name, filter_pattern="", limit=10000, start_time=0): return sorted(events, key=lambda k: k["timestamp"]) + def set_cloudwatch_retention_days(self, group_name, retention_days): + """ + Specify the number of days that the logs should be retained. + """ + try: + self.logs_client.put_retention_policy( + logGroupName=group_name, + retentionInDays=retention_days + ) + print(f"Set CloudWatch log retention for {group_name} to {retention_days} days.") + except Exception as e: + print(f"Failed to set CloudWatch log retention: {str(e)}") + + def remove_log_group(self, group_name): """ Filter all log groups that match the name given in log_filter. From e67f469eee096fe93b05fdcd216152921cfbd1d4 Mon Sep 17 00:00:00 2001 From: Simson Garfinkel Date: Sun, 21 Apr 2024 14:43:38 -0400 Subject: [PATCH 5/5] completed implementation --- zappa/cli.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/zappa/cli.py b/zappa/cli.py index f1622d9c6..98f471674 100755 --- a/zappa/cli.py +++ b/zappa/cli.py @@ -1063,10 +1063,10 @@ def update(self, source_zip=None, no_upload=False, docker_image_uri=None): # Set the cloudwatch retention days if specified in config. # (default is to never delete cloudwatch logs, which can become expensive over time.) - cloudwatch_retention_days = self.stage_config.get("cloudwatch_retention_days",False) - if cloudwatch_retention_days: - log_group_name = '/aws/lambda/' + conf['project_name'] + name - self.zappa.set_cloudwatch_log_retention(log_group_name, int(cloudwatch_retention_days)) + cloudwatch_retention_days = self.stage_config.get("cloudwatch_retention_days",None) + if cloudwatch_retention_days is not None: + log_group_name = '/aws/lambda/' + self.project_name + self.api_stage + self.zappa.set_cloudwatch_retention_days(log_group_name, int(cloudwatch_retention_days)) # Finally, delete the local copy our zip package if not source_zip and not no_upload and not docker_image_uri: