I am trying to create AMI from an existing Windows Instance with No Reboot and launch a new instance from the AMI with userdata.
But the userdata is not getting triggered.
Is there anyway to enable userdata execution without letting the main instance to reboot
I found the issue, while taking AMI the task scheduler "Amazon Ec2 Launch - Instance Initialization" inside the machine from which i take AMI is disabled. If we enable the task schedule and take an AMI from it, we will be able to execute userdata for the new machine
Related
I have scheduled EC2 instance to start every morning using Lambda. I have configured some task in task scheduler which I want it to run once EC2 is up however task wont get triggered until I login to EC2 instance. I want my task scheduler runs automatically at defined time without login to EC2 instance. Its a windows instance.
I tried configuring task without login but it doesn’t invoke.
Did you choose the Run whether user is logged on or not option of the task?
Fix Scheduled Task Won’t Run for .BAT File
Next, you have to choose the Run whether user is logged on or not option
To run a script every time an Amazon EC2 instance starts, simply add the script to this directory:
/var/lib/cloud/scripts/per-boot/
Most Amazon EC2 AMIs have cloud-init installed. It is responsible for running scripts from User Data. It will also check the above directory and run any scripts it finds there. This does not require a user to login.
I have created my own EC2 instance in AWS. That AMI is AWS ECS optimized AMI for launching ecs service from my EC2 instance. I previously discussed the same thing. And tried with that approach. The link is below,
Microservice Deployment Using AWS ECS Service
I created my cluster and configured that cluster name when I am creating optimized AMI by following code snippet in advanced userdata section,
#!/bin/bash
echo ECS_CLUSTER=your_cluster_name >> /etc/ecs/ecs.config
I followed the documentation of cluster creation from following link,
https://docs.aws.amazon.com/AmazonECS/latest/developerguide/create_cluster.htmlecs
But, no result - when creating cluster and ECS task definitions it creates and launches into one EC2. And again creating another EC2 by specifying above code. So total 2 Ec2. I already created my own ECS optimized.
I am finding for launching ECS service from my own AMI (that I created). Actually I need to launch my ECS service from my Ec2 (I had created my machine Amazon optimized AMI).
The reason behind this requirement is I don't want to launch my services in machine that owned by others. I need to launch from my machine. And also I need to host my angular application in the same my machine. So I need control of my machine. How can I do this?
Sounds like you just need to create a Launch Configuration. With this you can specify the User Data settings that should be applied when a host is setup.
After you create your Launch Configuration, create a new Auto Scaling Group based off of it (there's a drop-down to select the launch configuration you want to use).
From here, any new instances launched under that ASG will apply the settings you've configured in the associated Launch Configuration.
If I create the custom AMI, (This AMI is imported from VMware server using the import-image) can i use the user data while launching the instance from this AMI?Means cloud-init will be available in the custom AMI using the import-image? OS are RHEL..
You would need to install the cloud-init service on the VM you are creating the custom AMI from. That isn't something AWS does for you automatically.
I have an Amazon EC2 instance which is registered to a cluster of Amazon ECS.
And I want to change this instance's type from c4.large to c4.8xlarge.
I'm able to change its type from c4.large to c4.8xlarge in AWS console. But after the change, I found
[ERROR] Could not register module="api client" err="ClientException: Container instance type changes are not supported. Container instance XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX was previously registered as c4.large.
being printed in /var/log/ecs/ecs-agent.log.20XX-XX-XX-XX file.
Is it possible to change ec2 instance type and re-register it to a cluster?
I think maybe deregister it first, then register it again should work. But I'm afraid this may cause something irreversible in my AWS working environment. So I haven't tried this method yet.
To solve this connection problem between the agent and cluster, just delete the file /var/lib/ecs/data/ecs_agent_data.json and restart docker and ECS.
After that, a new container instance will be created in your cluster with the new size.
sudo rm /var/lib/ecs/data/ecs_agent_data.json
sudo service docker restart
sudo start ecs
Then you can go to the ECS cluster console and deregister the old container instance
UPDATE:
According to #florins and #MBear commented below, AWS updated the data file on ECS instances.
sudo rm /var/lib/ecs/data/agent.db
sudo service docker restart
sudo start ecs
As of March 2021 / AMI image ami-0db98e57137013b2d, /var/lib/ecs/data/ecs_agent_data.json mentioned in the last useful answer does not exist. For me, the commands to execute on the changed instance were:
sudo rm /var/lib/ecs/data/agent.db
sudo service docker restart
After that, it was possible to deploy containers to the instance, without fresh registration (AWS automatically registered a second ECS container instance of the new type). I did have a leftover container instance with the resources of the old instance type to remove.
You can't do this. Per their docs:
The type of EC2 instance that you choose for your container instances determines the resources available in your cluster. Amazon EC2 provides different instance types, each with different CPU, memory, storage, and networking capacity that you can use to run your tasks. For more information, see Amazon EC2 Instances.
This means that when you launch a container on an instance, the agent gathers a bunch of metadata about the instance to run it. If you change it, all of that metadata (or a lot) has changed in a bad way. CPU units, memory, etc. The agent is aware of this and will report it as an error.
You should spin up a new instance of the new type and register it to the cluster and let the task run on it. If it's a service, just terminate the old instance and let it run it against the new one.
I can't think of any real reason why terminating your old instance would cause something irreversible unless it is misconfigured or fragile via user specific settings, by default this would not cause anything destructive.
As alternative approach if the EC2 instance does not store any valuable a new instance using the old instance as template could be started. This takes all existing values and can be achieved just with a few clicks in minutes.
Select the EC2 instance and then "Actions -> Images and templates -> Start more like this". Just change the instance type.
When the instance is running got the the ECS cluster to the tab "ECS instances" and activate the new created instance.
Shutdown the old instance
Update your task maybe taking more cpu and memory and update the service to take the new task revision
What's the recommended method to automatically run 'yum update' after a new EC2 Instance launch (Instance based on Amazon Linux AMI)
You could use a CloudInit User-Data Script.
I would personally configure a shell script to pass as user data, which would run all needed server set up tasks.
Configure an init task on a fresh EC2 instance to run yum update on some sort of schedule (you generally want to ensure that all of your instances are always running identical versions of software), then create a new AMI from the running instance you just modified.
From then-on, simply launch your new AMI instead of a fresh AMI. Over time, you'll want to do this same thing again so that instance launches won't take so long as more and more updates become available.
Once every 3-4 months seems to be a good delta.