-
Notifications
You must be signed in to change notification settings - Fork 0
/
ec2.tf
105 lines (90 loc) · 4.07 KB
/
ec2.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# Create an EC2 Instance in the Public Subnet 1
resource "aws_instance" "terraform_orchestration_react_client_instance" {
ami = "ami-0182f373e66f89c85" # Amazon Linux
instance_type = "t2.micro"
subnet_id = aws_subnet.terraform_orchestration_public_subnet_1.id
key_name = aws_key_pair.terraform_ssh_key.key_name
associate_public_ip_address = true # Assigns a public IP for SSH and HTTP/HTTPS access
# Use the Security Group
vpc_security_group_ids = [aws_security_group.terraform_orchestration_ec2_public_sg.id]
# Trivy - require IMDS access to require a token - https://avd.aquasec.com/misconfig/aws/ec2/avd-aws-0028/
metadata_options {
http_tokens = "required"
}
tags = {
Name = "terraform-orchestration-react-client-ec2-1"
}
# User data to initialize the React app on instance launch
user_data = <<-EOF
#!/bin/bash
# Install Node.js and serve React app
sudo apt update -y
sudo apt install -y nodejs npm
# Example: Clone and run your React app
# git clone https://github.com/samuelberston/todo-app.git
# cd todo-app
# npm install
# npm start
EOF
}
# Create an EC2 Instance in the Public Subnet 2
resource "aws_instance" "terraform_orchestration_react_client_instance_2" {
ami = "ami-0182f373e66f89c85" # Amazon Linux
instance_type = "t2.micro"
subnet_id = aws_subnet.terraform_orchestration_public_subnet_2.id
key_name = aws_key_pair.terraform_ssh_key.key_name
associate_public_ip_address = true # Assigns a public IP for SSH and HTTP/HTTPS access
# Use the Security Group
vpc_security_group_ids = [aws_security_group.terraform_orchestration_ec2_public_sg.id]
# Trivy - require IMDS access to require a token - https://avd.aquasec.com/misconfig/aws/ec2/avd-aws-0028/
metadata_options {
http_tokens = "required"
}
tags = {
Name = "terraform-orchestration-react-client-ec2-2"
}
# User data to initialize the React app on instance launch
user_data = <<-EOF
#!/bin/bash
# Install Node.js and serve React app
sudo apt update -y
sudo apt install -y nodejs npm
# Example: Clone and run your React app
# git clone https://github.com/samuelberston/todo-app.git
# cd todo-app
# npm install
# npm start
EOF
}
# EC2 instance in Private Subnet 1 (for backend)
resource "aws_instance" "private_ec2_instance_1" {
ami = "ami-0182f373e66f89c85" # Amazon Linux
instance_type = "t2.micro"
iam_instance_profile = aws_iam_instance_profile.ec2_secretsmanager_instance_profile.name
subnet_id = aws_subnet.terraform_orchestration_private_subnet_1.id
key_name = aws_key_pair.terraform_ssh_key.key_name
vpc_security_group_ids = [aws_security_group.backend_ec2_sg.id] # Backend EC2 security group
# Trivy - require IMDS access to require a token - https://avd.aquasec.com/misconfig/aws/ec2/avd-aws-0028/
metadata_options {
http_tokens = "required"
}
tags = {
Name = "terraform-orchestration-nodejs-server-ec2-1"
}
}
# EC2 instance in Private Subnet 2 (for backend)
resource "aws_instance" "private_ec2_instance_2" {
ami = "ami-0182f373e66f89c85" # Amazon Linux
instance_type = "t2.micro"
iam_instance_profile = aws_iam_instance_profile.ec2_secretsmanager_instance_profile.name
subnet_id = aws_subnet.terraform_orchestration_private_subnet_2.id
key_name = aws_key_pair.terraform_ssh_key.key_name
vpc_security_group_ids = [aws_security_group.backend_ec2_sg.id] # Backend EC2 security group
# Trivy - require IMDS access to require a token - https://avd.aquasec.com/misconfig/aws/ec2/avd-aws-0028/
metadata_options {
http_tokens = "required"
}
tags = {
Name = "terraform-orchestration-nodejs-server-ec2-2"
}
}