JaviOverflow
JaviOverflow

Reputation: 1480

How to recover ebs from snapshot

Given a cloudformation template with an EC2 instance that uses a EBS volume defined as follows:

  DefaultVolume:
    Type: AWS::EC2::Volume
    DeletionPolicy: Snapshot
    Properties:
      AvailabilityZone: eu-west-1a
      Size: 8
      Tags:
        -
          Key: Name
          Value: Jenkins
      VolumeType: gp2

How can I set up the cloudformation template so when I recreate the stack again (after a deletion and ebs snapshot created), the ebs recovers the data from the snapshot instead of creating a brand new volume?

Upvotes: 0

Views: 236

Answers (1)

kichik
kichik

Reputation: 34704

I'm not sure there is a way to reference a snapshot of a deleted stack. One issue with that is how would it know which snapshot to take if there are multiple stacks created from the same template?

What you can do is add a parameter for your template for the snapshot id and use it with SnapshotId when specified.

Parameters:
  OldSnapshot:
    Type: String
    Default: ""
Conditions:
  OldSnapshotAvailable:
    !Not [!Equals [!Ref OldSnapshot, ""]]
Resources:
  DefaultVolume:
    Type: AWS::EC2::Volume
    DeletionPolicy: Snapshot
    Properties:
      AvailabilityZone: eu-west-1a
      Size: 8
      Tags:
        -
          Key: Name
          Value: Jenkins
      VolumeType: gp2
      SnapshotId: !If [OldSnapshotAvailable, !Ref OldSnapshot, !Ref AWS::NoValue]

Upvotes: 1

Related Questions