diff --git a/alb.tf b/alb.tf index 7153bbc..172b0af 100644 --- a/alb.tf +++ b/alb.tf @@ -1,32 +1,32 @@ resource "aws_alb_target_group" "main" { name = "tf-${var.base_name}-ecs" - port = "${var.alb_container_port}" + port = var.alb_container_port protocol = "HTTP" - vpc_id = "${aws_vpc.main.id}" + vpc_id = aws_vpc.main.id health_check { - path = "${var.health_check_path}" + path = var.health_check_path } } resource "aws_alb" "main" { name = "tf-${var.base_name}-alb-ecs" - subnets = ["${aws_subnet.main.*.id}"] - security_groups = ["${aws_security_group.lb_sg.id}"] + subnets = aws_subnet.main.*.id + security_groups = [aws_security_group.lb_sg.id] - depends_on = ["aws_internet_gateway.gw"] + depends_on = [aws_internet_gateway.gw] } resource "aws_alb_listener" "front_end" { - load_balancer_arn = "${aws_alb.main.id}" + load_balancer_arn = aws_alb.main.id port = "443" protocol = "HTTPS" - ssl_policy = "ELBSecurityPolicy-2015-05" - certificate_arn = "${aws_iam_server_certificate.elb_cert.arn}" + ssl_policy = "ELBSecurityPolicy-2015-05" + certificate_arn = aws_iam_server_certificate.elb_cert.arn default_action { - target_group_arn = "${aws_alb_target_group.main.id}" + target_group_arn = aws_alb_target_group.main.id type = "forward" } } diff --git a/cert.tf b/cert.tf index d957601..9924201 100644 --- a/cert.tf +++ b/cert.tf @@ -1,5 +1,5 @@ provider "acme" { - server_url = "${var.acme_server_url}" + server_url = var.acme_server_url } resource "tls_private_key" "private_key" { @@ -7,30 +7,30 @@ resource "tls_private_key" "private_key" { } resource "acme_registration" "reg" { - account_key_pem = "${tls_private_key.private_key.private_key_pem}" - email_address = "${var.acme_email}" + account_key_pem = tls_private_key.private_key.private_key_pem + email_address = var.acme_email } resource "acme_certificate" "certificate" { - account_key_pem = "${acme_registration.reg.account_key_pem}" + account_key_pem = acme_registration.reg.account_key_pem common_name = "api.${var.domain}" subject_alternative_names = [] dns_challenge { provider = "route53" - config { - AWS_ACCESS_KEY_ID = "${var.aws_access_key}" - AWS_SECRET_ACCESS_KEY = "${var.aws_secret_key}" - AWS_DEFAULT_REGION = "${var.aws_region}" + config = { + AWS_ACCESS_KEY_ID = var.aws_access_key + AWS_SECRET_ACCESS_KEY = var.aws_secret_key + AWS_DEFAULT_REGION = var.aws_region } } } resource "aws_iam_server_certificate" "elb_cert" { - name_prefix = "tf-${var.base_name}-cert-" - certificate_body = "${acme_certificate.certificate.certificate_pem}" - private_key = "${acme_certificate.certificate.private_key_pem}" + name_prefix = "tf-${var.base_name}-cert-" + certificate_body = acme_certificate.certificate.certificate_pem + private_key = acme_certificate.certificate.private_key_pem lifecycle { create_before_destroy = true diff --git a/compute.tf b/compute.tf index f61a588..a46e521 100644 --- a/compute.tf +++ b/compute.tf @@ -1,21 +1,21 @@ resource "aws_autoscaling_group" "app" { name = "tf-${var.base_name}-asg" - vpc_zone_identifier = ["${aws_subnet.main.*.id}"] - min_size = "${var.asg_min}" - max_size = "${var.asg_max}" - desired_capacity = "${var.asg_desired}" - launch_configuration = "${aws_launch_configuration.app.name}" + vpc_zone_identifier = aws_subnet.main.*.id + min_size = var.asg_min + max_size = var.asg_max + desired_capacity = var.asg_desired + launch_configuration = aws_launch_configuration.app.name } data "template_file" "cloud_config" { - template = "${file("${path.module}/cloud-config.yml")}" + template = file("${path.module}/cloud-config.yml") - vars { - aws_region = "${var.aws_region}" - ecs_cluster_name = "${aws_ecs_cluster.main.name}" + vars = { + aws_region = var.aws_region + ecs_cluster_name = aws_ecs_cluster.main.name ecs_log_level = "info" ecs_agent_version = "latest" - ecs_log_group_name = "${aws_cloudwatch_log_group.ecs.name}" + ecs_log_group_name = aws_cloudwatch_log_group.ecs.name } } @@ -41,19 +41,19 @@ data "aws_ami" "stable_coreos" { } resource "aws_key_pair" "instance" { - public_key = "${var.ssh_public_key}" + public_key = var.ssh_public_key } resource "aws_launch_configuration" "app" { security_groups = [ - "${aws_security_group.instance_sg.id}", + aws_security_group.instance_sg.id, ] - key_name = "${aws_key_pair.instance.key_name}" - image_id = "${data.aws_ami.stable_coreos.id}" - instance_type = "${var.instance_type}" - iam_instance_profile = "${aws_iam_instance_profile.app.name}" - user_data = "${data.template_file.cloud_config.rendered}" + key_name = aws_key_pair.instance.key_name + image_id = data.aws_ami.stable_coreos.id + instance_type = var.instance_type + iam_instance_profile = aws_iam_instance_profile.app.name + user_data = data.template_file.cloud_config.rendered associate_public_ip_address = true lifecycle { diff --git a/db.tf b/db.tf index 40baea7..9d7a81f 100644 --- a/db.tf +++ b/db.tf @@ -1,18 +1,18 @@ resource "aws_db_subnet_group" "main" { - name = "db_subnet" - subnet_ids = ["${aws_subnet.main.*.id}"] - tags { - Name = "db_subnet" + name = "db_subnet" + subnet_ids = aws_subnet.main.*.id + tags = { + Name = "db_subnet" } } resource "aws_db_parameter_group" "db_pg" { - name = "rds-pg" - family = "postgres10" + name = "rds-pg" + family = "postgres10" description = "Managed by Terraform" parameter { - name = "timezone" + name = "timezone" value = "Asia/Tokyo" } } @@ -22,15 +22,15 @@ resource "aws_db_instance" "db" { allocated_storage = 5 engine = "postgres" engine_version = "10.4" - instance_class = "${var.db_instance_type}" + instance_class = var.db_instance_type storage_type = "gp2" - username = "${var.db_username}" - password = "${var.db_password}" + username = var.db_username + password = var.db_password backup_retention_period = 7 multi_az = true - vpc_security_group_ids = ["${aws_security_group.db.id}"] - db_subnet_group_name = "${aws_db_subnet_group.main.name}" - parameter_group_name = "${aws_db_parameter_group.db_pg.name}" - skip_final_snapshot = true + vpc_security_group_ids = [aws_security_group.db.id] + db_subnet_group_name = aws_db_subnet_group.main.name + parameter_group_name = aws_db_parameter_group.db_pg.name + skip_final_snapshot = true } diff --git a/ecs.tf b/ecs.tf index b15c0da..d7263c8 100644 --- a/ecs.tf +++ b/ecs.tf @@ -3,45 +3,45 @@ resource "aws_ecs_cluster" "main" { } data "template_file" "task_definition" { - template = "${file("${path.module}/task-definition.json")}" + template = file("${path.module}/task-definition.json") - vars { - image_url = "${var.ecs_image_url}" - container_name = "sakuten_backend" - log_group_region = "${var.aws_region}" - log_group_name = "${aws_cloudwatch_log_group.app.name}" - secret_key = "${var.secret_key}" - container_port = "${var.container_port}" - host_port = "${var.alb_container_port}" - recaptcha_secret_key = "${var.recaptcha_secret_key}" - database_url = "postgresql://${var.db_username}:${var.db_password}@${aws_db_instance.db.endpoint}/postgres" - /* timepoints = "${var.timepoints}" */ - /* start_datetime = "${var.start_datetime}" */ - /* end_datetime = "${var.end_datetime}" */ + vars = { + image_url = var.ecs_image_url + container_name = "sakuten_backend" + log_group_region = var.aws_region + log_group_name = aws_cloudwatch_log_group.app.name + secret_key = var.secret_key + container_port = var.container_port + host_port = var.alb_container_port + recaptcha_secret_key = var.recaptcha_secret_key + database_url = "postgresql://${var.db_username}:${var.db_password}@${aws_db_instance.db.endpoint}/postgres" } + /* timepoints = "${var.timepoints}" */ + /* start_datetime = "${var.start_datetime}" */ + /* end_datetime = "${var.end_datetime}" */ } resource "aws_ecs_task_definition" "backend" { family = "tf_backend_td" - container_definitions = "${data.template_file.task_definition.rendered}" + container_definitions = data.template_file.task_definition.rendered } resource "aws_ecs_service" "main" { name = "tf-${var.base_name}-ecs" - cluster = "${aws_ecs_cluster.main.id}" - task_definition = "${aws_ecs_task_definition.backend.arn}" + cluster = aws_ecs_cluster.main.id + task_definition = aws_ecs_task_definition.backend.arn desired_count = 1 - iam_role = "${aws_iam_role.ecs_service.name}" + iam_role = aws_iam_role.ecs_service.name load_balancer { - target_group_arn = "${aws_alb_target_group.main.id}" + target_group_arn = aws_alb_target_group.main.id container_name = "sakuten_backend" - container_port = "${var.container_port}" + container_port = var.container_port } depends_on = [ - "aws_iam_role_policy.ecs_service", - "aws_alb_listener.front_end", + aws_iam_role_policy.ecs_service, + aws_alb_listener.front_end, ] } diff --git a/iam.tf b/iam.tf index 15d6060..06eb6e1 100644 --- a/iam.tf +++ b/iam.tf @@ -16,11 +16,12 @@ resource "aws_iam_role" "ecs_service" { ] } EOF + } resource "aws_iam_role_policy" "ecs_service" { name = "tf_ecs_policy" - role = "${aws_iam_role.ecs_service.name}" + role = aws_iam_role.ecs_service.name policy = <