Skip to content

Commit

Permalink
continued clean up, removing comments, updating resource and variable…
Browse files Browse the repository at this point in the history
… names, fmt
  • Loading branch information
alismx committed Jun 24, 2024
1 parent 2b3c8c3 commit ba79925
Show file tree
Hide file tree
Showing 17 changed files with 70 additions and 88 deletions.
5 changes: 4 additions & 1 deletion terraform/implementation/ecs/_local.tf
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ locals {
}
}

appmesh_name = "${var.project}-${var.appmesh_name}-${var.owner}-${terraform.workspace}"
cloudmap_namespace_name = "${var.project}-${var.cloudmap_namespace_name}-${var.owner}-${terraform.workspace}"
cloudmap_service_name = "${var.project}-${var.cloudmap_service_name}-${var.owner}-${terraform.workspace}"
ecs_ecr_policy_name = "${var.project}-${var.ecs_ecr_policy_name}-${var.owner}-${terraform.workspace}"
ecs_alb_sg = "${var.project}-${var.ecs_alb_sg}-${var.owner}-${terraform.workspace}"
ecs_alb_name = "${var.project}-${var.ecs_alb_name}-${var.owner}-${terraform.workspace}"
Expand All @@ -128,7 +131,7 @@ locals {
s3_viewer_bucket_name = "${var.project}-${var.s3_viewer_bucket_name}-${var.owner}-${terraform.workspace}-${random_string.s3_viewer.result}"
s3_viewer_bucket_role_name = "${var.project}-${var.s3_viewer_bucket_role_name}-${var.owner}-${terraform.workspace}"
s3_viewer_bucket_policy_name = "${var.project}-${var.s3_viewer_bucket_policy_name}-${var.owner}-${terraform.workspace}"
vpc = "${var.project}-${var.vpc}-${var.owner}-${terraform.workspace}"
vpc_name = "${var.project}-${var.vpc}-${var.owner}-${terraform.workspace}"

enable_nat_gateway = var.enable_nat_gateway
single_nat_gateway = var.single_nat_gateway
Expand Down
12 changes: 12 additions & 0 deletions terraform/implementation/ecs/_variable.tf
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,16 @@ variable "ecs_cloudwatch_policy_name" {
variable "ecs_cloudwatch_role_name" {
type = string
default = "ecs-cwr"
}
variable "cloudmap_namespace_name" {
type = string
default = "cloudmap-service-connect"
}
variable "cloudmap_service_name" {
type = string
default = "cloudmap-services"
}
variable "appmesh_name" {
type = string
default = "appmesh"
}
3 changes: 2 additions & 1 deletion terraform/implementation/ecs/backend.tf
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ provider "aws" {
default_tags {
tags = {
owner = var.owner
Environment = terraform.workspace
environment = terraform.workspace
project = var.project
}
}
}
4 changes: 2 additions & 2 deletions terraform/implementation/ecs/ecs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ fi
if ! grep -q "project" "$ENVIRONMENT.tfvars"; then
read -p "What is this project called? ( default=dibbs ): " project_choice
project_choice=${project_choice:-dibbs}
echo "project = \"$project_choice\"" >> "$ENVIRONMENT.tfvars"
echo "project = \"$project_choice\"" >> "$ENVIRONMENT.tfvars"
fi

if ! grep -q "region" "$ENVIRONMENT.tfvars"; then
read -p "What aws region are you setting up in? ( default=us-east-1 ): " region_choice
region_choice=${region_choice:-us-east-1}
echo "region = \"$region_choice\"" >> "$ENVIRONMENT.tfvars"
echo "region = \"$region_choice\"" >> "$ENVIRONMENT.tfvars"
fi

echo "Running Terraform with the following variables:"
Expand Down
12 changes: 6 additions & 6 deletions terraform/implementation/ecs/main.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
name = local.vpc
name = local.vpc_name
default_security_group_name = local.ecs_alb_sg
cidr = var.vpc_cidr
azs = var.availability_zones
Expand Down Expand Up @@ -28,8 +28,6 @@ module "ecr" {
service_data = local.service_data
phdi_version = var.phdi_version
ecs_cluster_name = local.ecs_cluster_name
tags = {}
lifecycle_policy = ""
region = var.region
}

Expand All @@ -50,10 +48,12 @@ module "ecs" {
availability_zones = module.vpc.azs
ecs_task_execution_role_arn = module.iam.ecs_task_execution_role.arn
ecs_cluster_name = local.ecs_cluster_name
app_task_name = local.ecs_app_task_name
alb_name = local.ecs_alb_name
ecs_alb_name = local.ecs_alb_name
ecs_cloudwatch_group = local.ecs_cloudwatch_group
service_data = local.service_data
retention_in_days = var.cw_retention_in_days
cw_retention_in_days = var.cw_retention_in_days
region = var.region
cloudmap_namespace_name = local.cloudmap_namespace_name
cloudmap_service_name = local.cloudmap_service_name
appmesh_name = local.appmesh_name
}
1 change: 1 addition & 0 deletions terraform/implementation/setup/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ provider "aws" {
tags = {
Owner = var.owner
Environment = terraform.workspace
project = var.project
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions terraform/modules/ecr/_local.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
locals {
policy = var.lifecycle_policy == "" ? file("${path.module}/ecr-lifecycle-policy.json") : var.lifecycle_policy
tags = {
Automation = "Terraform"
}
policy = file("${path.module}/ecr-lifecycle-policy.json")
}
2 changes: 0 additions & 2 deletions terraform/modules/ecr/_output.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,3 @@
output "repository_url" {
value = [for repo in aws_ecr_repository.repo : repo.repository_url]
}


12 changes: 0 additions & 12 deletions terraform/modules/ecr/_variable.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,6 @@ variable "ecs_cluster_name" {
description = "ECS Cluster Name"
}

variable "lifecycle_policy" {
type = string
description = "ECR repository lifecycle policy document. Used to override the default policy."
# default = ""
}

variable "tags" {
type = map(any)
description = "Additional tags to apply."
# default = {}
}

variable "aws_caller_identity" {
type = string
description = "AWS Caller Identity"
Expand Down
3 changes: 0 additions & 3 deletions terraform/modules/ecr/docker.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# Pull images from GitHub Container Registry and push to AWS Elastic Container Registry

resource "time_static" "now" {}

# NOTE: This pulls image down from the docker registry
resource "docker_image" "ghcr_image" {
for_each = var.service_data
name = data.docker_registry_image.ghcr_data[each.key].name
Expand Down
4 changes: 2 additions & 2 deletions terraform/modules/ecr/ecr.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
resource "aws_ecr_repository" "repo" {
for_each = var.service_data
name = each.key
for_each = var.service_data
name = each.key
force_delete = true
}
31 changes: 16 additions & 15 deletions terraform/modules/ecs/_variable.tf
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
variable "app_task_name" {
description = "ECS Task Name"
type = string
}

variable "alb_name" {
variable "ecs_alb_name" {
description = "ALB Name"
type = string
}
Expand Down Expand Up @@ -57,18 +52,10 @@ variable "fargate_memory" {
default = "2048"
}

# Note: Retention period can change (i.e. 0, 7, 14, 90, 180, etc.)
# See https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group
# In addition, if you want to delete log groups set "skip_destroy" to false
variable "retention_in_days" {
variable "cw_retention_in_days" {
type = number
}


#################
### NETWORKING ##
#################

variable "vpc_id" {
type = string
description = "ID of the VPC"
Expand All @@ -88,3 +75,17 @@ variable "service_data" {
type = map(any)
description = "Environment variables to pass to the container"
}

variable "cloudmap_namespace_name" {
type = string
description = ""
}
variable "cloudmap_service_name" {
type = string
description = ""
}

variable "appmesh_name" {
type = string
description = ""
}
9 changes: 3 additions & 6 deletions terraform/modules/ecs/alb.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
resource "aws_alb" "ecs" {
name = var.alb_name
name = var.ecs_alb_name
internal = false
load_balancer_type = "application"
subnets = flatten([var.public_subnet_ids])
Expand All @@ -8,11 +8,10 @@ resource "aws_alb" "ecs" {
enable_deletion_protection = false

tags = {
Name = var.alb_name
Name = var.ecs_alb_name
}
}

# Defines the target gropu associated with the ALB
resource "aws_alb_target_group" "this" {
for_each = {
for key, value in var.service_data : key => value
Expand Down Expand Up @@ -76,10 +75,8 @@ resource "aws_alb_listener_rule" "this" {

}

# Security Group for ECS
resource "aws_security_group" "ecs" {
vpc_id = var.vpc_id
# TODO parameterize sg name
vpc_id = var.vpc_id
name = "dibbs-aws-ecs"
description = "Security group for ECS"
revoke_rules_on_delete = true
Expand Down
5 changes: 5 additions & 0 deletions terraform/modules/ecs/ecs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ resource "aws_ecs_service" "this" {
type = "ECS"
}

force_new_deployment = true
triggers = {
redeployment = plantimestamp()
}

dynamic "load_balancer" {
# The conditional for this for_each checks the key for the current interation of aws_ecs_task_definition.this
# and var.service_data so that we only create a dynamic load_balancer block for the public services.
Expand Down
19 changes: 5 additions & 14 deletions terraform/modules/ecs/logs.tf
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
resource "aws_cloudwatch_log_group" "ecs_cloudwatch_logs" {
name = var.ecs_cloudwatch_group
retention_in_days = var.retention_in_days
retention_in_days = var.cw_retention_in_days
}

resource "aws_flow_log" "ecs_flow_log" {
vpc_id = var.vpc_id

# ARN of the IAM role that the flow log will assume to publish logs to CloudWatch Logs
iam_role_arn = var.ecs_task_execution_role_arn

# The type of traffic to capture. Valid values are ACCEPT, REJECT, or ALL.
traffic_type = "ALL"

# The ARN of the CloudWatch Logs group where the flow logs will be published
log_destination = aws_cloudwatch_log_group.ecs_cloudwatch_logs.arn

# IAM policy document for the IAM role assumed by the flow log
# Replace this with your own policy document as needed
vpc_id = var.vpc_id
iam_role_arn = var.ecs_task_execution_role_arn
traffic_type = "ALL"
log_destination = aws_cloudwatch_log_group.ecs_cloudwatch_logs.arn
log_destination_type = "cloud-watch-logs"
}

30 changes: 11 additions & 19 deletions terraform/modules/ecs/mesh.tf
Original file line number Diff line number Diff line change
@@ -1,35 +1,29 @@
# Create a Service Discovery Namespace
resource "aws_service_discovery_private_dns_namespace" "dibbs_aws_ecs_ns" {
# TODO parameterize name
name = "dibbs-aws-ecs-service-connect-ns"
resource "aws_service_discovery_private_dns_namespace" "this" {
name = var.cloudmap_namespace_name
vpc = var.vpc_id
}

# Register ECS Services with Service Discovery
resource "aws_service_discovery_service" "this" {
# TODO parameterize name
name = "alis-services"
namespace_id = aws_service_discovery_private_dns_namespace.dibbs_aws_ecs_ns.id
name = var.cloudmap_service_name
namespace_id = aws_service_discovery_private_dns_namespace.this.id
dns_config {
namespace_id = aws_service_discovery_private_dns_namespace.dibbs_aws_ecs_ns.id
namespace_id = aws_service_discovery_private_dns_namespace.this.id
dns_records {
ttl = 60
type = "A"
}
}
}

# Define the AWS App Mesh resources
resource "aws_appmesh_mesh" "dibbs_aws_ecs_mesh" {
# TODO parameterize name
name = "dibbs-aws-ecs-mesh"
resource "aws_appmesh_mesh" "this" {
name = var.appmesh_name
}

# Define the AWS App Mesh resources
resource "aws_appmesh_virtual_node" "this" {
for_each = aws_ecs_service.this
name = each.key
mesh_name = aws_appmesh_mesh.dibbs_aws_ecs_mesh.name
mesh_name = aws_appmesh_mesh.this.name

# dynamic "spec" {
# # The conditional for this for_each checks the key for the current interation of aws_ecs_task_definition.this
Expand All @@ -49,7 +43,7 @@ resource "aws_appmesh_virtual_node" "this" {
# service_discovery {
# aws_cloud_map {
# service_name = spec.key
# namespace_name = aws_service_discovery_private_dns_namespace.dibbs_aws_ecs_ns.id
# namespace_name = aws_service_discovery_private_dns_namespace.this.id
# # namespace_name = "dibbs-aws-service-connect-ns"
# }
# }
Expand All @@ -73,18 +67,16 @@ resource "aws_appmesh_virtual_node" "this" {
service_discovery {
aws_cloud_map {
service_name = each.key
namespace_name = aws_service_discovery_private_dns_namespace.dibbs_aws_ecs_ns.id
# namespace_name = "dibbs-aws-service-connect-ns"
namespace_name = aws_service_discovery_private_dns_namespace.this.id
}
}
}
}

# Define the virtual service
resource "aws_appmesh_virtual_service" "this" {
for_each = aws_appmesh_virtual_node.this
name = each.key
mesh_name = aws_appmesh_mesh.dibbs_aws_ecs_mesh.name
mesh_name = aws_appmesh_mesh.this.name

spec {
provider {
Expand Down
1 change: 0 additions & 1 deletion terraform/modules/s3/iam.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# s3 bucket role
resource "aws_iam_role" "s3_role_for_ecr_viewer" {
name = var.s3_viewer_bucket_role_name
assume_role_policy = var.ecs_assume_role_policy
Expand Down

0 comments on commit ba79925

Please sign in to comment.