I am working on a script through which I can create an instance and has the same setting (like vpc, subnet ID, Security group and user data) from another existing instance. I am able to get all the setting except user data as user data needs to be saved then can be passed to another instance. But when I saved the user data, it is saved in Jibrish language. I used this command : (Get-EC2InstanceAttribute -InstanceId i-sda23dsf -Attribute userData).UserData > userdata.txt so how can I pass it to another instance so that both can have same user data script.
3 Answers
Userdata is available at
http://169.254.169.254/latest/user-data
if you absolutely want to continue down the route you're on.
It would make far more sense to create both instances using an orchestration language like ansible or puppet or terraform, which would give you reproducibility and allow you to make changes to various parts of your infrastructure on the fly.

- 11,442
- 35
- 28
-
http://169.254.169.254/latest/user-data This needs to be access from server it self which I cannot. As a scenario, server is not up or corrupted that is why I am creating another instance which has same setting. I am not trying to create two instance at one go. I am trying to create an instance having the same setting of existing instance. I don't want to manually create instance and putting all the value of existing instance one by one. It is tedious and error prone. – Rohit Singh Mar 16 '20 at 19:30
-
@RohitSingh use an orchestration tool as mentioned. CloudFormation, ansible, terraform, or others. this way you can keep your userdata versioned and reusable. – ptierno Mar 16 '20 at 23:09
If you still have access to the original instance, even if that instance is stopped, try to select the instance, then in "Actions", do "Launch more like this" and continue until you reach "3. Configure Instance" screen. Under "Advanced Details" you'll find the original UserData.
No need to actually launch "new" instance.

- 3,587
- 2
- 12
- 27
You don't do it that way, the user data script is run once the first time an EC2 is created.
What you're talking about sounds like "Baking an AMI" or "making a Golden Image".
This is handy for user data scripts that take more than a minute in that you can spin up the AMI with everything pre-installed. This is also ideal for multiple EC2s that have the same userdata:
The professional way is to automate the AMI creation using a tool called Packer.
Another common way people share userdata scripts between EC2s is have the userdata download a script from an S3 bucket and then execute it. This is perfect for userdata scripts larger than the 16500 byte limit.

- 61,933
- 36
- 195
- 321