Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLDC-3810: Automatically redeploy prod and staging when collection year rolls over #154

Merged
merged 12 commits into from
Dec 17, 2024
100 changes: 100 additions & 0 deletions terraform/modules/application/scheduled_redeploy.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
resource "aws_scheduler_schedule" "app_collection_rollover_redeploy" {
#checkov:skip=CKV_AWS_297: CMK not required here and seems to cause issues
count = var.collection_rollover_redeploy_enabled ? 1 : 0

name = "${var.prefix}-app-collection-rollover-redeploy"

schedule_expression = "cron(0 0 1 4 ? *)"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sometimes things on AWS are an hour off, like logs, or the time at which things are uploaded to S3. I'm not sure if it would impact this at all, but would it make sense to redeploy at 1am ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be set to execute in UTC (so by April it will be 1am local time), which means it will be fine - AWS times on logs / file uploads I think are always in UTC, the important one for this will be what day the app thinks it is which is probably based on UTC (but if not will still be ok this way round).

schedule_expression_timezone = "UTC"

flexible_time_window {
mode = "OFF"
}

target {
arn = "arn:aws:scheduler:::aws-sdk:ecs:updateService"
role_arn = aws_iam_role.scheduler[0].arn

input = jsonencode({
Cluster = aws_ecs_cluster.this.name,
Service = aws_ecs_service.app.name,
ForceNewDeployment = true
})
}
}

resource "aws_scheduler_schedule" "sidekiq_collection_rollover_redeploy" {
#checkov:skip=CKV_AWS_297: CMK not required here and seems to cause issues
count = var.collection_rollover_redeploy_enabled ? 1 : 0

name = "${var.prefix}-sidekiq-collection-rollover-redeploy"

schedule_expression = "cron(0 0 1 4 ? *)"
schedule_expression_timezone = "UTC"

flexible_time_window {
mode = "OFF"
}

target {
arn = "arn:aws:scheduler:::aws-sdk:ecs:updateService"
role_arn = aws_iam_role.scheduler[0].arn

input = jsonencode({
Cluster = aws_ecs_cluster.this.name,
Service = aws_ecs_service.sidekiq.name,
ForceNewDeployment = true
})
}
}

resource "aws_iam_role" "scheduler" {
count = var.collection_rollover_redeploy_enabled ? 1 : 0

name = "${var.prefix}-rds-scheduler"
assume_role_policy = data.aws_iam_policy_document.scheduler_assume_role[0].json
}

data "aws_iam_policy_document" "scheduler_assume_role" {
count = var.collection_rollover_redeploy_enabled ? 1 : 0

statement {
actions = ["sts:AssumeRole"]
effect = "Allow"

principals {
type = "Service"
identifiers = ["scheduler.amazonaws.com"]
}
}
}

data "aws_iam_policy_document" "allow_ecs_actions" {
count = var.collection_rollover_redeploy_enabled ? 1 : 0

statement {
actions = [
"ecs:Describe*",
"ecs:UpdateService"
]
resources = [
aws_ecs_service.app.id,
aws_ecs_service.sidekiq.id
]
effect = "Allow"
}
}

resource "aws_iam_policy" "allow_ecs_actions" {
count = var.collection_rollover_redeploy_enabled ? 1 : 0

name = "${var.prefix}-allow-ecs-actions"
policy = data.aws_iam_policy_document.allow_ecs_actions[0].json
}

resource "aws_iam_role_policy_attachment" "scheduler_allow_ecs_actions" {
count = var.collection_rollover_redeploy_enabled ? 1 : 0

role = aws_iam_role.scheduler[0].name
policy_arn = aws_iam_policy.allow_ecs_actions[0].arn
}
6 changes: 6 additions & 0 deletions terraform/modules/application/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ variable "collection_resources_bucket_details" {
description = "Details block for collection resources bucket"
}

variable "collection_rollover_redeploy_enabled" {
type = bool
description = "Schedules redeploy overnight on the 1st April if true"
default = false
}

variable "database_name" {
type = string
description = "The name of the database to connect to"
Expand Down
2 changes: 2 additions & 0 deletions terraform/production/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ module "application" {
}
}

collection_rollover_redeploy_enabled = true

ecr_repository_url = "815624722760.dkr.ecr.eu-west-2.amazonaws.com/core"

prefix = local.prefix
Expand Down
2 changes: 2 additions & 0 deletions terraform/staging/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions terraform/staging/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ module "application" {
}
}

collection_rollover_redeploy_enabled = true

ecr_repository_url = "815624722760.dkr.ecr.eu-west-2.amazonaws.com/core"

prefix = local.prefix
Expand Down
Loading