User data works on individual EC2 instance but not in Auto Scaling Group

36 Views Asked by At

I have this user data on both an individual ec2 instance as well as the user data of a launch template but only the individual ec2 loads the website. This is done with http and both the ec2 and launch template have the same configuration. What the user data is, is essentially just a sample web server that shows which ec2 instance is being connected to.

#!/bin/bash


IP=$(curl -s http://169.254.169.254/latest/meta-data/public-ipv4)


#install apache
sudo yum -y install httpd
sudo yum install mod_ssl
sudo vi /etc/httpd/conf.d/ssl.conf
#enable and start apache
sudo systemctl enable httpd
sudo systemctl start httpd
sudo sed -i '/^#ServerName www.example.com:443/s/^#//' /etc/httpd/conf/httpd.conf

cat <<'EOF' > /var/www/html/index.html
<!DOCTYPE html>

<html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />

        <title>A Basic HTML5 Template</title>

        <link rel="preconnect" href="https://fonts.googleapis.com" />
        <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
        <link
            href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;700;800&display=swap"
            rel="stylesheet"
        />

        <link rel="stylesheet" href="css/styles.css?v=1.0" />
    </head>

    <body>
        <div class="wrapper">
            <div class="container">
                <h1>Welcome! An Apache web server has been started successfully.</h1>
                <p>Replace this with your own index.html file in /var/www/html.</p>
            </div>
        </div>
    </body>
</html>

<style>
    body {
        background-color: #34333d;
        display: flex;
        align-items: center;
        justify-content: center;
        font-family: Inter;
        padding-top: 128px;
    }

    .container {
        box-sizing: border-box;
        width: 741px;
        height: 449px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: flex-start;
        padding: 48px 48px 48px 48px;
        box-shadow: 0px 1px 32px 11px rgba(38, 37, 44, 0.49);
        background-color: #5d5b6b;
        overflow: hidden;
        align-content: flex-start;
        flex-wrap: nowrap;
        gap: 24;
        border-radius: 24px;
    }

    .container h1 {
        flex-shrink: 0;
        width: 100%;
        height: auto; /* 144px */
        position: relative;
        color: #ffffff;
        line-height: 1.2;
        font-size: 40px;
    }
    .container p {
        position: relative;
        color: #ffffff;
        line-height: 1.2;
        font-size: 18px;
    }
</style>
EOF

I tried changing the auto scaling group settings to enable anything involving meta data but it still doesn't work. Running the command, it seems like the issue is that the file at /var/www/html/index.html isn't being installed for any ec2 instance in the asg

0

There are 0 best solutions below