Update EC2 AMI root drive - amazon-web-services

I have an EC2 AMI that I create instances from to be used to execute builds. I now need to modify this AMI because I need an additional program installed on it. What I want to do is make my AMI point at a different snapshot to use as its root drive (a snapshot with the new program installed) and things would be all well and good. But, I can't find a way to do this. Someone from Amazon on the forums said it's not possible, but I'm not so sure. So, I wanted to ask here.
I know I can just take the updated snapshot I want and create an entirely new AMI from it, but this results in a new AMI ID and now I need to go change the AMI ID which my scripts use to launch a new instance. I don't want to do this every time I realize I need a change to my AMI setup.

you can build private AMI from exist ec2 instance (of course you can public it as well). Then you can start a new ec2 instance from "My AMI" with all installed application/package directly.
Take a look on this doc
Amazon Machine Images (AMI)
There are new feature such as Docker introduced by AWS last month, but it is not ready to public to use currently.
Another benefit to create private AMI image is, it will save you much time when you need launch a new instance.
If you need update your configuration file, after new AMI created, I recommend to make a CI trigger (via Jenkins, for example). run a awscli command in script, it will easily update your config file. All these tasks, include create a new AMI, update configuration file, etc can be done under Jenkins/bamboo automatically.
If you are not confident with this way, then think about cloudformation template. it will make big improvement in your system. But if you fully set it, the cloudformation way will save you a lot of time in future changes.
In Cloudformation, you need set launch configuration and its autoscaling group, you still need create private AMI image, but every time after create new image, you need trigger a script to update the ami image in its Launch configuration, after that, any new instance will automatically use the new AMI.

Related

Old data still coming from EC2 instance in Auto Scaling group

I have created ELB (load Balancing) and Auto Scaling Group (ASG) after instance coming 'this is my 1st instance working fine after I have login via putty and changed the "index.html" file updated index.html ("This is my 2nd Instance updated') file then deleted instance, new instance coming old data only coming -- Here how to recover new data I mean updated instance,
I want "This is my 2nd Instance updated"
I'm not sure I followed but I think what you're saying is:
You manually SSH to the instance and update index.html, then AutoScaling launches a new instance and it doesn't have those changes. Is that correct?
AutoScaling has no way of knowing what you did inside the instance. AWS can't peak inside your instances and look at your data, that would be a pretty big privacy breach.
When you make updates you need to modify the launch template or launch configuration in some way to make sure the new instances have the updates. Either:
Make a new AMI with the changes
Put the changes in a userdata script
Alternatively, you could have some sort of automation help with this such as:
Have a userdata script which downloads the newest files from an S3 bucket. Just make sure to update the website files in this bucket whenever you make changes
Use some sort of CI/CD pipeline tool such as AWS CodeDeploy. This will automatically push changes to your existing instances, and make sure new instances are launched with the newest code.

AWS Cloud formation does not copy the data to the newly created stack

In AWS cloud formation, i use the cloud former tool. I can use that tool to create a cloud formation template from existing resources. And then use the template to create a stack. I tested with that tool. It can work, (as in it can create instances with same memory size, with same volume size, same VPC settings, and auto start the instances). But there is no files in the volume.
Do i have to create a snapshot of the existing volume, create a new volume from the snapshot, attach it to the newly created instance, and copy the files manually ?
Or is there any better way ?
Do i have to create a snapshot of the existing volume, create a new volume from the snapshot, attach it to the newly created instance, and copy the files manually ?
Cloudformation is provisioning resources, but is not responsible for provisioning the contents of those resources - that you have to do yourself.
You can leverage the EC2 Userdata to manually pull files from S3 or other repos as the instance boots.
Or is there any better way ?
If you want to share data between applications, EFS is always an option. In your case, though, using Userdata might be effective.
If you wish to launch new EC2 instances with software automatically loaded, there are basically two choices:
Use a pre-configured AMI, or
Use a startup script to load the software
Pre-configured AMI
An Amazon Machine Image (AMI) is a copy of a disk. When a new EC2 instance is launched, an AMI is selected and the boot disk (and optionally other disks) are automatically pre-loaded with the contents of the AMI.
A common practice is to boot an EC2 instance and configure it as desired. Then, create an AMI. Thereafter, when a new EC2 instance is required for the application, launch it using the pre-built AMI.
There are also tools available to automate the building of an AMI, such as Netflix Aminator and Packer.
Benefits: New machine boots quickly, fully-configured.
Issues: Need to create a new AMI whenever you update your software.
Use a startup script to load the software
When an Amazon EC2 instance is launched from a standard Amazon-provided AMI (eg Amazon Linux, Microsoft Windows), software on the AMI automatically looks at the User Data passed to an EC2 instance. If the User Data contains a startup script, the script will be executed -- but only the first time that an instance is launched. This is an excellent way to install software on the instance.
You are responsible for writing the script. The script should install whatever tools, software and data you want on the instance.
Benefits: Updating your software? Just launch a new instance and the script will install the latest version of your software (assuming you have written the script to always point to the latest version).
Issues: It takes longer to launch the new instance, since the software is being installed.

