I cannot get my newly launched ec2 instances to run the User Data script specified in Advanced Settings -> User Data.
If I manually SSH into my instance after it is done initializing and type "cd DIRECTORY", & "npm run SCRIPT" the instance will run the NodeJS app perfectly. However I cannot get the User Data script to do this automatically on ec2 instance initialization.
I saw in similar articles to include "#!/bin/bash" at the beginning of my script but this has not made a difference.
When launching a new instance, in Advanced Settings -> User Data script I have:
#!/bin/bash <- tried with and without this
cd DIRECTORY
npm run SCRIPT
Which is the exact command I use to run my NodeJS app by manually SSH-ing into the instance. Is there something I am missing??
I am launching an Amazon Linux T3.micro instance from my AMI.
Any ideas on how to troubleshoot or fix this??
Related
So I have set up ci/cd using gitlab and is now able to do
Build the Docker image
Tag it properly
Push it to ECR
SSH to EC2 instance
Pull image to the EC2 instance
However, I still need to run the docker image for it to be complete.
Right now, I am using the --env_file to specify the env_file for that container, but I still have to create the env file manually on the ec2 instance first.
Is there a way for me to just copy and replace the .env file I have in my repository to the ec2 instance, so it can be updated from that file instead of having to redo it every time there's a change?
You can use scp <path-to-env-file> <ec2-user#><ec2-address>:<path-to-docker-directory>.
Another solution: You could also build Ansible Playbook and execute all of your steps. You would need to write your steps in form of ansible tasks or roles, then target the correct host. For example steps from 1->3 are excuted locally, and 5->7 (where 6 is copying .env file and 7 is starting the docker container) to be remotely (on EC2 instance) executed.
More on this: https://www.redhat.com/en/topics/automation/what-is-an-ansible-playbook#:~:text=Ansible%20Playbooks%20are%20lists%20of,as%20which%20user%20executes%20it.
I'm trying to automatically run airflow webserver and scheduler in a VM upon boot using startup scripts just followed the documentation here: https://cloud.google.com/compute/docs/instances/startup-scripts/linux . Here is my script:
export AIRFLOW_HOME=/home/name/airflow
cd /home/name/airflow
nohup airflow scheduler >> scheduler.log &
nohup airflow webserver -p 8080 >> webserver.log &
The .log files are created which means the script is been executed but the webserver and the scheduler don't.
Any apparent reason?
I have tried replicating the Airflow webserver Startup script on GCP VM using the document.
Steps followed to run Airflow webserver Startup script on GCP VM :
Create a Service Account. Give minimum access to BigQuery with the role of BigQuery Job User and Dataflow with the role of Dataflow Worker. Click Add Key/Create new key/Done. This will download a JSON file.
Create a Compute Engine instance. Select the Service Account created.
Install Airflow libraries. Create a virtual environment using miniconda.
Init your metadata database and register at least one admin user using command:
airflow db init
airflow users create -r Admin -u username -p mypassword -e example#mail.com -f yourname -l lastname
Whitelist IP for port 8080. Create Firewall Rule and add firewall rule on GCP VM instance. Now go to terminal and start web server using command
airflow webserver -p 8080.
Open another terminal and start the Scheduler.
export AIRFLOW_HOME=/home/acachuan/airflow-medium
cd airflow-medium
conda activate airflow-medium
airflow db init
airflow scheduler
We want our Airflow to start immediately after the Compute Engine starts. So we can create a Cloud Storage bucket and then create a script, upload the file and keep it as a backup.
Now pass a Linux startup script from Cloud Storage to a VM. Refer Passing a startup script that is stored in Cloud Storage to an existing VM. You can also pass a startup script to an existing VM.
Note : PermissionDenied desc = The caller does not have permission means you don’t have sufficient permissions, you need to request access from your project, folder, or organization admin. Depending on the assets you are trying to export. And to access files which are created by root users you need read, write or execute permissions. Refer File permissions.
Can we set Environment variable against an EC2 instance from AWS console?
Or do we need load any other service to achieve this?
Also can we load this variable insider Docker?
That appears to be out of scope for the AWS Console. EC2 however is basically AWS hosted VMs, So anything you could do normally on that OS can be done.
Just connect to the machine and do what you would normally do. (See the AWS CLI Guide for further help)
Using the AWS Console:
Stop the ec2 instance
For that ec2 instance, from top dropdown - select Actions > Instance Settings > View/Change User Data
Create a script that would add required environment variables to init scripts.
!/bin/sh echo 'export MY_ENV_VARIABLE=value' >> ~/.bashrc
or a script to /etc/init.d or other similar locations..
Start the EC2 instance for the Environment variables to be available.
If you want to pass the EC2 instance environment variable to the Docker container environment variable:
When you start a docker instance through "docker run" you can pass the value through --env param in the command line.
eg: docker run -e DOCKER_ENV_VARIABLE=$MY_ENV_VARIABLE ...
https://docs.docker.com/engine/reference/commandline/run/#options
You can use user_data to load and afterwards pass to your docker container using docker run -e CONTAINER_ENV_VAR=$EC2_ENV_VARor put in your Dockerfile.
for keys or any sensitive data I would advise you to use the Parameter Store, you can put passwords, users, or whatever data you want and use a service call chamber
My aim is to launch an instance such that a start-up script is triggered on boot-up to download some configuration files stored in AWS S3. Therefore, in the start-up script, I am setting the S3 bucket details and then, triggering a config.sh where aws s3 sync does the actual download. However, the aws command does not work - it is not found for execution.
User data
I have the following user data when launching an EC2 instance:
#!/bin/bash
# Set command from https://stackoverflow.com/a/34206311/919480
set -e -x
export S3_PREFIX='bucket-name/folder-name'
/home/ubuntu/app/sh/config.sh
The AWS CLI was installed with pip as described in the documentation.
Observation
I think, the user data script is run with root user ID. That is why, in the user data I have /home/ubuntu/ because $HOME did not resolve into /home/ubuntu/. In fact, the first command in config.sh is mkdir /home/ubuntu/csv which creates a directory with owner as root!
So, would it be right to conclude that, the user data runs under root user ID?
Resolution
Should I use REST API to download?
Scripts entered as user data are executed as the root user, so do not use the sudo command in the script.
See: Running Commands on Your Linux Instance at Launch
One solution is to set the PATH env variable to include AWS CLI (and add any other required path) before executing AWS CLI.
Solution
Given that, AWS CLI was installed without a sudo pip, the CLI is not available for root. Therefore, to run with ubuntu user, I used the following user data script:
#!/bin/bash
su ubuntu -c '$HOME/app/sh/config.sh default`
In config.sh, the argument default is used to build the full S3 URI before invoking the CLI. However, the invocation was successful only with the full path $HOME/.local/bin/aws despite the fact that aws can be accessed with normal login.
Have some Python script to be run on Amazon AMI Spot Instance.
Wondering can I deploy by Python script/remote script :
1) The AMI spot instance.
2) Lubuntu, Anaconda + additionnal Python conda packages dynamically
on the AMI spot instance through script
Do I need to use Docker to have everything packaged in advance ?
There is StarCluster pacakge in Python, am not sure if we can use to launch
Spot Instance ?
The easiest way to get started with bootstrapping EC2 instances at launch is to add a custom user data script. If you start the instance user data with #! and the path to the interpreter you want to use, it gets executed at boot time and can perform any customization you want.
Example:
#!/bin/bash
yum update -y
Further documentation: User Data and Shell Scripts