@@ -30,6 +30,11 @@ data "aws_ssm_parameter" "venue" {
30
30
name = " /unity/account/venue"
31
31
}
32
32
33
+ # Get existing target group
34
+ data "aws_lb_target_group" "httpd_tg" {
35
+ name = " ucs-httpd-tg"
36
+ }
37
+
33
38
# Create EC2 instance
34
39
resource "aws_instance" "httpd_instance" {
35
40
ami = data. aws_ssm_parameter . ubuntu_ami . value
@@ -71,16 +76,29 @@ resource "aws_instance" "httpd_instance" {
71
76
sudo a2enmod rewrite
72
77
sudo a2enmod auth_openidc
73
78
79
+ # Remove default Apache site
80
+ sudo a2dissite 000-default.conf
81
+ sudo rm -f /etc/apache2/sites-enabled/000-default.conf
82
+
74
83
# Generate self-signed SSL certificate
75
84
echo "Generating self-signed SSL certificate..."
76
85
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
77
86
-keyout /etc/ssl/private/apache-selfsigned.key \
78
87
-out /etc/ssl/certs/apache-selfsigned.crt \
79
88
-subj "/C=US/ST=CA/L=LA/O=Unity/OU=CS/CN=shared-services-httpd-unity-test/[email protected] "
80
89
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
84
102
85
103
# Create and set permissions for sync log file
86
104
echo "Setting up sync log file..."
@@ -94,21 +112,25 @@ resource "aws_instance" "httpd_instance" {
94
112
(crontab -l 2>/dev/null; echo "PATH=$SYSTEM_PATH") | crontab -
95
113
(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 -
96
114
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
101
118
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
105
121
106
122
echo "Installation and configuration complete!"
107
123
USERDATA
108
124
EOF
109
125
110
126
tags = {
111
- # TODO: change this to the actual name
112
127
Name = " shared-services-httpd2"
113
128
}
114
129
}
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