Update stack with imported EC2 instance that was restored

0

My customer spins up and updates EC2 instances using Cloudformation stacks with some parameters, among which a custom AMI. AWS backup makes daily backups of the stack (retention of 7 days), containing the stack template and the EC2 instance. When restoring an EC2 instance, it refers to the AMI that was created by the backup service. To keep updating the restored instance using stacks, it is imported again into the stack. What AMI should I use when importing? The original custom AMI is not the right reference, and the AMI used at the time of restore will no longer be available after 7 days. Can I somehow take the AMI that was used during restore out of the retention policy and put it aside?

asked 2 months ago244 views
2 Answers
4
Accepted Answer

Hello,

To copy AMI:

  1. In the EC2 console, go to Images > AMIs.
  2. Select the AMI that was created during the restore.
  3. Click on Actions > Copy AMI.
  4. Specify the name (EX. Restored-Instance-AMI) and description.
  5. Choose the destination region if necessary and click Copy AMI.

Update CloudFormation Template

To Edit your CloudFormation template to include the new AMI ID example.

Resources:
  MyEC2Instance:
    Type: "AWS::EC2::Instance"
    Properties:
      InstanceType: t2.micro
      ImageId: ami-0abcd1234efgh5678  # Replace with your copied AMI ID
      KeyName: my-key-pair
      # ... other properties ...

Perform Stack Update:

  1. In the CloudFormation console, select your stack and choose Update Stack.
  2. Upload the updated template with the new AMI ID or edit the template inline.
  3. Review and confirm the update.

https://docs.aws.amazon.com/managedservices/latest/ctref/management-advanced-ec2-instance-stack-update.html

https://docs.amazonaws.cn/en_us/AWSCloudFormation/latest/UserGuide/using-cfn-updating-stacks-update-behaviors.html

EXPERT
answered 2 months ago
profile picture
EXPERT
reviewed 2 months ago
EXPERT
reviewed 2 months ago
profile picture
EXPERT
Sandeep
reviewed 2 months ago
3

HELLO....!

Create a new AMI after restoring the EC2 instance.

This is the thing you solve simply your issue:

  • Restore EC2 instance: Use AWS Backup to restore the desired instance.
  • Create new AMI: Once the instance is restored and running, create a new AMI from it.
  • Update CloudFormation stack: Modify the stack template to use the newly created AMI.
  • Update stack: Deploy the updated stack to incorporate the new AMI.
profile picture
EXPERT
answered 2 months ago
profile picture
EXPERT
Sandeep
reviewed 2 months ago
profile picture
EXPERT
reviewed 2 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions