Error occurred during build: Command 01_symlink failed

0

Hello team,

I have deployed wordpress application on Elastic Beanstalk. Now I have added one more directory named .ebextension in project root directory to mount the elastic filesystem on Beanstalk EC2 Amazon Linux instance

In this .ebextension, i have placed two files.

  1. i have used default storage-efs-mountfilesystem.config and updated my filesystem id and mounted directory that is /uploads
option_settings:
  aws:elasticbeanstalk:application:environment:
    FILE_SYSTEM_ID: 'fs-0123456789'
    MOUNT_DIRECTORY: '/uploads'

##############################################
#### Do not modify values below this line ####
##############################################

packages:
  yum:
    amazon-efs-utils: []

commands:
  01_mount:
    command: "/tmp/mount-efs.sh"

files:
  "/tmp/mount-efs.sh":
      mode: "000755"
      content : |
        #!/bin/bash

        EFS_MOUNT_DIR=$(/opt/elasticbeanstalk/bin/get-config environment -k MOUNT_DIRECTORY)
        EFS_FILE_SYSTEM_ID=$(/opt/elasticbeanstalk/bin/get-config environment -k FILE_SYSTEM_ID)

        echo "Mounting EFS filesystem ${EFS_FILE_SYSTEM_ID} to directory ${EFS_MOUNT_DIR} ..."

        echo 'Stopping NFS ID Mapper...'
        service rpcidmapd status &> /dev/null
        if [ $? -ne 0 ] ; then
            echo 'rpc.idmapd is already stopped!'
        else
            service rpcidmapd stop
            if [ $? -ne 0 ] ; then
                echo 'ERROR: Failed to stop NFS ID Mapper!'
                exit 1
            fi
        fi

        echo 'Checking if EFS mount directory exists...'
        if [ ! -d ${EFS_MOUNT_DIR} ]; then
            echo "Creating directory ${EFS_MOUNT_DIR} ..."
            mkdir -p ${EFS_MOUNT_DIR}
            if [ $? -ne 0 ]; then
                echo 'ERROR: Directory creation failed!'
                exit 1
            fi
        else
            echo "Directory ${EFS_MOUNT_DIR} already exists!"
        fi

        mountpoint -q ${EFS_MOUNT_DIR}
        if [ $? -ne 0 ]; then
            echo "mount -t efs -o tls ${EFS_FILE_SYSTEM_ID}:/ ${EFS_MOUNT_DIR}"
            mount -t efs -o tls ${EFS_FILE_SYSTEM_ID}:/ ${EFS_MOUNT_DIR}
            if [ $? -ne 0 ] ; then
                echo 'ERROR: Mount command failed!'
                exit 1
            fi
            chmod 777 ${EFS_MOUNT_DIR}
            runuser -l  ec2-user -c "touch ${EFS_MOUNT_DIR}/it_works"
            if [[ $? -ne 0 ]]; then
                echo 'ERROR: Permission Error!'
                exit 1
            else
                runuser -l  ec2-user -c "rm -f ${EFS_MOUNT_DIR}/it_works"
            fi
        else
            echo "Directory ${EFS_MOUNT_DIR} is already a valid mountpoint!"
        fi

        echo 'EFS mount complete.'
  1. mount.config - In this mount.config file i have updated code like below.
container_commands:
  01_symlink:
    command: ln -s /uploads /var/app/ondeck/wp-content/

When i deploy my code as zip into Beantsalk environment, its getting failed.

If I had a quick look at the eb-engine logs in Beanstalk,

2024/03/02 06:27:18.719789 [INFO] Error occurred during build: Command 01_symlink failed

2024/03/02 06:27:18.719827 [ERROR] An error occurred during execution of command [app-deploy] - [PostBuildEbExtension]. Stop running the command. Error: Container commands build failed. Please refer to /var/log/cfn-init.log for more details. 

If I check with the cfn-init.log, I could see the below logs

