Skip to content

Commit 2ed3f84

Browse files
committed
ADD: USER DATA script
1 parent 887d3db commit 2ed3f84

File tree

1 file changed

+38
-71
lines changed

1 file changed

+38
-71
lines changed

terraform-ss-proxy/main.tf

+38-71
Original file line numberDiff line numberDiff line change
@@ -19,73 +19,15 @@ data "aws_ssm_parameter" "private_subnet_id" {
1919
name = "/unity/shared-services/network/privatesubnet2"
2020
}
2121

22-
# Create ALB security group
23-
# TODO: REMOVE this ALB securty group.
24-
# Use ucs-httpd-alb-sec-group
25-
resource "aws_security_group" "alb_sg" {
26-
#TODO: change this to the actual name
27-
name = "ucs-httpd-alb-sec-group2"
28-
description = "Security group for shared services ALB"
29-
vpc_id = data.aws_ssm_parameter.vpc_id.value
30-
31-
# Incoming rules for ALB
32-
ingress {
33-
from_port = 80
34-
to_port = 80
35-
protocol = "tcp"
36-
cidr_blocks = ["0.0.0.0/0"]
37-
description = "Allow HTTP from anywhere"
38-
}
39-
40-
ingress {
41-
from_port = 443
42-
to_port = 443
43-
protocol = "tcp"
44-
cidr_blocks = ["0.0.0.0/0"]
45-
description = "Allow HTTPS from anywhere"
46-
}
47-
48-
# Outgoing rule - allow all traffic
49-
egress {
50-
from_port = 0
51-
to_port = 0
52-
protocol = "-1"
53-
cidr_blocks = ["0.0.0.0/0"]
54-
}
55-
56-
tags = {
57-
#TODO: change this to the actual name
58-
Name = "ucs-httpd-alb-sec-group2"
59-
}
22+
# Get existing security group
23+
data "aws_security_group" "httpd_sg" {
24+
name = "shared-services-httpd-sg"
25+
vpc_id = data.aws_ssm_parameter.vpc_id.value
6026
}
6127

62-
# Create security group for HTTPD instance
63-
resource "aws_security_group" "httpd_sg" {
64-
#TODO: change this to the actual name
65-
name = "shared-services-httpd-sg2"
66-
description = "Security group for shared services HTTPD"
67-
vpc_id = data.aws_ssm_parameter.vpc_id.value
68-
69-
# Incoming rule for HTTPS
70-
ingress {
71-
from_port = 443
72-
to_port = 443
73-
protocol = "tcp"
74-
security_groups = [aws_security_group.alb_sg.id]
75-
}
76-
77-
# Outgoing rule - allow all traffic
78-
egress {
79-
from_port = 0
80-
to_port = 0
81-
protocol = "-1"
82-
cidr_blocks = ["0.0.0.0/0"]
83-
}
84-
85-
tags = {
86-
#TODO: change this to the actual name
87-
Name = "shared-services-httpd-sg2"
88-
}
28+
# Get venue from SSM Parameter Store
29+
data "aws_ssm_parameter" "venue" {
30+
name = "/unity/account/venue"
8931
}
9032

9133
# Create EC2 instance
@@ -94,8 +36,8 @@ resource "aws_instance" "httpd_instance" {
9436
instance_type = "t3.large"
9537

9638
subnet_id = data.aws_ssm_parameter.private_subnet_id.value
97-
vpc_security_group_ids = [aws_security_group.httpd_sg.id]
98-
iam_instance_profile = "MCP-SSM-CloudWatch"
39+
vpc_security_group_ids = [data.aws_security_group.httpd_sg.id]
40+
iam_instance_profile = "U-CS_Service_Role"
9941
associate_public_ip_address = false
10042

10143
user_data = <<-EOF
@@ -104,14 +46,18 @@ resource "aws_instance" "httpd_instance" {
10446
sudo su - ubuntu << 'USERDATA'
10547
echo "Starting Apache installation and configuration..."
10648
49+
# Clone unity-cs-infra repository
50+
echo "Cloning unity-cs-infra repository..."
51+
cd /home/ubuntu
52+
git clone https://github.com/unity-sds/unity-cs-infra.git
53+
10754
# Update package lists
10855
echo "Updating package lists..."
10956
sudo apt update
11057
111-
# Install Apache2 and OpenIDC module
112-
echo "Installing Apache2 and OpenIDC module..."
113-
sudo DEBIAN_FRONTEND=noninteractive apt install -y apache2
114-
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y libapache2-mod-auth-openidc
58+
# Install Apache2, OpenIDC module, and AWS CLI
59+
echo "Installing Apache2, OpenIDC module, and AWS CLI..."
60+
sudo DEBIAN_FRONTEND=noninteractive apt install -y apache2 libapache2-mod-auth-openidc awscli
11561
11662
# Enable Apache modules
11763
echo "Enabling Apache modules..."
@@ -136,6 +82,27 @@ resource "aws_instance" "httpd_instance" {
13682
echo "Restarting Apache..."
13783
sudo systemctl restart apache2
13884
85+
# Create and set permissions for sync log file
86+
echo "Setting up sync log file..."
87+
sudo touch /var/log/sync_apache_config.log
88+
sudo chown ubuntu:ubuntu /var/log/sync_apache_config.log
89+
sudo chmod 644 /var/log/sync_apache_config.log
90+
91+
# Set up cron job with PATH
92+
echo "Setting up cron job..."
93+
SYSTEM_PATH=$(echo $PATH)
94+
(crontab -l 2>/dev/null; echo "PATH=$SYSTEM_PATH") | crontab -
95+
(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+
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/
101+
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
105+
139106
echo "Installation and configuration complete!"
140107
USERDATA
141108
EOF

0 commit comments

Comments
 (0)