A Terraform module to automate the taking of backup snapshots.
Include this repository as a module in your existing terraform code:
module "ebsnapshot" {
source = "JamesWoolfenden/ebssnap/aws"
version = "0.2.9"
common_tags = var.common_tags
snapshot_name = local.snapshot_name
cron_expression = var.cron_expression
regions = var.regions
}
Creates a lambda and cloudwatch event to look for any instances that have the specified tag and then takes a snapshot of that volume. This is a heavily modified version of this module: https://github.com/kgorskowski/ebs_bckup
Set up your Terraform tf.vars file to look something like:
common_tags = {
application = "terraform"
module = "auto-ebs-snapshot"
environment = "develop"
}
EC2_INSTANCE_TAG = "Backup"
RETENTION_DAYS = 10
cron_expression = "45 1 * * ? *"
regions = ["eu-west-1","eu-west-2","eu-west-3"]
No requirements.
Name | Version |
---|---|
archive | n/a |
aws | n/a |
random | n/a |
template | n/a |
No modules.
Name | Type |
---|---|
aws_cloudwatch_event_rule.ebsbackuptimer | resource |
aws_cloudwatch_event_target.runebsbackuplambda | resource |
aws_iam_role.ebsbackuplambda | resource |
aws_iam_role_policy.ebsbackuplambda | resource |
aws_lambda_function.ebsbackup | resource |
aws_lambda_permission.allow_cloudwatch_to_call | resource |
random_string.label | resource |
template_dir.vars | resource |
archive_file.lambda_zip | data source |
aws_iam_policy_document.ebsbackuplambda | data source |
Name | Description | Type | Default | Required |
---|---|---|---|---|
EC2_INSTANCE_TAG | Tag to identify the EC2 target instances of the Lambda Function | string |
"Backup" |
no |
RETENTION_DAYS | Numbers of Days that the EBS Snapshots will be stored (INT) | string |
5 |
no |
common_tags | Implements the common tags scheme | map(any) |
n/a | yes |
cron_expression | Cron expression for firing up the Lambda Function | string |
n/a | yes |
regions | Which Regions to apply the snapshots too | list(any) |
n/a | yes |
runtime | n/a | string |
"python2.7" |
no |
snapshot_name | The name of the snapshot | string |
n/a | yes |
timeout | Lambda Timeout in seconds | number |
60 |
no |
tracing_mode | Xray tracing mode | string |
"Active" |
no |
Name | Description |
---|---|
lambda | n/a |
role | n/a |
This is the policy required to build this project:
The Terraform resource required is:
resource "aws_iam_policy" "terraform_pike" {
name_prefix = "terraform_pike"
path = "/"
description = "Pike Autogenerated policy from IAC"
policy = jsonencode({
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"ec2:DescribeAccountAttributes"
],
"Resource": "*"
},
{
"Sid": "VisualEditor1",
"Effect": "Allow",
"Action": [
"events:DeleteRule",
"events:DescribeRule",
"events:ListTagsForResource",
"events:ListTargetsByRule",
"events:PutRule",
"events:PutTargets",
"events:RemoveTargets"
],
"Resource": "*"
},
{
"Sid": "VisualEditor2",
"Effect": "Allow",
"Action": [
"iam:CreateRole",
"iam:DeleteRole",
"iam:DeleteRolePolicy",
"iam:GetRole",
"iam:GetRolePolicy",
"iam:ListAttachedRolePolicies",
"iam:ListInstanceProfilesForRole",
"iam:ListRolePolicies",
"iam:PassRole",
"iam:PutRolePolicy"
],
"Resource": "*"
},
{
"Sid": "VisualEditor3",
"Effect": "Allow",
"Action": [
"lambda:AddPermission",
"lambda:CreateFunction",
"lambda:DeleteFunction",
"lambda:GetFunction",
"lambda:GetFunctionCodeSigningConfig",
"lambda:GetPolicy",
"lambda:ListVersionsByFunction",
"lambda:RemovePermission",
"lambda:TagResource",
"lambda:UntagResource"
],
"Resource": "*"
}
]
})
}
Check out these related projects.
- terraform-aws-s3 - S3 buckets
Copyright 2021-2022 James Woolfenden
See LICENSE for full details.
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.