amazon-web-servicesscalaamazon-ec2sbtsdkman

How to pre-install sbt on an AWS EC2 instance (via user data)?


What I tried to far without success inside the AWS EC2 user data section:

#!/bin/bash    
sudo curl -s "https://get.sdkman.io" | bash

#!/bin/bash
sudo curl --output-dir "/home/ec2-user/" -s "https://get.sdkman.io" | bash

The problem is that the .sdkman folder in above cases is not stored inside /home/ec2-user/.sdkman but rather always stored inside the root folder under /.sdkman for an automated EC2 instance launch with user data. (Manual installation via cmd on the other hand would work)

Which fails to make following next command to install sbt via user data:

source "/home/ec2-user/.sdkman/bin/sdkman-init.sh"

(Accepting that the .sdkman folder is stored inside the root folder) I also tried instead to use:

source ".sdkman/bin/sdkman-init.sh"

source "/.sdkman/bin/sdkman-init.sh"

but this does not work and returns following error:

find: ‘/home/ec2-user/.sdkman/src’: No such file or directory
find: ‘/home/ec2-user/.sdkman/ext’: No such file or directory
touch: cannot touch ‘/home/ec2-user/.sdkman/var/delay_upgrade’: No such file or directory
-bash: /home/ec2-user/.sdkman/var/candidates: No such file or directory
-bash: __sdkman_echo_debug: command not found

As it looks like with the source command am I not able to go below /home/ec2-user/ as it's always added back to the path.


Further resources: https://sdkman.io/install


AWS EC2 Instance: Amazon Linux 2 AMI (HVM) - Kernel 5.10 // t3a.xlarge

Any help on how I can install sbt on an AWS EC2 instance automated via user data is highly appreciated!


Solution

  • Ec2 UserData will execute as the root user. if you want to execute the user data as a non-root user, please follow the below format. p.s - all command has to be nested under single command, if you break into multiple lines, then the next line will be executed under the root user.

    #!/bin/bash
    su ec2-user bash -c "curl -s "https://get.sdkman.io" | bash; source "/home/ec2-user/.sdkman/bin/sdkman-init.sh"