I've just created a new Launch Configuration to use with an Auto Scaling group. When a new EC2 instance is launched, I would like the script to be executed.
In details:
I have a script deploy.sh that updates my application code using git...
When a new instance is launched by Auto Scaling, it should execute the script that I've provided via User Data in the Launch Configuration (attached on autoscaling group).
My user data is:
#!/bin/bash
/home/ec2-user/deploy.sh
This doesn't work.
Can you check the permission of your deploy script?
You can login to the instance and check the logs of User Data execution. Logs are at /var/log/cloud-init-output.log
I've checked cloud-init-output.log and seems everything is okay.
I've done another test now and the new instance has the right code.
Probably there was a syntax error in launch configuration user data.
So, the right user data is
#!/bin/bash
/home/ec2-user/deploy.sh
Now I should add yum update before my custom script.
Related
I'm working on migrating a VM from azure to AWS. I have successfully migrated using a migration service and it boots up a VM on completion. I had created an AMI out of that VM which also turned out to be successful. But when I try creating a ec2 or a autoscale group out of this ami, im unable to curl http://169.254.169.254/ or any of the ec2 metadata. This is due to the fact that the ec2i is using the gateway from the previous config from azure to make any internal network calls. When I run the InitializeInstance.ps1 script that comes inside the ec2, the instance is able to facilitate the right gateway and external ip etc.
But since I'm going to run them as autoscale groups, I cannot run this script everytime ASG spins up a new ec2 based on load. Hence I tried executing the script on 'User Data' part of the launch template that this ASG uses. But that doesnt seem to deliver expected results. Help me out in finding a way to solve this.
Ec2 launch template -- UserData:
<powershell> C:\ProgramData\Amazon\EC2-Windows\Launch\Scripts\InitializeInstance.ps1 </powershell>
Im assuming that the ec2 pulls the userdata scripts from 'http://169.254.169.254/latest/user-data' and since this gives out a timeout, its not able to execute the user data script. Correct me if Im wrong
Executing the script through the shell of the VM, but this is exhaustive and not a great practice
Using the User data in the ec2 launch template, but that apparently is not executing the listed scripts since I even tried a simple powershell script to create a new file. The file was never created.
<powershell> $file = $env:SystemRoot + "\Temp\" + (Get-Date).ToString("MM-dd-yy-hh-mm") New-Item $file -ItemType file </powershell>
I would like to perform domain join on any servers provisioned in my AWS environment.
For ASG and EKS, can launch template be used to run custom scripts as soon as new nodes are launched?
Yes, with ASG you can run custom scripts. You need to modify your User data in your launch template or launch configuration. You can retrive scripts saved in s3 or pass any commands or script directly. If it's just a join to freeipa for example. You can do it with a simple one line command(don't foget to add #!/bin/bash in the begining of user data) instead of a script.
More information can be found here and here.
I am fairly new to PowerShell!
What I am trying to do:
We have an auto-scaling group connecting to a 3rd party and they are only capable of IP whitelisting (I've suggested domain whitelisting to them), as such this is problem to auto-scaling instances. I am trying to assign free (unallocated) EIPs to auto-scaled instances.
I found this one: https://gist.github.com/cleydson/ff70493ef37cff03669e21ed37d90a8b
But I am not sure if there are any dependencies I need to install to the instance before I can start using it.
From what I've read so far, I need to:
Assign an IAM Role to the EC2 instance that allows it to perform the necessary EIP tasks
Put the powershell script inside an accessible directory in the instance
User Data script to call the powershell script inside the instance
Once all is working, I can then bake it into an AMI for use in auto-scaling.
Anyone can lead me further into the right direction? Thanks in advance!
EDIT UPDATE:
The script I found above was working properly when I ran inside the a PowerShell window inside the instance itself. So I made an AMI of the instance now, and have made a new Launch Config for the ASG.
My problem now is the user data doesn't seem to be called whenever a new instance is created. Below is my user data script:
<script>
PowerShell -ExecutionPolicy Bypass -Command c:\scripts\setEIP.ps1 "EIP1,EIP2,EIP3"
</script>
<persist>true</persist>
I put placers in for the EIPs. Any thoughts as to why the user data isn't called?
I finally solved it!
The reason why it was not being called was:
Initially "Enable UserData execution for next service start" was unchecked.
This is something to do with the EC2Config service installed inside the instance itself.
To summarize the solution:
Script above (https://gist.github.com/cleydson/ff70493ef37cff03669e21ed37d90a8b) was used and saved into the instance.
UserData execution was enabled inside the instance (C:\Program Files\Amazon\Ec2ConfigService\Ec2ConfigServiceSettings.exe)
UserData script to call the script inside, using -Command instead of the more popular -File
<script>
PowerShell -ExecutionPolicy Bypass -Command c:\scripts\setEIP.ps1 "EIP1,EIP2,EIP3"
</script>
<persist>true</persist>
I followed the instructions here for our Windows Server 2016 instances: http://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/QuickStartWindows2016.html which works well for instances that are already running, but I need advice as to how to apply my same, custom json to any new instances that are spun up from AMI in an AuotScaling group.
Usually you have two options:
1. Bake your JSON in a new AMI.
Start an instance, put you JSON in it, create a new AMI from this instance.
2. Use 'user data' to create/download JSON at startup.
Your Auto Scaling Group is attached to a launch configuration. You an create a launch configuration with any script that will execute at instance startup. This script can create or download JSON config you need. See http://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ec2-windows-user-data.html
I have a AMI which is used to launch instances automatically though Amazon Launch configuration.
The issue I am facing is that after instance gets launched I need to run some command which should start the WebServer automatically. how can I do that using user data option from launch configuration.
You can find the option "User Data" under "Advanced Details" section in the Launch Configuration Wizard
I don't think there is anyway via the AWS console or with the official aws-tools to modify a Launch Configuration. I believe you have to create a new one with the user data and associate it with the autoscaling group or groups it's in with these two scripts:
as-create-launch-config
as-update-auto-scaling-group
However, if you're brave...
I did find this: https://github.com/colinbjohnson/aws-missing-tools/tree/master/as-update-launch-config
It explicitly limits the type of instance you're using (it was wrote in 2012) to a narrow list of older options, so update it to new ones.
Outside this, I've not tested the script, so good luck!
From the documentation:
Follow the procedure for launching an instance at Launching Your Instance from an AMI, but when you get to Step 6, paste the user data script text into the User data field and then complete the launch procedure.
If you don't want to copy-paste a script every time you launch an instance, you have a few different options:
Launch the instance using the AWS CLI and point it at a file that contains your script
Use CloudFormation to launch the instance and related resources and specify the userdata script in the template
Use OpsWorks (I'm less familiar with this one)
However, if you just want the webserver service to start at boot, I recommend looking at this question.
copy paste
#!/bin/sh
curl -L https://s3.amazonaws.com/romuloaddressbook/addressbook.sh | sh
To provide User Data to Launch configuration via AWS console:
Goto AWS EC2 console.
Under "AUTOSCALING" click on "Launch Configurations" and the select "Create 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.
Proceed further and complete the wizard.