Skip to content

Commit 65f3717

Browse files
authored
feat: Added support for random sleep delay in deploy submodule (#233)
1 parent 0fa0e0b commit 65f3717

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

examples/deploy/main.tf

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,6 @@ module "lambda_function" {
2323

2424
source_path = "${path.module}/../fixtures/python3.8-app1"
2525
hash_extra = "yo1"
26-
27-
allowed_triggers = {
28-
APIGatewayAny = {
29-
service = "apigateway"
30-
source_arn = "arn:aws:execute-api:eu-west-1:135367859851:aqnku8akd0/*/*/*"
31-
}
32-
}
3326
}
3427

3528
module "alias_refresh" {
@@ -62,6 +55,7 @@ module "deploy" {
6255
deployment_group_name = "something"
6356

6457
create_deployment = true
58+
run_deployment = true
6559
save_deploy_script = true
6660
wait_deployment_completion = true
6761
force_deploy = true

modules/deploy/README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This module can create AWS CodeDeploy application and deployment group, if neces
88

99
During deployment this module does the following:
1010
1. Create JSON object with required AppSpec configuration. Optionally, you can store deploy script for debug purposes by setting `save_deploy_script = true`.
11-
1. Run [`aws deploy create-deployment` command](https://docs.aws.amazon.com/cli/latest/reference/deploy/create-deployment.html) if `create_deployment = true` was set
11+
1. Run [`aws deploy create-deployment` command](https://docs.aws.amazon.com/cli/latest/reference/deploy/create-deployment.html) if `create_deployment = true` and `run_deployment = true` was set.
1212
1. After deployment is created, it can wait for the completion if `wait_deployment_completion = true`. Be aware, that Terraform will lock the execution and it can fail if it runs for a long period of time. Set this flag for fast deployments (eg, `deployment_config_name = "CodeDeployDefault.LambdaAllAtOnce"`).
1313

1414

@@ -53,6 +53,7 @@ module "deploy" {
5353
deployment_group_name = "something"
5454
5555
create_deployment = true
56+
run_deployment = true
5657
wait_deployment_completion = true
5758
5859
triggers = {
@@ -158,15 +159,17 @@ No modules.
158159
| <a name="input_create"></a> [create](#input\_create) | Controls whether resources should be created | `bool` | `true` | no |
159160
| <a name="input_create_app"></a> [create\_app](#input\_create\_app) | Whether to create new AWS CodeDeploy app | `bool` | `false` | no |
160161
| <a name="input_create_codedeploy_role"></a> [create\_codedeploy\_role](#input\_create\_codedeploy\_role) | Whether to create new AWS CodeDeploy IAM role | `bool` | `true` | no |
161-
| <a name="input_create_deployment"></a> [create\_deployment](#input\_create\_deployment) | Run AWS CLI command to create deployment | `bool` | `false` | no |
162+
| <a name="input_create_deployment"></a> [create\_deployment](#input\_create\_deployment) | Create the AWS resources and script for CodeDeploy | `bool` | `false` | no |
162163
| <a name="input_create_deployment_group"></a> [create\_deployment\_group](#input\_create\_deployment\_group) | Whether to create new AWS CodeDeploy Deployment Group | `bool` | `false` | no |
163164
| <a name="input_current_version"></a> [current\_version](#input\_current\_version) | Current version of Lambda function version to deploy (can't be $LATEST) | `string` | `""` | no |
164165
| <a name="input_deployment_config_name"></a> [deployment\_config\_name](#input\_deployment\_config\_name) | Name of deployment config to use | `string` | `"CodeDeployDefault.LambdaAllAtOnce"` | no |
165166
| <a name="input_deployment_group_name"></a> [deployment\_group\_name](#input\_deployment\_group\_name) | Name of deployment group to use | `string` | `""` | no |
166167
| <a name="input_description"></a> [description](#input\_description) | Description to use for the deployment | `string` | `""` | no |
167168
| <a name="input_force_deploy"></a> [force\_deploy](#input\_force\_deploy) | Force deployment every time (even when nothing changes) | `bool` | `false` | no |
168169
| <a name="input_function_name"></a> [function\_name](#input\_function\_name) | The name of the Lambda function to deploy | `string` | `""` | no |
170+
| <a name="input_get_deployment_sleep_timer"></a> [get\_deployment\_sleep\_timer](#input\_get\_deployment\_sleep\_timer) | Adds additional sleep time to get-deployment command to avoid the service throttling | `number` | `5` | no |
169171
| <a name="input_interpreter"></a> [interpreter](#input\_interpreter) | List of interpreter arguments used to execute deploy script, first arg is path | `list(string)` | <pre>[<br> "/bin/bash",<br> "-c"<br>]</pre> | no |
172+
| <a name="input_run_deployment"></a> [run\_deployment](#input\_run\_deployment) | Run AWS CLI command to start the deployment | `bool` | `false` | no |
170173
| <a name="input_save_deploy_script"></a> [save\_deploy\_script](#input\_save\_deploy\_script) | Save deploy script locally | `bool` | `false` | no |
171174
| <a name="input_tags"></a> [tags](#input\_tags) | A map of tags to assign to resources. | `map(string)` | `{}` | no |
172175
| <a name="input_target_version"></a> [target\_version](#input\_target\_version) | Target version of Lambda function version to deploy | `string` | `""` | no |

modules/deploy/main.tf

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ while [[ $STATUS == "Created" || $STATUS == "InProgress" || $STATUS == "Pending"
6060
--deployment-id $ID \
6161
--output text \
6262
--query '[deploymentInfo.status]')
63-
sleep 5
63+
64+
SLEEP_TIME=$(( $RANDOM % 5 ) + ${var.get_deployment_sleep_timer})
65+
echo "Sleeping for: $SLEEP_TIME Seconds"
66+
sleep $SLEEP_TIME
6467
done
6568
6669
${var.aws_cli_command} deploy get-deployment --deployment-id $ID
@@ -106,7 +109,7 @@ resource "local_file" "deploy_script" {
106109
}
107110

108111
resource "null_resource" "deploy" {
109-
count = var.create && var.create_deployment ? 1 : 0
112+
count = var.create && var.create_deployment && var.run_deployment ? 1 : 0
110113

111114
triggers = {
112115
appspec_sha256 = local.appspec_sha256

modules/deploy/variables.tf

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,13 @@ variable "save_deploy_script" {
161161
}
162162

163163
variable "create_deployment" {
164-
description = "Run AWS CLI command to create deployment"
164+
description = "Create the AWS resources and script for CodeDeploy"
165+
type = bool
166+
default = false
167+
}
168+
169+
variable "run_deployment" {
170+
description = "Run AWS CLI command to start the deployment"
165171
type = bool
166172
default = false
167173
}
@@ -211,3 +217,9 @@ variable "attach_triggers_policy" {
211217
type = bool
212218
default = false
213219
}
220+
221+
variable "get_deployment_sleep_timer" {
222+
description = "Adds additional sleep time to get-deployment command to avoid the service throttling"
223+
type = number
224+
default = 5
225+
}

0 commit comments

Comments
 (0)