66 * - Internet Gateway and route tables
77 */
88
9+ # Data source for current AWS account ID
10+ data "aws_caller_identity" "current" {}
11+
912# VPC
1013resource "aws_vpc" "main" {
1114 cidr_block = var. vpc_cidr
@@ -17,6 +20,86 @@ resource "aws_vpc" "main" {
1720 }
1821}
1922
23+ # Restrict default security group (CKV2_AWS_12)
24+ resource "aws_default_security_group" "default" {
25+ vpc_id = aws_vpc. main . id
26+
27+ tags = {
28+ Name = " ${ var . project_name } -default-sg"
29+ }
30+ }
31+
32+ # VPC Flow Logs (CKV2_AWS_11)
33+ resource "aws_flow_log" "main" {
34+ iam_role_arn = aws_iam_role. flow_logs . arn
35+ log_destination = aws_cloudwatch_log_group. flow_logs . arn
36+ traffic_type = " ALL"
37+ vpc_id = aws_vpc. main . id
38+
39+ tags = {
40+ Name = " ${ var . project_name } -vpc-flow-logs"
41+ }
42+ }
43+
44+ resource "aws_cloudwatch_log_group" "flow_logs" {
45+ name = " /aws/vpc/flowlogs/${ var . project_name } "
46+ retention_in_days = 30
47+ kms_key_id = aws_kms_key. flow_logs . arn
48+
49+ tags = {
50+ Name = " ${ var . project_name } -vpc-flow-logs"
51+ }
52+ }
53+
54+ resource "aws_kms_key" "flow_logs" {
55+ description = " KMS key for VPC Flow Logs"
56+ deletion_window_in_days = 10
57+ enable_key_rotation = true
58+
59+ tags = {
60+ Name = " ${ var . project_name } -flow-logs-key"
61+ }
62+ }
63+
64+ resource "aws_iam_role" "flow_logs" {
65+ name = " ${ var . project_name } -vpc-flow-logs-role"
66+
67+ assume_role_policy = jsonencode ({
68+ Version = " 2012-10-17"
69+ Statement = [
70+ {
71+ Action = " sts:AssumeRole"
72+ Effect = " Allow"
73+ Principal = {
74+ Service = " vpc-flow-logs.amazonaws.com"
75+ }
76+ }
77+ ]
78+ })
79+ }
80+
81+ resource "aws_iam_role_policy" "flow_logs" {
82+ name = " ${ var . project_name } -vpc-flow-logs-policy"
83+ role = aws_iam_role. flow_logs . id
84+
85+ policy = jsonencode ({
86+ Version = " 2012-10-17"
87+ Statement = [
88+ {
89+ Action = [
90+ " logs:CreateLogGroup" ,
91+ " logs:CreateLogStream" ,
92+ " logs:PutLogEvents" ,
93+ " logs:DescribeLogGroups" ,
94+ " logs:DescribeLogStreams"
95+ ]
96+ Effect = " Allow"
97+ Resource = " *"
98+ }
99+ ]
100+ })
101+ }
102+
20103# Internet Gateway
21104resource "aws_internet_gateway" "main" {
22105 vpc_id = aws_vpc. main . id
@@ -54,7 +137,7 @@ resource "aws_subnet" "public" {
54137 vpc_id = aws_vpc. main . id
55138 cidr_block = var. public_subnet_cidrs [count . index ]
56139 availability_zone = var. availability_zones [count . index ]
57- map_public_ip_on_launch = true
140+ map_public_ip_on_launch = false
58141
59142 tags = {
60143 Name = " ${ var . project_name } -public-subnet-${ count . index + 1 } "
@@ -149,28 +232,39 @@ resource "aws_route_table_association" "database" {
149232# Security Group for ALB
150233resource "aws_security_group" "alb" {
151234 name_prefix = " ${ var . project_name } -alb-"
152- description = " Security group for ALB"
235+ description = " Security group for ALB - allows HTTP/HTTPS from internet "
153236 vpc_id = aws_vpc. main . id
154237
155238 ingress {
156239 from_port = 80
157240 to_port = 80
158241 protocol = " tcp"
159242 cidr_blocks = [" 0.0.0.0/0" ]
243+ description = " Allow HTTP from Internet"
160244 }
161245
162246 ingress {
163247 from_port = 443
164248 to_port = 443
165249 protocol = " tcp"
166250 cidr_blocks = [" 0.0.0.0/0" ]
251+ description = " Allow HTTPS from Internet"
252+ }
253+
254+ egress {
255+ from_port = 0
256+ to_port = 65535
257+ protocol = " tcp"
258+ cidr_blocks = [" 0.0.0.0/0" ]
259+ description = " Allow outbound TCP traffic"
167260 }
168261
169262 egress {
170263 from_port = 0
171- to_port = 0
172- protocol = " -1 "
264+ to_port = 65535
265+ protocol = " udp "
173266 cidr_blocks = [" 0.0.0.0/0" ]
267+ description = " Allow outbound UDP traffic"
174268 }
175269
176270 tags = {
@@ -181,21 +275,31 @@ resource "aws_security_group" "alb" {
181275# Security Group for ECS Tasks
182276resource "aws_security_group" "ecs_tasks" {
183277 name_prefix = " ${ var . project_name } -ecs-tasks-"
184- description = " Security group for ECS tasks"
278+ description = " Security group for ECS tasks - allows traffic from ALB "
185279 vpc_id = aws_vpc. main . id
186280
187281 ingress {
188282 from_port = var. app_port
189283 to_port = var. app_port
190284 protocol = " tcp"
191285 security_groups = [aws_security_group . alb . id ]
286+ description = " Allow app port from ALB"
287+ }
288+
289+ egress {
290+ from_port = 0
291+ to_port = 65535
292+ protocol = " tcp"
293+ cidr_blocks = [" 0.0.0.0/0" ]
294+ description = " Allow outbound TCP traffic"
192295 }
193296
194297 egress {
195298 from_port = 0
196- to_port = 0
197- protocol = " -1 "
299+ to_port = 65535
300+ protocol = " udp "
198301 cidr_blocks = [" 0.0.0.0/0" ]
302+ description = " Allow outbound UDP traffic"
199303 }
200304
201305 tags = {
0 commit comments