@@ -5,41 +5,111 @@ provider "aws" {
55}
66
77locals {
8- db_ip = " 10.0.1.10"
9- region = var. aws_region
10-
8+ db_ip = " 10.0.1.10"
9+ region = var. aws_region
10+ name = " ${ var . event_name } -osc-workshop-linux"
11+ my_cidr = " ${ chomp (data. http . my_ip . response_body )} /32"
1112 tags = {
12-
13+ event_name = var.event_name
1314 }
1415}
1516
1617module "vpc" {
1718 source = " terraform-aws-modules/vpc/aws"
1819
19- name = " osc-workshop-linux "
20+ name = local . name
2021 cidr = " 10.0.0.0/16"
2122
22- azs = [" ${ local . region } a " ]
23+ azs = [data . aws_availability_zones . available . names [ 0 ] ]
2324 public_subnets = [" 10.0.1.0/24" ]
2425
2526 enable_nat_gateway = false
2627
28+ manage_default_network_acl = false
29+ manage_default_route_table = false
30+ manage_default_security_group = false
31+ manage_default_vpc = false
32+
33+ tags = local. tags
34+ }
35+
36+ module "security_group" {
37+ source = " terraform-aws-modules/security-group/aws"
38+ version = " ~> 4.0"
39+
40+ name = local. name
41+ description = " Security group for local IP ssh"
42+ vpc_id = module. vpc . vpc_id
43+
44+ ingress_cidr_blocks = [local . my_cidr ]
45+ ingress_rules = [" ssh-tcp" , " all-icmp" ]
46+ egress_rules = [" all-all" ]
47+
48+ ingress_with_cidr_blocks = concat (
49+ [
50+ {
51+ from_port = 8080
52+ to_port = 8080
53+ protocol = " tcp"
54+ description = " Score server dashboard"
55+ cidr_blocks = local.my_cidr
56+ },
57+ {
58+ from_port = 5432
59+ to_port = 5432
60+ protocol = " tcp"
61+ description = " Score DB for team servers"
62+ cidr_blocks = module.vpc.vpc_cidr_block
63+ },
64+ {
65+ from_port = 5432
66+ to_port = 5432
67+ protocol = " tcp"
68+ description = " Score DB for deployer"
69+ cidr_blocks = local.my_cidr
70+ }
71+ ],
72+ var. custom_security_group_ingress
73+ )
74+
2775 tags = local. tags
2876}
2977
3078module "db" {
3179 source = " terraform-aws-modules/ec2-instance/aws"
32- version = " ~> 3.0"
80+ version = " ~> 4.0"
81+
82+ name = " ${ local . name } -db"
83+
84+ ami = data. aws_ami . latest . id
85+ instance_type = " t2.micro"
86+ key_name = aws_key_pair. main . key_name
87+ vpc_security_group_ids = [module . security_group . security_group_id ]
88+ subnet_id = module. vpc . public_subnets [0 ]
89+ associate_public_ip_address = true
3390
34- subnet_id = module. vpc . public_subnets [0 ]. subnet_id
35- private_ip = local. db_ip
91+ tags = local. tags
3692}
3793
38- module "team_server " {
94+ module "team_servers " {
3995 source = " terraform-aws-modules/ec2-instance/aws"
40- version = " ~> 3.0"
96+ version = " ~> 4.0"
97+
98+ count = var. num_teams
4199
42- for_each = toset ([" 1" , " 2" ])
100+ name = " ${ local . name } -team-${ count . index + 1 } "
101+
102+ ami = data. aws_ami . latest . id
103+ instance_type = " t2.micro"
104+ key_name = aws_key_pair. main . key_name
105+ vpc_security_group_ids = [module . security_group . security_group_id ]
106+ subnet_id = module. vpc . public_subnets [0 ]
107+ associate_public_ip_address = true
108+
109+ tags = local. tags
110+ }
43111
44- subnet_id = module. vpc . public_subnets [0 ]. subnet_id
112+ resource "aws_key_pair" "main" {
113+ key_name = local. name
114+ public_key = file (pathexpand (var. ssh_local_key_path ))
45115}
0 commit comments