{"id":359,"date":"2023-05-13T22:36:23","date_gmt":"2023-05-13T15:36:23","guid":{"rendered":"https:\/\/thnkandgrow.com\/?p=359"},"modified":"2023-05-13T22:36:23","modified_gmt":"2023-05-13T15:36:23","slug":"understanding-user-scripts-when-launching-ec2-instances","status":"publish","type":"post","link":"https:\/\/thnkandgrow.com\/blog\/2023\/05\/13\/understanding-user-scripts-when-launching-ec2-instances\/","title":{"rendered":"Understanding User Scripts When Launching EC2 Instances"},"content":{"rendered":"\n
Amazon Elastic Compute Cloud (EC2) is a popular cloud computing service that allows users to launch virtual servers, also known as instances, in the cloud. When launching an EC2 instance, users can provide a user script, also known as user data, that will be executed during the instance’s launch process. In this blog post, we will discuss user scripts in more detail and how they can be used to automate instance configuration and management tasks.<\/p>\n\n\n\n
<\/p>\n\n\n\n
User scripts are scripts that are executed when an EC2 instance is launched. They can be written in any scripting language, including bash, Python, or PowerShell, and are passed to the instance during launch as part of the instance metadata. User scripts can be used to perform various tasks, such as installing software, configuring the instance, and running custom scripts.<\/p>\n\n\n\n
User scripts can be used to automate the process of launching and configuring EC2 instances. This can save time and reduce the chance of errors when configuring instances manually. For example, you can use user scripts to automatically install and configure web servers, databases, or other software on the instance. User scripts can also be used to customize instances based on your specific needs, such as setting up a specific directory structure or configuring the network settings.<\/p>\n\n\n\n
To use a user script when launching an EC2 instance, you can provide the script as part of the instance’s user data. This can be done through the EC2 console, API, or command-line interface. When the instance is launched, the user script will be passed to the instance and executed automatically.<\/p>\n\n\n\n
It’s important to note that user scripts should be idempotent, meaning that they should be able to be executed multiple times without causing issues. This is because EC2 instances can be stopped and started multiple times during their lifetime, and the user script will be executed each time the instance is launched.<\/p>\n\n\n\n
Example user script to deploy Rails app with redis, nodejs, ruby installed with rvm, postgresql and puma config for nginx (for ubuntu<\/strong>)<\/p>\n\n\n\n When the instance is booted for the first time, this script will run and the output will be in the\u00a0 When using user scripts, there are some best practices to follow to ensure that they are effective and reliable:<\/p>\n\n\n\n User scripts are a powerful tool for automating the process of launching and configuring EC2 instances. By providing a script during instance launch, users can automate the installation and configuration of software, customize instances to their specific needs, save time, and reduce the risk of errors when configuring instances manually. By following best practices for using user scripts, such as testing scripts and keeping them simple, users can ensure that their instances are configured reliably and effectively.<\/p>\n\n\n\n <\/p>\n\n\n\n <\/p>\n\n\n\n Notion: https:\/\/www.notion.so\/hoangtamle\/Deploy-rails-to-AWS-47d46f0310e54ebcb9b0c11e6b262b49?pvs=4<\/p>\n","protected":false},"excerpt":{"rendered":" Amazon Elastic Compute Cloud (EC2) is a popular cloud computing service that allows users to launch virtual servers, also known as instances, in the cloud. When launching an EC2 instance, users can provide a user script, also known as user data, that will be executed during the instance’s launch process. In this blog post, we […]<\/p>\n","protected":false},"author":1,"featured_media":362,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[62,6,53],"tags":[12],"yoast_head":"\n#!\/bin\/bash\nsudo apt-get update\nsudo apt-get -y install build-essential libssl-dev libreadline-dev zlib1g-dev git\nsudo apt-get -y install libpq-dev postgresql postgresql-contrib\nsudo apt-get -y install redis-server\nsudo apt-get -y install nodejs\nsudo apt-get -y install nginx\n\n# Install RVM and Ruby\nsudo apt-add-repository -y ppa:rael-gc\/rvm\nsudo apt-get update\nsudo apt-get -y install rvm\nsource \/etc\/profile.d\/rvm.sh\nrvm install 2.7.4\nrvm use 2.7.4 --default\n\n# Install Bundler\ngem install bundler\n\n# Configure PostgreSQL\nsudo -u postgres createuser --superuser ubuntu\nsudo -u postgres createdb -O ubuntu myapp_production\n\n# Configure Nginx\nsudo rm \/etc\/nginx\/sites-enabled\/default\nsudo touch \/etc\/nginx\/sites-available\/myapp\nsudo ln -s \/etc\/nginx\/sites-available\/myapp \/etc\/nginx\/sites-enabled\/myapp\nsudo bash -c \"cat > \/etc\/nginx\/sites-available\/myapp\" <<EOF\nupstream myapp {\n server unix:\/\/\/var\/www\/myapp\/myapp.sock;\n}\n\nserver {\n listen 80;\n server_name myapp.com;\n\n root \/var\/www\/myapp\/public;\n\n try_files \\$uri\/index.html \\$uri @myapp;\n\n location @myapp {\n proxy_pass http:\/\/myapp;\n proxy_set_header Host \\$host;\n proxy_set_header X-Real-IP \\$remote_addr;\n proxy_set_header X-Forwarded-For \\$proxy_add_x_forwarded_for;\n proxy_set_header X-Forwarded-Proto \\$scheme;\n proxy_redirect off;\n proxy_buffering off;\n proxy_http_version 1.1;\n proxy_set_header Connection '';\n chunked_transfer_encoding off;\n proxy_cache_bypass \\$http_pragma;\n proxy_cache_revalidate on;\n proxy_cache_valid 200 5m;\n expires 5m;\n }\n}\nEOF\n\nsudo service nginx restart\n<\/code><\/pre>\n\n\n\n
\/var\/log\/cloud-init-output.log<\/code>\u00a0directory on your instance.<\/p>\n\n\n\n
Best Practices for Using User Scripts<\/h2>\n\n\n\n
Conclusion<\/h2>\n\n\n\n