Skip to content

Commit

Permalink
Add datetime to tagging
Browse files Browse the repository at this point in the history
  • Loading branch information
jameslinnell committed Oct 11, 2023
1 parent b7e9e20 commit cf04aaf
Show file tree
Hide file tree
Showing 14 changed files with 45 additions and 160 deletions.
3 changes: 2 additions & 1 deletion infrastructure/localstack/provider.tf
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ provider "aws" {
default_tags {
tags = {
Environment = var.environment
Created = local.created
LastUpdate = var.updated_date
Workspace = replace(terraform.workspace, "_", "-")
Project = local.project
Name = "${local.project}--${replace(terraform.workspace, "_", "-")}"
Owner = "NHSE"
ProjectShortName = "CPM"
ProjectFullname = "Connecting Party Manager"
Expiration = var.expiration_date
}
}
}
17 changes: 0 additions & 17 deletions infrastructure/terraform/per_account/main.tf
Original file line number Diff line number Diff line change
@@ -1,17 +0,0 @@
resource "aws_resourcegroups_group" "test" {
name = "workspace-resource-group"

resource_query {
query = <<JSON
{
"ResourceTypeFilters": [],
"TagFilters": [
{
"Key": "Workspace",
"Values": ["Test"]
}
]
}
JSON
}
}
6 changes: 3 additions & 3 deletions infrastructure/terraform/per_workspace/locals.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
locals {
region = "eu-west-2"
project = "nhse-cpm"
created = timestamp()
region = "eu-west-2"
project = "nhse-cpm"
current_time = timestamp()
}
9 changes: 6 additions & 3 deletions infrastructure/terraform/per_workspace/main.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
resource "aws_resourcegroups_group" "resource_group" {
name = "${local.project}--${replace(terraform.workspace, "_", "-")}--resource-group"
tags = {
Name = "${local.project}--${replace(terraform.workspace, "_", "-")}--resource-group"
Created = local.created
Name = "${local.project}--${replace(terraform.workspace, "_", "-")}--resource-group"
CreatedOn = local.current_time
}

lifecycle {
ignore_changes = [tags["CreatedOn"]]
}

resource_query {
Expand Down Expand Up @@ -38,7 +42,6 @@ module "products_table" {
]
deletion_protection_enabled = var.deletion_protection_enabled
kms_deletion_window_in_days = 7
created = local.created
}

# module "api_worker_create" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ module "dynamodb_table" {
point_in_time_recovery_enabled = true

tags = {
Name = var.name
Created = var.created
Name = var.name
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ module "iam_policy_read" {
description = "Read the ${var.name} table"

tags = {
Name = "${var.name}--iam-policy-read"
Created = var.created
Name = "${var.name}--iam-policy-read"
}

policy = jsonencode({
Expand Down Expand Up @@ -48,11 +47,9 @@ module "iam_policy_write" {
description = "Write to the ${var.name} table"

tags = {
Name = "${var.name}--iam-policy-write"
Created = var.created
Name = "${var.name}--iam-policy-write"
}


policy = jsonencode({
Version = "2012-10-17"
Statement = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ module "kms" {
aliases = [var.name]

tags = {
Name = "${var.name}--kms"
Created = var.created
Name = "${var.name}--kms"
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,3 @@ variable "deletion_protection_enabled" {
}

variable "kms_deletion_window_in_days" {}

variable "created" {}
3 changes: 2 additions & 1 deletion infrastructure/terraform/per_workspace/provider.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ provider "aws" {
default_tags {
tags = {
Environment = var.environment
Created = local.created
LastUpdated = var.updated_date
Workspace = replace(terraform.workspace, "_", "-")
Project = local.project
Name = "${local.project}--${replace(terraform.workspace, "_", "-")}"
Owner = "NHSE"
ProjectShortName = "CPM"
ProjectFullname = "Connecting Party Manager"
ExpirationDate = var.expiration_date
}
}
}
8 changes: 8 additions & 0 deletions infrastructure/terraform/per_workspace/vars.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,11 @@ variable "deletion_protection_enabled" {
type = bool
default = false
}

variable "expiration_date" {
default = "NEVER"
}

variable "updated_date" {
default = "NEVER"
}
12 changes: 0 additions & 12 deletions scripts/infrastructure/terraform.mk
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,3 @@ initialise--non-mgmt: aws--login ## Bootstrap the Non-MGMT AWS environments. Mus

destroy--non-mgmt: aws--login ## Destroy the Non-MGMT AWS environments. Must provide TERRAFORM_ROLE_NAME keyword argument.
@ AWS_ACCESS_KEY_ID=$(AWS_ACCESS_KEY_ID) AWS_SECRET_ACCESS_KEY=$(AWS_SECRET_ACCESS_KEY) AWS_SESSION_TOKEN=$(AWS_SESSION_TOKEN) bash $(PATH_TO_INFRASTRUCTURE)/destroy-non-mgmt-resources.sh $(TERRAFORM_ROLE_NAME)

terraform--ci-init: ## Run terraform init in the CI
@ AWS_ACCESS_KEY_ID=$(AWS_ACCESS_KEY_ID) AWS_SECRET_ACCESS_KEY=$(AWS_SECRET_ACCESS_KEY) AWS_SESSION_TOKEN=$(AWS_SESSION_TOKEN) PROJECT_PREFIX=$(PROJECT_PREFIX) bash $(PATH_TO_INFRASTRUCTURE)/terraform/terraform-ci-commands.sh ci-init $(TERRAFORM_ENVIRONMENT) $(TERRAFORM_ACCOUNT_WIDE) $(TERRAFORM_ARGS)

terraform--ci-plan: ## Run terraform plan in the CI
@ AWS_ACCESS_KEY_ID=$(AWS_ACCESS_KEY_ID) AWS_SECRET_ACCESS_KEY=$(AWS_SECRET_ACCESS_KEY) AWS_SESSION_TOKEN=$(AWS_SESSION_TOKEN) PROJECT_PREFIX=$(PROJECT_PREFIX) bash $(PATH_TO_INFRASTRUCTURE)/terraform/terraform-ci-commands.sh ci-plan $(TERRAFORM_ENVIRONMENT) $(TERRAFORM_ACCOUNT_WIDE) $(TERRAFORM_ARGS)

terraform--ci-apply: ## Run terraform apply in the CI
@ AWS_ACCESS_KEY_ID=$(AWS_ACCESS_KEY_ID) AWS_SECRET_ACCESS_KEY=$(AWS_SECRET_ACCESS_KEY) AWS_SESSION_TOKEN=$(AWS_SESSION_TOKEN) PROJECT_PREFIX=$(PROJECT_PREFIX) bash $(PATH_TO_INFRASTRUCTURE)/terraform/terraform-ci-commands.sh ci-apply $(TERRAFORM_ENVIRONMENT) $(TERRAFORM_ACCOUNT_WIDE) $(TERRAFORM_ARGS)

terraform--ci-destroy: ## Run terraform destroy in the CI
@ AWS_ACCESS_KEY_ID=$(AWS_ACCESS_KEY_ID) AWS_SECRET_ACCESS_KEY=$(AWS_SECRET_ACCESS_KEY) AWS_SESSION_TOKEN=$(AWS_SESSION_TOKEN) PROJECT_PREFIX=$(PROJECT_PREFIX) bash $(PATH_TO_INFRASTRUCTURE)/terraform/terraform-ci-commands.sh ci-destroy $(TERRAFORM_ENVIRONMENT) $(TERRAFORM_ACCOUNT_WIDE) $(TERRAFORM_ARGS)
109 changes: 0 additions & 109 deletions scripts/infrastructure/terraform/terraform-ci-commands.sh

This file was deleted.

11 changes: 8 additions & 3 deletions scripts/infrastructure/terraform/terraform-commands.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ function _terraform() {
local env
local aws_account_id
local var_file
local current_timestamp
local current_date
local terraform_dir
local expiration_time
env=$(_get_environment_name $TERRAFORM_ENVIRONMENT)
aws_account_id=$(_get_aws_account_id "$env")
var_file=$(_get_environment_vars_file "$env")
terraform_dir=$(_get_terraform_dir "$env" "$TERRAFORM_ACCOUNT_WIDE")
current_timestamp="$(date '+%Y_%m_%d__%H_%M_%S')"
expiration_date=$(_get_expiration_date)
current_date=$(_get_current_date)
local plan_file="./tfplan"
# local ci_log_bucket="${PROFILE_PREFIX}--mgmt--github-ci-logging"

Expand Down Expand Up @@ -94,13 +96,16 @@ function _terraform_plan() {
local aws_account_id=$4
local args=${@:5}


terraform init || return 1
terraform workspace select "$env" || terraform workspace new "$env" || return 1
terraform plan \
-out="$plan_file" \
-var-file="$var_file" \
-var "assume_account=${aws_account_id}" \
-var "assume_role=${TERRAFORM_ROLE_NAME}" || return 1
-var "assume_role=${TERRAFORM_ROLE_NAME}" \
-var "updated_date=${current_date}" \
-var "expiration_date=${expiration_date}" || return 1
}

function _terraform_apply() {
Expand Down
12 changes: 12 additions & 0 deletions scripts/infrastructure/terraform/terraform-utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,15 @@ function _get_terraform_dir() {
echo "${dir}/infrastructure/terraform/per_workspace"
fi
}

function _get_current_date() {
#local timestamp=$(date -d '+72 hours' -u +"%Y-%m-%dT%H:%M:%SZ")
local timestamp=$(python -c "from datetime import datetime, timedelta, timezone; print(format(datetime.now(timezone.utc), '%Y-%m-%dT%H:%M:%SZ'))")
echo "${timestamp}"
}

function _get_expiration_date() {
#local timestamp=$(date -d '+72 hours' -u +"%Y-%m-%dT%H:%M:%SZ")
local timestamp=$(python -c "from datetime import datetime, timedelta, timezone; print(format(datetime.now(timezone.utc) + timedelta(hours=72), '%Y-%m-%dT%H:%M:%SZ'))")
echo "${timestamp}"
}

0 comments on commit cf04aaf

Please sign in to comment.