-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ad490c3
commit 2c18d7e
Showing
17 changed files
with
748 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# alb.tf | Load Balancer Configuration | ||
|
||
resource "aws_alb" "application_load_balancer" { | ||
name = "${var.app_name}-${var.app_environment}-alb" | ||
internal = false | ||
load_balancer_type = "application" | ||
subnets = aws_subnet.public.*.id | ||
security_groups = [aws_security_group.load_balancer_security_group.id] | ||
|
||
tags = { | ||
Name = "${var.app_name}-alb" | ||
Environment = var.app_environment | ||
} | ||
} | ||
|
||
data "http" "myip" { | ||
url = "http://ipv4.icanhazip.com" | ||
} | ||
|
||
resource "aws_security_group" "load_balancer_security_group" { | ||
vpc_id = aws_vpc.aws-vpc.id | ||
|
||
ingress { | ||
from_port = 443 | ||
to_port = 443 | ||
protocol = "tcp" | ||
cidr_blocks = ["${chomp(data.http.myip.response_body)}/32"] | ||
} | ||
|
||
ingress { | ||
from_port = 80 | ||
to_port = 80 | ||
protocol = "tcp" | ||
cidr_blocks = ["${chomp(data.http.myip.response_body)}/32"] | ||
} | ||
|
||
egress { | ||
from_port = 0 | ||
to_port = 0 | ||
protocol = "-1" | ||
cidr_blocks = ["0.0.0.0/0"] | ||
ipv6_cidr_blocks = ["::/0"] | ||
} | ||
tags = { | ||
Name = "${var.app_name}-sg" | ||
Environment = var.app_environment | ||
} | ||
} | ||
|
||
resource "aws_lb_target_group" "target_group" { | ||
name = "${var.app_name}-${var.app_environment}-tg" | ||
port = 6789 | ||
protocol = "HTTP" | ||
target_type = "ip" | ||
vpc_id = aws_vpc.aws-vpc.id | ||
|
||
health_check { | ||
healthy_threshold = "3" | ||
interval = "30" | ||
protocol = "HTTP" | ||
matcher = "200" | ||
timeout = "5" | ||
path = "/api/status" | ||
unhealthy_threshold = "2" | ||
} | ||
|
||
tags = { | ||
Name = "${var.app_name}-lb-tg" | ||
Environment = var.app_environment | ||
} | ||
} | ||
|
||
resource "aws_lb_listener" "listener" { | ||
load_balancer_arn = aws_alb.application_load_balancer.id | ||
port = "80" | ||
protocol = "HTTP" | ||
|
||
default_action { | ||
type = "forward" | ||
target_group_arn = aws_lb_target_group.target_group.id | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# db.tf | Database Configuration | ||
|
||
resource "aws_db_subnet_group" "rds_subnet_group" { | ||
name = "${var.app_name}-${var.app_environment}-rds-subnet-group" | ||
description = "${var.app_name} RDS subnet group" | ||
subnet_ids = aws_subnet.public.*.id | ||
tags = { | ||
Environment = var.app_environment | ||
} | ||
} | ||
|
||
|
||
resource "aws_security_group" "rds_sg" { | ||
name = "${var.app_name}-${var.app_environment}-rds-sg" | ||
description = "${var.app_name} RDS Security Group" | ||
vpc_id = aws_vpc.aws-vpc.id | ||
|
||
tags = { | ||
Name = "${var.app_name}-${var.app_environment}-rds-sg" | ||
Environment = var.app_environment | ||
} | ||
|
||
// allows traffic from the SG itself | ||
ingress { | ||
from_port = 0 | ||
to_port = 0 | ||
protocol = "-1" | ||
self = true | ||
} | ||
|
||
//allow traffic for TCP 5432 | ||
ingress { | ||
from_port = 5432 | ||
to_port = 5432 | ||
protocol = "tcp" | ||
security_groups = ["${aws_security_group.service_security_group.id}"] | ||
} | ||
|
||
// outbound internet access | ||
egress { | ||
from_port = 0 | ||
to_port = 0 | ||
protocol = "-1" | ||
cidr_blocks = ["0.0.0.0/0"] | ||
} | ||
} | ||
|
||
resource "aws_db_instance" "rds" { | ||
identifier = "${var.app_name}-${var.app_environment}-db" | ||
allocated_storage = 20 | ||
engine = "postgres" | ||
engine_version = "16.3" | ||
instance_class = "db.t3.micro" | ||
multi_az = false | ||
db_name = "mage" | ||
username = var.database_user // export TF_VAR_database_username="..." | ||
password = var.database_password // export TF_VAR_database_password="..." | ||
db_subnet_group_name = aws_db_subnet_group.rds_subnet_group.id | ||
vpc_security_group_ids = ["${aws_security_group.rds_sg.id}"] | ||
skip_final_snapshot = true | ||
publicly_accessible = true | ||
|
||
tags = { | ||
Environment = var.app_environment | ||
} | ||
} | ||
|
||
# Extra resources specific to this project. | ||
|
||
resource "null_resource" "db_setup" { | ||
depends_on = [aws_db_instance.rds] | ||
|
||
provisioner "local-exec" { | ||
command = <<EOT | ||
PGPASSWORD="${var.database_password}" psql -U ${var.database_user} -h ${aws_db_instance.rds.address} -d ${aws_db_instance.rds.db_name} -c "CREATE DATABASE ${var.experiments_database_name};" | ||
EOT | ||
} | ||
|
||
triggers = { | ||
# Forces this resource to be recreated on any change of the RDS instance. | ||
always_run = "${timestamp()}" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# efs.tf | Elastic File System Configuration | ||
|
||
resource "aws_efs_file_system" "file_system" { | ||
encrypted = true | ||
performance_mode = "generalPurpose" | ||
throughput_mode = "elastic" | ||
|
||
tags = { | ||
Name = "${var.app_name}-efs" | ||
Environment = var.app_environment | ||
} | ||
} | ||
|
||
resource "aws_efs_mount_target" "mount_target" { | ||
count = length(aws_subnet.public) | ||
file_system_id = aws_efs_file_system.file_system.id | ||
subnet_id = aws_subnet.public[count.index].id | ||
security_groups = [ aws_security_group.mount_target_security_group.id ] | ||
} | ||
|
||
|
||
resource "aws_security_group" "mount_target_security_group" { | ||
vpc_id = aws_vpc.aws-vpc.id | ||
|
||
ingress { | ||
from_port = 2049 | ||
to_port = 2049 | ||
protocol = "tcp" | ||
security_groups = [aws_security_group.service_security_group.id] | ||
} | ||
|
||
tags = { | ||
Name = "${var.app_name}-efs-sg" | ||
Environment = var.app_environment | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
[ | ||
{ | ||
"name": "ENV", | ||
"value": "production" | ||
}, | ||
{ | ||
"name": "AWS_ACCESS_KEY_ID", | ||
"value": "${aws_access_key_id}" | ||
}, | ||
{ | ||
"name": "AWS_SECRET_ACCESS_KEY", | ||
"value": "${aws_secret_access_key}" | ||
}, | ||
{ | ||
"name": "AWS_REGION_NAME", | ||
"value": "${aws_region_name}" | ||
}, | ||
{ | ||
"name": "MAGE_EC2_SUBNET_ID", | ||
"value": "${ec2_subnet_id}" | ||
}, | ||
{ | ||
"EXPERIMENTS_TRACKING_URI": "${experiments_tracking_uri}" | ||
}, | ||
{ | ||
"SMTP_EMAIL": "${smtp_email}" | ||
}, | ||
{ | ||
"SMTP_PASSWORD": "${smtp_password}" | ||
}, | ||
{ | ||
"MAGE_PRESENTERS_DIRECTORY": "mlops/presenters" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# iam.tf | IAM Role Policies | ||
|
||
resource "aws_iam_role" "ecsTaskExecutionRole" { | ||
name = "${var.app_name}-execution-task-role" | ||
assume_role_policy = data.aws_iam_policy_document.assume_role_policy.json | ||
tags = { | ||
Name = "${var.app_name}-iam-role" | ||
Environment = var.app_environment | ||
} | ||
} | ||
|
||
data "aws_iam_policy_document" "assume_role_policy" { | ||
statement { | ||
actions = ["sts:AssumeRole"] | ||
|
||
principals { | ||
type = "Service" | ||
identifiers = ["ecs-tasks.amazonaws.com"] | ||
} | ||
} | ||
} | ||
|
||
resource "aws_iam_role_policy_attachment" "ecsTaskExecutionRole_policy" { | ||
role = aws_iam_role.ecsTaskExecutionRole.name | ||
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role" | ||
} | ||
|
||
|
||
# resource "aws_iam_role" "lambda_role" { | ||
# name = "${var.app_name}-lambda-role" | ||
# assume_role_policy = <<EOF | ||
# { | ||
# "Version": "2012-10-17", | ||
# "Statement": [ | ||
# { | ||
# "Action": "sts:AssumeRole", | ||
# "Principal": { | ||
# "Service": "lambda.amazonaws.com" | ||
# }, | ||
# "Effect": "Allow", | ||
# "Sid": "" | ||
# } | ||
# ] | ||
# } | ||
# EOF | ||
# } | ||
|
||
# resource "aws_iam_policy" "iam_policy_for_lambda" { | ||
# name = "${var.app_name}_policy_for_lambda_role" | ||
# path = "/" | ||
# description = "IAM Policy for managing ${var.app_name} lambda role" | ||
# policy = <<EOF | ||
# { | ||
# "Version": "2012-10-17", | ||
# "Statement": [ | ||
# { | ||
# "Action": [ | ||
# "logs:CreateLogGroup", | ||
# "logs:CreateLogStream", | ||
# "logs:PutLogEvents" | ||
# ], | ||
# "Resource": "arn:aws:logs:*:*:*", | ||
# "Effect": "Allow" | ||
# } | ||
# ] | ||
# } | ||
# EOF | ||
# } | ||
|
||
# resource "aws_iam_role_policy_attachment" "attach_iam_policy_to_lambda_role" { | ||
# role = aws_iam_role.lambda_role.name | ||
# policy_arn = aws_iam_policy.iam_policy_for_lambda.arn | ||
# } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# # lambda.tf | Lambda Function Configuration | ||
|
||
# data "archive_file" "zip_the_python_code" { | ||
# type = "zip" | ||
# source_dir = "${path.module}/python/" | ||
# output_path = "${path.module}/python/event_handler.zip" | ||
# } | ||
|
||
# resource "aws_lambda_function" "terraform_lambda_func" { | ||
# filename = "${path.module}/python/event_handler.zip" | ||
# function_name = "${var.app_name}-events" | ||
# role = aws_iam_role.lambda_role.arn | ||
# handler = "event_handler.lambda_handler" | ||
# runtime = "python3.8" | ||
# depends_on = [aws_iam_role_policy_attachment.attach_iam_policy_to_lambda_role, | ||
# aws_alb.application_load_balancer] | ||
|
||
# environment { | ||
# variables = { | ||
# MAGE_API_HOST = aws_alb.application_load_balancer.dns_name, | ||
# } | ||
# } | ||
# } |
Oops, something went wrong.