...

/

Configuration: Setting Up User Data

Configuration: Setting Up User Data

Learn to provide the configuration via environment variables through EC2 user data.

Now that our WordPress image is able to read environment variables to set up its configuration, it’s time to provide them.

Note: For now, you will create the environment variables and the credentials in the EC2 user data script. This is not ideal for security and flexibility reasons, but right now, you haven’t learned about other ways to do so yet. In any case, the fundamental approach of using environment variables is good, just hardcoding the credentials into the EC2 user data is not. Later on in this course, you’ll learn about AWS Parameter Store meant to store credentials more securely.

Extracting your DB_PASSWORD

Run the script in the code widget below to extract your DB_PASSWORD. All it does is log setting up your SSH key, get the public DNS name, and then execute the grep DB_PASSWORD /opt/bitnami/wordpress/wp-config.php command to print out all lines in the wp-config.php file that contain DB_PASSWORD.

Press + to interact
#!/bin/bash
# 1. Set up your hello-wordpress SSH key
mkdir -p ~/.ssh && aws s3 cp $SSH_PRIVATE_KEY_URL ~/.ssh/id_rsa && chmod 0400 ~/.ssh/id_rsa
# 2. Get the public DNS name of the WordPress instance
PUBLIC_DNS_NAME=$(aws ec2 describe-instances \
--filters "Name=tag:team,Values=wordpress" \
--filters "Name=instance-state-name,Values=running" \
--query 'Reservations[0].Instances[0].PublicDnsName' --output text)
# Check if the WordPress instance is running
if [ -z $PUBLIC_DNS_NAME ]; then echo 'ERROR: Could not find the WordPress instance. Are you sure its running?'; exit 1; fi
if [ "$PUBLIC_DNS_NAME" == 'None' ]; then echo 'ERROR: Could not find the WordPress instance. Are you sure its running?'; exit 1; fi
# 3. Extract the DB_PASSWORD
ssh -q -o StrictHostKeyChecking=no bitnami@$PUBLIC_DNS_NAME 'grep DB_PASSWORD /opt/bitnami/wordpress/wp-config.php'

This should return the following output (with a different password for you): ...

Access this course and 1400+ top-rated courses and projects.