Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass in the user-data when launching AWS instances using CLI

I'm using the AWS CLI to launch instances, and the command is: aws ec2 run-instances

what i'm expecting is to pass in a script as the user-data. so, I did: DATA=base64 ./my_script on my Mac OSX, and then pass the DATA by: aws ec2 run-instances --user-data $DATA

BUT, nothing happened after the instance launched

So, how exactly should I do?

Thanks!!

like image 488
Edward Avatar asked Mar 10 '14 03:03

Edward


People also ask

How do I add user data in launch configuration?

Create Launch Configuration wizard will open. Select the required AMI. In the next window, select the Instance Type and click "Next: Configure details". Under "Configure details" -> "Advanced Details" -> Enter your USERDATA in the "User data" text box.

What is user data when launching a new ec2 instance?

When you launch an instance in Amazon EC2, you have the option of passing user data to the instance that can be used to perform common automated configuration tasks and even run scripts after the instance starts. You can pass two types of user data to Amazon EC2: shell scripts and cloud-init directives.

Which AWS CLI command should I use to launch create an ec2 instance?

Launch your instance To launch an Amazon EC2 instance using the AMI you selected, use the aws ec2 run-instances command.


2 Answers

To pass the script as a string, make sure to specify the interpreter as normal prior to your commands. Hitting enter in the open string allows for adding a new line. Ex.:

$ aws ec2 run-instances --image-id ami-16d4986e --user-data '#!/bin/bash
> poweroff'
like image 109
AXE Labs Avatar answered Oct 16 '22 10:10

AXE Labs


There is no need to base64 encode the data yourself.

You can prefix a file name/path with file://

So,

aws ec2 run-instances --user-data file://my_script

or

aws ec2 run-instances --user-data file:///full/path/to/my_script
like image 36
Eric Hammond Avatar answered Oct 16 '22 11:10

Eric Hammond