diff --git a/app/app/settings.py b/app/app/settings.py index 1f6c3ab..67e758d 100644 --- a/app/app/settings.py +++ b/app/app/settings.py @@ -12,6 +12,7 @@ import os from pathlib import Path +from socket import gethostname, gethostbyname # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent @@ -33,6 +34,8 @@ os.environ.get('ALLOWED_HOSTS', '').split(','), ) ) +if os.environ.get('AWS_EXECUTION_ENV'): + ALLOWED_HOSTS.append(gethostbyname(gethostname())) # Application definition diff --git a/infra/deploy/ecs.tf b/infra/deploy/ecs.tf index ef2febb..5c38cf1 100644 --- a/infra/deploy/ecs.tf +++ b/infra/deploy/ecs.tf @@ -173,10 +173,12 @@ resource "aws_security_group" "ecs_service" { } ingress { - from_port = 8000 - to_port = 8000 - protocol = "tcp" - cidr_blocks = ["0.0.0.0/0"] + from_port = 8000 + to_port = 8000 + protocol = "tcp" + security_groups = [ + aws_security_group.lb.id + ] } } @@ -194,13 +196,18 @@ resource "aws_ecs_service" "api" { enable_execute_command = true network_configuration { - assign_public_ip = true subnets = [ - aws_subnet.public_a.id, - aws_subnet.public_b.id + aws_subnet.private_a.id, + aws_subnet.private_b.id ] security_groups = [aws_security_group.ecs_service.id] } + + load_balancer { + target_group_arn = aws_lb_target_group.api.arn + container_name = "proxy" + container_port = 8000 + } } \ No newline at end of file diff --git a/infra/deploy/load_balancer.tf b/infra/deploy/load_balancer.tf new file mode 100644 index 0000000..2a6e0eb --- /dev/null +++ b/infra/deploy/load_balancer.tf @@ -0,0 +1,60 @@ +################# +# Load Balancer # +################# + +resource "aws_security_group" "lb" { + description = "Configure access for the application Load Balancer" + name = "${local.prefix}-alb-access" + vpc_id = aws_vpc.main.id + + ingress { + protocol = "tcp" + from_port = 80 + to_port = 80 + cidr_blocks = ["0.0.0.0/0"] + } + + ingress { + protocol = "tcp" + from_port = 443 + to_port = 443 + cidr_blocks = ["0.0.0.0/0"] + } + + egress { + protocol = "tcp" + from_port = 8000 + to_port = 8000 + cidr_blocks = ["0.0.0.0/0"] + } +} + +resource "aws_lb" "api" { + name = "${local.prefix}-lb" + load_balancer_type = "application" + subnets = [aws_subnet.public_a.id, aws_subnet.public_b.id] + security_groups = [aws_security_group.lb.id] +} + +resource "aws_lb_target_group" "api" { + name = "${local.prefix}-api" + protocol = "HTTP" + vpc_id = aws_vpc.main.id + target_type = "ip" + port = 8000 + + health_check { + path = "/api/health-check" + } +} + +resource "aws_lb_listener" "api" { + load_balancer_arn = aws_lb.api.arn + port = 80 + protocol = "HTTP" + + default_action { + type = "forward" + target_group_arn = aws_lb_target_group.api.arn + } +} \ No newline at end of file diff --git a/infra/setup/iam.tf b/infra/setup/iam.tf index 9c1fa12..050023b 100644 --- a/infra/setup/iam.tf +++ b/infra/setup/iam.tf @@ -291,4 +291,45 @@ resource "aws_iam_policy" "logs" { resource "aws_iam_user_policy_attachment" "logs" { user = aws_iam_user.cd.name policy_arn = aws_iam_policy.logs.arn +} + +######################### +# Policy for ELB access # +######################### + +data "aws_iam_policy_document" "elb" { + statement { + effect = "Allow" + actions = [ + "elasticloadbalancing:DeleteLoadBalancer", + "elasticloadbalancing:DeleteTargetGroup", + "elasticloadbalancing:DeleteListener", + "elasticloadbalancing:DescribeListeners", + "elasticloadbalancing:DescribeLoadBalancerAttributes", + "elasticloadbalancing:DescribeTargetGroups", + "elasticloadbalancing:DescribeTargetGroupAttributes", + "elasticloadbalancing:DescribeLoadBalancers", + "elasticloadbalancing:CreateListener", + "elasticloadbalancing:SetSecurityGroups", + "elasticloadbalancing:ModifyLoadBalancerAttributes", + "elasticloadbalancing:CreateLoadBalancer", + "elasticloadbalancing:ModifyTargetGroupAttributes", + "elasticloadbalancing:CreateTargetGroup", + "elasticloadbalancing:AddTags", + "elasticloadbalancing:DescribeTags", + "elasticloadbalancing:ModifyListener" + ] + resources = ["*"] + } +} + +resource "aws_iam_policy" "elb" { + name = "${aws_iam_user.cd.name}-elb" + description = "Allow user to manage ELB resources." + policy = data.aws_iam_policy_document.elb.json +} + +resource "aws_iam_user_policy_attachment" "elb" { + user = aws_iam_user.cd.name + policy_arn = aws_iam_policy.elb.arn } \ No newline at end of file