Backup and Restore of Terraform Statefile & EC2
Backup & Restore Of Member StateFile
Overview
This runbook provides detailed instructions for backing up a replicated Terraform statefile from the eu-west-1
region and restoring it to a new folder in the eu-west-2
region. The statefile is stored in an S3 bucket with DynamoDB for state locking.
Step 1: Create a New Folder for Testing in S3
Log into AWS Management Console:
- Access the Modernisation Platform Account and navigate to the S3 service.
Navigate to the S3 Bucket:
- Go to the
modernisation-platform-terraform-state
bucket located in theeu-west-2
region.
- Go to the
Create a New Folder for Testing:
- Create a new folder within this bucket specifically for testing. This ensures the production state file remains isolated, preventing accidental overwrites.
Step 2: Copy the Backup State File for Testing
Locate and Copy the Backup State File:
- In the S3 bucket located in the
eu-west-1
region, find the backup state file.
- In the S3 bucket located in the
Set the Destination:
- Use the “Copy” function for the state file.
- Under “Destination type,” select “General purpose bucket.”
- Use the “Browse S3” feature to navigate to the destination bucket and folder created in the previous step.
Specify Destination Path:
- Ensure the destination path is correctly set, such as
s3://modernisation-platform-terraform-state/environments/accounts/cooker/cooker-development/test-restore/
.
- Ensure the destination path is correctly set, such as
Note: This method is applicable for objects encrypted with SSE-S3 (Amazon S3 managed keys). It is not suitable for objects encrypted with AWS KMS keys (SSE-KMS) or customer-provided encryption keys (SSE-C) due to additional restrictions.
Step 3: Verify the Copied State File in eu-west-2
- Confirm State File Availability:
- Navigate to the
modernisation-platform-terraform-state
bucket in theeu-west-2
region. - Verify that the copied state file exists in the specified test folder.
- Navigate to the
Step 4: Configure Terraform Backend for Testing
- Update Terraform Configuration:
- Modify Terraform configuration file (e.g.,
backend.tf
) to point the backend key to the new test path where the copied state file resides.
- Modify Terraform configuration file (e.g.,
Step 5: Initialise and Test the Configuration
Reinitialise the Terraform Backend:
- Run
terraform init -reconfigure
to reinitialize the backend with the new test configuration. This step ensures that Terraform recognises the state file at the new location.
- Run
Verify the Setup:
- Execute
terraform plan
to confirm that the state file is correctly recognised and that no changes will be applied unintentionally.
- Execute
EC2 Backup and Restore
1. Set Up and Launch an EC2 Instance
- Create an EC2 instance with a “Test” file.
2. Set Up AWS Backup
- You can set up AWS Backup manually through the AWS Management Console or automate the process using Terraform. The setup involves creating a Backup Vault, an IAM role with necessary policies, and defining a backup plan.
Create Backup Vault
IAM Role and Policy Setup (create a new one or use default)
Create IAM Role for AWS Backup:
- Create a new role with AWS Backup service as the trusted entity.
- Attach the
AWSBackupServiceRolePolicyForBackup
managed policy.
Create and Attach IAM Policy:
- Create a custom IAM policy with permissions for EC2 and backup operations.
- Attach this policy to the IAM role created for AWS Backup.
3. Backup Plan Setup
Create a Backup Plan: - In the AWS Backup Console, go to “Backup plans” and click “Create backup plan.” - Choose a predefined plan or create a custom one. - Set backup frequency (e.g., daily) and retention period (e.g., 7 days).
4. Create On-Demand Backup
- Select protected resources and create on-demand backup.
- Select EC2 as the resource type that you want to back up.
- Choose the Instance ID of the EC2 resource that you want to protect.
5. Restore EC2 Instance
- Go to the AWS Backup console
- Click on “Backup vaults” and select your vault.
- Select the recovery point ARN and choose Restore.
- This will bring you to a Restore backup screen that will have the configurations for the EC2 instance using the backed-up AMI.
- Follow the prompts to complete the restoration.
- Check for your restored backup job under Restore jobs in the the AWS Backup console.
6. Verify the restored instance
- Connect to the restored EC2 instance using SSH or AWS Systems Manager (SSM).
- Navigate to the location of the test file.