We use machine learning technology to do auto-translation. Click "English" on top navigation bar to check Chinese version.
How to import migrated Amazon EC2 instances into infrastructure code
Modeling
This blog discusses the use case of server migrations using
Considerations and prerequisites
The following are recommended for continuing with this post:
- For testing purposes and to familiarize yourself with the implementation procedure, you can
launch an Amazon EC2 Linux instance and then use Application Migration Service to trigger thedata replication , launch a test instance and thena cutover instance . Use an older OS version to perform a patch or upgrade with Amazon Web Services Systems Manager. - Former2 provides a
CLI that allows direct use from the command line. Consult theFormer2 GitHub project to download Former2 and learn how to use thegenerate command, and to build a Docker image which we will upload toAmazon Elastic Container Registry (Amazon ECR) . - For large-scale migration scenarios, we recommend the use of the
Cloud Migration Factory on Amazon Web Services Solution . Consult theimplementation guide for solution components and installation steps. Our implementation procedure is applicable to migration waves executed with the solution. With the latter, thelaunch of cutover instances and theactivation of post-launch scripts occur programmatically. - Customers using
Amazon Web Services Systems Manager Automation only pay for what they use. Consumption is calculated based on the number and duration of steps. No additional charges incur for patching Linux and Windows EC2 instances usingAmazon Web Services Systems Manager Patch Manager as well as for usingAmazon Web Services Systems Manager Inventory . Consult the Amazon Web Services website for details onSystems Manager pricing .
Important note
Since Former2 is an open-source utility, we recommend a thorough review of its output prior to importing it into CloudFormation.
Solution overview
We assume the use case of a rehost migration factory, executing server migrations using Application Migration Service for batches of servers also called migration waves. The solution is equally applicable if you are using the
Identify resources outside of CloudFormation management
In the case of a rehost migration factory with multiple migration waves, we can build an event-driven workflow to add cutover instances into IaC. This can be achieved using an

Figure 1. Add existing Amazon Web Services resources to infrastructure code
Import resources into CloudFormation management
We use
Enable post-launch actions
You can enable

Figure 2. Enabling automated upgrades for cutover instances
Implementation procedures
Step 1. Identify the relevant events
Application Migration Service creates transient
Step 2. Create destination for Amazon S3 event notifications
Amazon S3 event notifications initiate our Lambda function, which fetches the associated server ID by reading the value from the Amazon Web ServicesApplicationMigrationServiceSourceServerID tag (created by Application Migration Service) and formulating the
Step 3. Set up Former2
Former2 provides a
Step 4. CloudFormation import
The CloudFormation
For any ancillary resources such as
We recommend keeping stacks small to reduce blast radius and de-risk follow-on changes. Resources managed by different teams and with different frequency of changes should be separated into different stacks. If your application servers are managed by one team, while database servers are managed by another, we recommend splitting the layers into separate stacks.
Step 5. Enable Application Migration Service post-launch actions
Activate
Cleaning up
Remember to delete example resources if you no longer need them to avoid incurring future costs. This includes:
-
Delete or disable the EventBridge rule - Clean up the Kinesis Data Firehose delivery stream on the Amazon Web Services Management Console. Go to Amazon Kinesis, then Delivery streams, select the delivery stream you want to delete, and click on Delete.
-
Delete the S3 buckets with cutover events and infrastructure code - Delete the Lambda function triggering Former2, by logging in to the Amazon Web Services Management Console. Go to the Functions section in Amazon Web Services Lambda, select the function, click on Actions and select Delete.
-
Deregister the Former2 Fargate task definition -
Terminate any Amazon EC2 instances (including cutover instances) used to test this solution -
Delete any Application Migration Service jobs used to test the presented solution
You can view your costs and usage using the Amazon Web Services Cost Explorer user interface.
Conclusion
In this post, we simplified the import of your migrated Amazon EC2 instances into infrastructure code using Former2. We showed how you can set up an event-driven workflow and continuously import cutover instances into CloudFormation. This keeps the number of resources outside the IaC state to a minimum. The presented solution can be adapted to different use cases including rapid prototyping and experimentation with new Amazon Web Services resources in sandbox environments. You can also initiate Former2 from the command line and control which Amazon Web Services resources are included in the stack.
Want to learn more?
-
Bringing existing resources into CloudFormation management -
Accelerate infrastructure as code development with open source Former2
About the authors:
The mentioned AWS GenAI Services service names relating to generative AI are only available or previewed in the Global Regions. Amazon Web Services China promotes AWS GenAI Services relating to generative AI solely for China-to-global business purposes and/or advanced technology introduction.