2024-03-02 06:27:18,681 [ERROR] Error encountered during build of postbuild_0_MyWebsiteApplication: Command 01_symlink failed
Traceback (most recent call last):
  File "/usr/lib/python3.9/site-packages/cfnbootstrap/construction.py", line 579, in run_config
    CloudFormationCarpenter(config, self._auth_config, self.strict_mode).build(worklog)
  File "/usr/lib/python3.9/site-packages/cfnbootstrap/construction.py", line 277, in build
    changes['commands'] = CommandTool().apply(
  File "/usr/lib/python3.9/site-packages/cfnbootstrap/command_tool.py", line 127, in apply
    raise ToolError(u"Command %s failed" % name)
cfnbootstrap.construction_errors.ToolError: Command 01_symlink failed
2024-03-02 06:27:18,683 [ERROR] -----------------------BUILD FAILED!------------------------
2024-03-02 06:27:18,683 [ERROR] Unhandled exception during build: Command 01_symlink failed
Traceback (most recent call last):
  File "/opt/aws/bin/cfn-init", line 181, in <module>
    worklog.build(metadata, configSets, strict_mode)
  File "/usr/lib/python3.9/site-packages/cfnbootstrap/construction.py", line 137, in build
    Contractor(metadata, strict_mode).build(configSets, self)
  File "/usr/lib/python3.9/site-packages/cfnbootstrap/construction.py", line 567, in build
    self.run_config(config, worklog)
  File "/usr/lib/python3.9/site-packages/cfnbootstrap/construction.py", line 579, in run_config
    CloudFormationCarpenter(config, self._auth_config, self.strict_mode).build(worklog)
  File "/usr/lib/python3.9/site-packages/cfnbootstrap/construction.py", line 277, in build
    changes['commands'] = CommandTool().apply(
  File "/usr/lib/python3.9/site-packages/cfnbootstrap/command_tool.py", line 127, in apply
    raise ToolError(u"Command %s failed" % name)
cfnbootstrap.construction_errors.ToolError: Command 01_symlink failed

However, EFS has been mounted in /uploads directory successfully.

#mount -t nfs4

127.0.0.1:/ on /uploads type nfs4 (rw,relatime,vers=4.1,rsize=1048576,wsize=1048576,namlen=255,hard,noresvport,proto=tcp,port=20091,timeo=600,retrans=2,sec=sys,clientaddr=127.0.0.1,local_lock=none,addr=127.0.0.1)

If I check with my project directory, whether mount link has been created, But its not created.

sym link

Am I missing anything? Can any one please what should i do to resolve this?

asked 2 months ago245 views
1 Answer
1

From the information you've provided and the screenshot you shared, it looks like the symlink command in your .ebextensions configuration is failing during deployment on AWS Elastic Beanstalk. Here are a few things to consider that might resolve the issue:

  1. Make sure that the source directory (/uploads) exists and is accessible at the time the symlink command is run. Since /uploads is your EFS mount point, confirm that it's successfully mounted before the symlink command is executed.

  2. Confirm that the destination directory (/var/app/ondeck/wp-content/) exists. If the wp-content directory does not exist or if there is a typo in the path, the symlink command will fail.

  3. The user that the Elastic Beanstalk environment uses to execute the container_commands might not have the necessary permissions to create a symlink in the destination directory.

  4. If the destination directory (/var/app/ondeck/wp-content/uploads) already exists, the symlink command will fail. You can modify the command to handle this case, for example by checking if the directory exists and if so, removing it before creating the symlink.

Here's an updated version of your container_commands which includes a check to remove the existing directory before creating the symlink:

container_commands:
  01_remove_existing_dir:
    command: "rm -rf /var/app/ondeck/wp-content/uploads"
    ignoreErrors: true
  02_symlink:
    command: "ln -s /uploads /var/app/ondeck/wp-content/uploads"

Adding the ignoreErrors: true line will ensure that the build process doesn't stop if the directory doesn't exist and the rm command fails.

Please try updating your .ebextensions configuration and redeploy. If you still face issues, you may want to log into the EC2 instance and try to run the symlink command manually to see if it provides any additional error messages.

profile picture
EXPERT
answered 2 months ago
  • I just updated the container_commands as you mentioned and deploy it again to Beanstalk environment. Now deployment has been succeeded, however still not performing as i desired. my mounting directory seems like looping like below ls -lh /uploads/uploads/uploads/uploads/uploads/ When i check with /var/app/ondeck/wp-content/, there also /uploads/uploads/uploads/ and sym link has been created, but looping same folder. whereas i dont see any symlink in /var/app/current/wp-content/ - Because this is my production path. /var/log/cfn-init.log logs, 2024-03-03 05:50:40,510 [INFO] -----------------------Starting build----------------------- 2024-03-03 05:50:40,515 [INFO] Running configSets: Infra-EmbeddedPreBuild 2024-03-03 05:50:40,518 [INFO] Running configSet Infra-EmbeddedPreBuild 2024-03-03 05:50:40,520 [INFO] Running config prebuild_0_MyWebsiteApplication 2024-03-03 05:50:41,472 [INFO] Command 01_mount succeeded 2024-03-03 05:50:41,473 [INFO] ConfigSets completed 2024-03-03 05:50:41,473 [INFO] -----------------------Build complete----------------------- 2024-03-03 05:50:46,362 [INFO] -----------------------Starting build----------------------- 2024-03-03 05:50:46,367 [INFO] Running configSets: Infra-EmbeddedPostBuild 2024-03-03 05:50:46,369 [INFO] Running configSet Infra-EmbeddedPostBuild 2024-03-03 05:50:46,371 [INFO] Running config postbuild_0_MyWebsiteApplication 2024-03-03 05:50:46,425 [INFO] Command 01_remove_existing_dir succeeded 2024-03-03 05:50:46,475

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions