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

upgrade code for terraform 1.x #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions terraform/autoscaling.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ resource "aws_appautoscaling_target" "target" {
service_namespace = "ecs"
resource_id = "service/${aws_ecs_cluster.main.name}/${aws_ecs_service.service.name}"
scalable_dimension = "ecs:service:DesiredCount"
min_capacity = "${var.desired_count}"
max_capacity = "${var.max_count}"
min_capacity = var.desired_count
max_capacity = var.max_count
lifecycle {
ignore_changes = [tags_all]
}
}

# Automatically scale capacity up by one
Expand Down
34 changes: 17 additions & 17 deletions terraform/ecs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ resource "aws_ecs_cluster" "main" {

tags = merge(
var.extra_tags,
map("Name", "${var.environment}-${var.app_name}"),
{ "Name" = format("%s-%s-sg", var.environment, var.app_name) },
)
}

Expand All @@ -16,11 +16,11 @@ ECS task definitions

resource "aws_cloudwatch_log_group" "cwlog" {
name = "/ecs/${var.environment}-${var.app_name}"
retention_in_days = 30
retention_in_days = var.log_retention_in_days

tags = merge(
var.extra_tags,
map("Name", format("%s-%s", var.environment, var.app_name)),
{ "Name" = format("%s-%s-sg", var.environment, var.app_name) },
)
}

Expand Down Expand Up @@ -79,33 +79,33 @@ resource "aws_ecs_task_definition" "squid" {
EOF

requires_compatibilities = ["FARGATE"]
network_mode = "awsvpc"
cpu = "256"
memory = "512"
execution_role_arn = aws_iam_role.ecs_execution_role.arn
task_role_arn = aws_iam_role.ecs_execution_role.arn
network_mode = "awsvpc"
cpu = "256"
memory = "512"
execution_role_arn = aws_iam_role.ecs_execution_role.arn
task_role_arn = aws_iam_role.ecs_execution_role.arn
tags = merge(
var.extra_tags,
map("Name", format("%s-%s-task", var.environment, var.app_name)),
{ "Name" = format("%s-%s-sg", var.environment, var.app_name) },
)
}

resource "aws_ecs_service" "service" {
name = "${var.environment}-${var.app_name}"
cluster = aws_ecs_cluster.main.id
name = "${var.environment}-${var.app_name}"
cluster = aws_ecs_cluster.main.id
task_definition = "${aws_ecs_task_definition.squid.family}:${aws_ecs_task_definition.squid.revision}"
launch_type = "FARGATE"
desired_count = var.desired_count
launch_type = "FARGATE"
desired_count = var.desired_count

load_balancer {
target_group_arn = aws_lb_target_group.main.arn
container_name = var.app_name
container_port = var.app_port
container_name = var.app_name
container_port = var.app_port
}

network_configuration {
subnets = var.fargate_subnets
security_groups = [aws_security_group.fargate.id]
subnets = var.fargate_subnets
security_groups = [aws_security_group.fargate.id]
assign_public_ip = true
}

Expand Down
8 changes: 4 additions & 4 deletions terraform/iam.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ EOF

tags = merge(
var.extra_tags,
map("Name", format("%s-%s-fargate-role", var.environment, var.app_name)),
{ "Name" = format("%s-%s-fargate-role", var.environment, var.app_name) },
)
}

Expand All @@ -36,12 +36,12 @@ data "aws_iam_policy_document" "app_policy" {
}

resource "aws_iam_role_policy" "app_policy_pl" {
name = "app_policy"
role = aws_iam_role.ecs_execution_role.name
name = "app_policy"
role = aws_iam_role.ecs_execution_role.name
policy = data.aws_iam_policy_document.app_policy.json
}

resource "aws_iam_role_policy_attachment" "ecs_execution_policy" {
role = aws_iam_role.ecs_execution_role.name
role = aws_iam_role.ecs_execution_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
}
2 changes: 1 addition & 1 deletion terraform/main.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
terraform {
required_version = ">= 0.12.0"
required_version = ">= 1.0"
}
10 changes: 5 additions & 5 deletions terraform/networking.tf
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
resource "aws_security_group" "fargate" {
name = format("%s-%s-sg", var.environment, var.app_name)
description = format("%s-%s-sg", var.environment, var.app_name)
vpc_id = "${var.vpc_id}"
vpc_id = var.vpc_id

ingress {
from_port = var.app_port
to_port = var.app_port
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
cidr_blocks = ["10.0.0.0/8"]
}

egress {
Expand All @@ -17,8 +17,8 @@ resource "aws_security_group" "fargate" {
cidr_blocks = ["0.0.0.0/0"]
}

tags = "${merge(
tags = merge(
var.extra_tags,
map("Name", format("%s-%s-sg", var.environment, var.app_name)),
)}"
{ "Name" = format("%s-%s-sg", var.environment, var.app_name) },
)
}
12 changes: 6 additions & 6 deletions terraform/nlb.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ resource "aws_lb" "main" {
subnets = var.lb_subnets


tags = "${merge(
tags = merge(
var.extra_tags,
map("Name", format("%s-%s-nlb", var.environment, var.app_name)),
)}"
{ "Name" = format("%s-%s-fargate-role", var.environment, var.app_name) },
)
}

# adds a tcp listener to the load balancer and allows ingress
Expand Down Expand Up @@ -45,8 +45,8 @@ resource "aws_lb_target_group" "main" {
unhealthy_threshold = 2
}

tags = "${merge(
tags = merge(
var.extra_tags,
map("Name", format("%s-%s-tg", var.environment, var.app_name)),
)}"
{ "Name" = format("%s-%s-fargate-role", var.environment, var.app_name) },
)
}
4 changes: 4 additions & 0 deletions terraform/output.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ output "nlb_arn" {
output "nlb_hostname" {
value = aws_lb.main.dns_name
}

output "nlb_zone_id" {
value = aws_lb.main.zone_id
}
7 changes: 6 additions & 1 deletion terraform/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ variable "fargate_image" {

# Additional tags to apply to all tagged resources.
variable "extra_tags" {
type = "map"
type = map(any)
}

variable "internal" {
Expand Down Expand Up @@ -69,6 +69,11 @@ variable "deregistration_delay" {
type = number
}

variable "log_retention_in_days" {
default = 30
type = number
}

variable "whitelist_aws_region" {
description = "URL filter for AWS region"
default = "eu-west-1,eu-west-2,eu-central-1"
Expand Down