Chris Bolton
Chris Bolton

Reputation: 2314

Export Existing EC2 instance to CloudFormation json/yaml

Problem:

I have an EC2 instance running and I have made some modifications to the instance: installed docker, setup directories for certs, etc. Now, I am wanting to create the same instance but use infrastructure as code principals. Instead of remembering all the additions that I have done and creating a template by hand, I am trying to find a way to export my current EC2 instance into a json or yaml format so that I can terminate this instance and create another one that is equivalent to the one running.

I have tried:

aws ec2 describe-instances

Reading through the AWS CLI EC2 docs

Reading through the CloudFormation docs

Searched Google

Searched SO

Upvotes: 4

Views: 4080

Answers (4)

John Rotenstein
John Rotenstein

Reputation: 269340

Since you have no knowledge of how the instance was setup, the only choice is to create an Amazon Machine Image (AMI). This will create an exact copy of the disk, so everything you have installed will be available to any new instances launched from the AMI. The CloudFormation template can then be configured to launch instances using this AMI.

If, on the other hand, you knew all the commands that needed to be run to configure the instance, then you could provide a User Data script that would run when new instances first boot. This would configure the instances automatically and is the recommended way to configure instances because it is easy to modify and allows instances to launch with the latest version of the Operating System.

Such a script can be provided as part of a CloudFormation template.

See: Running commands on your Linux instance at launch - Amazon EC2

Upvotes: 3

Marcin
Marcin

Reputation: 238209

There is a tool (still in beta) developed by AWS called CloudFormer:

CloudFormer is a template creation beta tool that creates an AWS CloudFormation template from existing AWS resources in your account. You select any supported AWS resources that are running in your account, and CloudFormer creates a template in an Amazon S3 bucket.

The CloudFormer is an AWS managed template. Once you launch it, the template will create an AWS::EC2::Instance for you along with a number of other related resources. You will access the instance using URL through browser, and an AWS wizard will guide you from there.

Its tutorial even shows how to create a CloudFormation template from an existing EC2 instance.

Upvotes: 1

alexis-donoghue
alexis-donoghue

Reputation: 3387

One option would be to create AMI from live instance and spin up new CF stack using the AMI.

Other would be importing resource: https://aws.amazon.com/blogs/aws/new-import-existing-resources-into-a-cloudformation-stack/

Upvotes: 3

Chris Williams
Chris Williams

Reputation: 35188

Import the EC2 instance into CloudFormation then copy it’s template.

Read more: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/resource-import.html

Upvotes: 0

Related Questions