Docker wp cli AWS RDS ELB EC2 Scaling group

For a project I need to do this

On an EC2 (AWS) instance install docker and in docker a wordpress site with wp-cli. I already have a database (RDS)

So it will be necessary that wordpress (on docker) this automatically connects to my database and also that this is the address of the load balancing that is taken into account because there will be several machines (3 instance) that can be created automatically according to the needs.

So it will be necessary that each machine that will automatically create (scaling group) can connect to the database (RDS) by always taking the address of the load balancing and that the site is always online; if I make a change this impacts on other machines or future machine creates

How to do this?

thank you

SOLUTION HERE :

      UserData:
        Fn::Base64: !Sub |
          #!/bin/bash
          sudo su
          apt-get update -qq
          apt update -qq
          apt-get upgrade -y
          apt upgrade -y
          apt-get install -y apt-transport-https ca-certificates curl gnupg docker docker-compose
          usermod -aG docker ubuntu
          newgrp docker
          systemctl enable --now docker
          myip=$(ip -f inet -o addr show eth0 | cut -d\  -f 7 | cut -d/ -f 1)
          echo -e $myip > /root/mesip.txt
          mkdir /var/www
          mkdir /var/www/html
          chmod -R 777 /var/www/html
          chown -R www-data:www-data /var/www/html
          mkdir /root/wordpress
          docker volume create --name wp_files --opt type=none --opt o=bind --opt device=/var/www/html
          echo -e "
          version: \"3.4\"
          services:
            wordpress:
              image: "wordpress:latest"
              container_name: wordpress
              ports:
                - 80:80
              restart: always
              environment:
                - WORDPRESS_DB_HOST=${DBserver.Endpoint.Address}:3306
                - WORDPRESS_DB_USER= YOUR USERNAME
                - WORDPRESS_DB_PASSWORD= PASSWORD
                - WORDPRESS_DB_NAME=DATABASE NAME
                - WORDPRESS_TABLE_PREFIX=wp_
              volumes:
                - wp_files:/var/www/html
          volumes:
            wp_files:
              external: true
          " > /root/wordpress/docker-compose.yml
          cd /root/wordpress
          docker-compose up -d
          sleep 20
          docker exec wordpress curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
          docker exec wordpress chmod +x wp-cli.phar
          docker exec wordpress mv wp-cli.phar /usr/local/bin/wp
          tempo=$(( $RANDOM % 40 + 4 ))
          echo $tempo > /root/tempo.txt
          sleep $tempo
          dnspage=${ELBloadbalancer.DNSName}
          testpage=$(curl --write-out %{http_code} -s --output /dev/null "http://$dnspage/index.html")
          echo -e $dnspage >> /root/mesip.txt
          echo -e $testpage > /root/testpage.txt
          docker exec wordpress wp core download --locale=fr_FR --force --allow-root;
          sleep 5
          if [ $testpage -eq 302 -o $testpage -eq 301 ] ; then docker exec wordpress wp core install --url=http://$dnspage --title="Le Site Entreprise" --admin_name=adminwp --admin_password=${PwdWP} --admin_email=monmail@gmail.com --allow-root; fi;
          docker exec wordpress wp language core activate fr_FR --allow-root
          docker exec wordpress wp plugin install https://downloads.wordpress.org/plugin/amazon-s3-and-cloudfront.2.6.1.zip --activate --allow-root --force
          sed -e :a -e '$q;N;10,$D;ba' /var/www/html/wp-config.php > /var/www/html/endwpconfig.txt
          for i in `seq 1 10`; do sed -i '$d' /var/www/html/wp-config.php; done
          echo -e "
          define( 'AS3CF_SETTINGS', serialize( array(\n    'provider' => 'aws',
              'access-key-id' => '${WPAccessKey}',
              'secret-access-key' => '${WPAccessKey.SecretAccessKey}',
              'use-server-roles' => false,
              'bucket' => '${S3BucketWP}',
              'region' => 'eu-west-3',
              'serve-from-s3' => true,
              'remove-local-file' => true,
              'copy-to-s3' => true,\n) ) );
          " >> /var/www/html/wp-config.php
          cat /var/www/html/endwpconfig.txt >> /var/www/html/wp-config.php
          chown -R www-data:www-data /var/www/html/wp-content/*```