Creating image of the running instance using Packer

I launched an instance in AWS using Terraform with basic functionalities.Once the instance is launched, i need to capture that instance into image using Packer.
How can i accomplish the same?
Packer is used to make customised AMIs. But if the image is already running and customised then an AMI can be made with standard AWS tools
Once the instance is running and configured with Terraform, take an ami image of it. See this documentation https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/creating-an-ami-ebs.html
OTOH if you want to automate the whole process of making the AMI automatically then add your steps that configure the base AMI in the first place to run Terraform within the packer script
Once you have a Packer script like this, the whole process is automatic
To make a new AMI running packer build on your packer config file should do everything automatically
Edit: your comments below hint that maybe what you want to do is use Packer on an already running instance to make an AMI, perhaps with further configuration to add to it. To do this, first make an AMI of the running image. There are instructions for doing this in the link above.
Next, write your Packer script in such a way that it takes the AMI id of the new image as a parameter. Then you can run the Packer script using the new AMI as input and so finish up with an AMI made by Packer but based on the running instance
Sorry it took a while to add this but I can't imagine a use case where this would be a useful thing to do

Is there any way to edit AMI being used for auto scaling in AWS?

I have created Auto scaling group in AWS using a customized AMI. Now to rollout my new code I need to either update all instances running but then if a new instance comes up it won't be updated. So, I need a way to update AMI. One way could be creating new AMI and Autoscaling group.
Thanks in advance.
This is one way to go about it:
Spin up a stand-alone instance using the AMI
Make changes
Stop instance
Create new AMI from this instance
Create a new Launch Configuration that uses the new AMI
Update the Autoscaling Group to use the new Launch Configuration
Slowly terminate the old instances in the Autoscaling Group, and let them be automatically replaced with instances using the new AMI
Of course all this is a pain to deal with manually every time you need to make a change. Elastic Beanstalk and CloudFormat both provide mechanisms to deal with this in a more automated way.
If you are just changing the code you are deploying to your servers, then there are other ways to handle this, such as using AWS CodeDeploy. You could also update the running servers in some automated or manual fashion, and configure the AMI such that any new instances that are created will go get the latest code on startup.

Amazon Custom AMI

If I create a custom AMI for an EBS backed EC2 instance after installing numerous applications and making lot of config changes to the EC2 instance like IP Tables, httpd.conf file etc...
Will the custom AMI image capture all those config changes and/or installed applications so that I can use it to launch exact functioning copy of the Custom AMI originating EC2 Instance?
Anything done after launching an EC2 instance will be independent of what the original AMI had. There isn't a relationship among the instances which use the same AMI as well; except that they all were materialised from a single AMI - the individual / independent changes in the Instances ( AMI ) would be in silos.
Coming back to your point; after making numerous changes; you would need to create an image AMI out of the running instance where the changes have been made. Going forward you can use the AMI to create new instances. Already created instances wouldn't reflect any new changes.
This is where the tools like Ansible, Chef, Puppet come into picture.