Skip to content

Commit ee905bb

Browse files
committed
ADD: EC2 to TG
1 parent 2ed3f84 commit ee905bb

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

terraform-ss-proxy/main.tf

+33-11
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ data "aws_ssm_parameter" "venue" {
3030
name = "/unity/account/venue"
3131
}
3232

33+
# Get existing target group
34+
data "aws_lb_target_group" "httpd_tg" {
35+
name = "ucs-httpd-tg"
36+
}
37+
3338
# Create EC2 instance
3439
resource "aws_instance" "httpd_instance" {
3540
ami = data.aws_ssm_parameter.ubuntu_ami.value
@@ -71,16 +76,29 @@ resource "aws_instance" "httpd_instance" {
7176
sudo a2enmod rewrite
7277
sudo a2enmod auth_openidc
7378
79+
# Remove default Apache site
80+
sudo a2dissite 000-default.conf
81+
sudo rm -f /etc/apache2/sites-enabled/000-default.conf
82+
7483
# Generate self-signed SSL certificate
7584
echo "Generating self-signed SSL certificate..."
7685
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
7786
-keyout /etc/ssl/private/apache-selfsigned.key \
7887
-out /etc/ssl/certs/apache-selfsigned.crt \
7988
-subj "/C=US/ST=CA/L=LA/O=Unity/OU=CS/CN=shared-services-httpd-unity-test/[email protected]"
8089
81-
# Restart Apache to apply changes
82-
echo "Restarting Apache..."
83-
sudo systemctl restart apache2
90+
# Set proper permissions for SSL files
91+
sudo chmod 600 /etc/ssl/private/apache-selfsigned.key
92+
sudo chmod 644 /etc/ssl/certs/apache-selfsigned.crt
93+
94+
# Get venue from SSM and download Apache config
95+
echo "Downloading Apache configuration..."
96+
VENUE=$(aws ssm get-parameter --name "/unity/account/venue" --query "Parameter.Value" --output text)
97+
sudo aws s3 cp "s3://ucs-shared-services-apache-config-$VENUE/unity-cs.conf" /etc/apache2/sites-enabled/
98+
99+
# Set proper permissions
100+
sudo chown root:root /etc/apache2/sites-enabled/unity-cs.conf
101+
sudo chmod 644 /etc/apache2/sites-enabled/unity-cs.conf
84102
85103
# Create and set permissions for sync log file
86104
echo "Setting up sync log file..."
@@ -94,21 +112,25 @@ resource "aws_instance" "httpd_instance" {
94112
(crontab -l 2>/dev/null; echo "PATH=$SYSTEM_PATH") | crontab -
95113
(crontab -l 2>/dev/null; echo "* * * * * ~/unity-cs-infra/terraform-ss-proxy/sync_apache_config.sh >> /var/log/sync_apache_config.log 2>&1") | crontab -
96114
97-
# Get venue from SSM and download Apache config
98-
echo "Downloading Apache configuration..."
99-
VENUE=$(aws ssm get-parameter --name "/unity/account/venue" --query "Parameter.Value" --output text)
100-
sudo aws s3 cp "s3://ucs-shared-services-apache-config-$VENUE/unity-cs.conf" /etc/apache2/sites-enabled/
115+
# Final Apache restart after all configurations are in place
116+
echo "Final Apache restart..."
117+
sudo systemctl restart apache2
101118
102-
# Set proper permissions
103-
sudo chown root:root /etc/apache2/sites-enabled/unity-cs.conf
104-
sudo chmod 644 /etc/apache2/sites-enabled/unity-cs.conf
119+
# Verify Apache is running
120+
sudo systemctl status apache2
105121
106122
echo "Installation and configuration complete!"
107123
USERDATA
108124
EOF
109125

110126
tags = {
111-
#TODO: change this to the actual name
112127
Name = "shared-services-httpd2"
113128
}
114129
}
130+
131+
# Attach the EC2 instance to the target group
132+
resource "aws_lb_target_group_attachment" "httpd_tg_attachment" {
133+
target_group_arn = data.aws_lb_target_group.httpd_tg.arn
134+
target_id = aws_instance.httpd_instance.id
135+
port = 443
136+
}

0 commit comments

Comments
 (0)