AWS Amazon RDS Snapshot
This page shows how to write Terraform and CloudFormation for Amazon RDS Snapshot and write them securely.
aws_db_snapshot (Terraform)
The Snapshot in Amazon RDS can be configured in Terraform with the resource name aws_db_snapshot
. The following sections describe 5 examples of how to use the resource and its parameters.
Example Usage from GitHub
resource "aws_db_snapshot" "audit-snapshot" {
db_instance_identifier = aws_db_instance.audit.id
db_snapshot_identifier = "audit-snapshot"
resource "aws_db_snapshot" "snap" {
db_instance_identifier = aws_db_instance.database-1.id
db_snapshot_identifier = "snap-database-1"
resource "aws_db_snapshot" "db_snapshot" {
db_instance_identifier = var.db_instance_identifier
db_snapshot_identifier = var.db_snapshot_identifier
tags = module.camtags.tagsmap
}
resource "aws_db_snapshot" "test" {
db_instance_identifier = aws_db_instance.default.id
db_snapshot_identifier = "testsnapshot1234"
}
resource "aws_db_snapshot" "manual_snapshot" {
db_instance_identifier = aws_db_instance.mysql.identifier
db_snapshot_identifier = "manual-snapshot"
}
data "aws_subnet_ids" "database" {
Parameters
-
allocated_storage
optional computed - number -
availability_zone
optional computed - string -
db_instance_identifier
required - string -
db_snapshot_arn
optional computed - string -
db_snapshot_identifier
required - string -
encrypted
optional computed - bool -
engine
optional computed - string -
engine_version
optional computed - string -
id
optional computed - string -
iops
optional computed - number -
kms_key_id
optional computed - string -
license_model
optional computed - string -
option_group_name
optional computed - string -
port
optional computed - number -
snapshot_type
optional computed - string -
source_db_snapshot_identifier
optional computed - string -
source_region
optional computed - string -
status
optional computed - string -
storage_type
optional computed - string -
tags
optional - map from string to string -
vpc_id
optional computed - string -
timeouts
single block-
read
optional - string
-
Explanation in Terraform Registry
Manages an RDS database instance snapshot. For managing RDS database cluster snapshots, see the
aws_db_cluster_snapshot
resource.
Tips: Best Practices for The Other AWS Amazon RDS Resources
In addition to the aws_db_instance, AWS Amazon RDS has the other resources that should be configured for security reasons. Please check some examples of those resources and precautions.
aws_db_instance
Ensure backup retension of your RDS instance is specified
It's better to set it explicitly to reduce the risk of availability issues.
aws_rds_cluster
Ensure backup retension of your RDS cluster is specified
It's better to set it explicitly to reduce the risk of availability issues.
aws_rds_cluster_instance
Ensure your RDS cluster instance blocks unwanted access
It's better to limit accessibily to the minimum that is required for the application to work.
AWS::RDS::DBCluster (CloudFormation)
The DBCluster in RDS can be configured in CloudFormation with the resource name AWS::RDS::DBCluster
. The following sections describe 10 examples of how to use the resource and its parameters.
Example Usage from GitHub
Type: 'AWS::RDS::DBCluster'
Properties:
MasterUsername: !Ref DBUsername
MasterUserPassword: !Ref DBPassword
DBClusterIdentifier: aurora-postgresql-cluster
Engine: aurora-postgresql
Type: 'AWS::RDS::DBCluster'
Properties:
MasterUsername: !Ref DBUsername
MasterUserPassword: !Ref DBPassword
DBClusterIdentifier: aurora-postgresql-cluster
Engine: aurora-postgresql
Type: 'AWS::RDS::DBCluster'
Properties:
MasterUsername: !Ref DBUsername
MasterUserPassword: !Ref DBPassword
DBClusterIdentifier: aurora-postgresql-cluster
Engine: aurora-postgresql
Type: 'AWS::RDS::DBClusterParameterGroup'
Properties:
Description: 'Aurora PostgreSQL 10 Parameter Group'
Family: aurora-postgresql10
Parameters:
rds.force_ssl: 1
Type: AWS::RDS::DBCluster
Properties:
DatabaseName: ${self:custom.environments.DB_DATABASE}
Engine: aurora-mysql
EngineMode: serverless
MasterUsername: ${self:custom.environments.DB_USER_NAME}
"resourceType": "AWS::RDS::DBClusterSnapshot",
"resourceId": "rds:database-1-2020-05-19-05-58",
"resourceName": "rds:database-1-2020-05-19-05-58"
},
{
"resourceType": "AWS::RDS::DBClusterSnapshot",
"resourceType": "AWS::RDS::DBClusterSnapshot",
"resourceId": "rds:database-1-2020-05-19-05-58",
"resourceName": "rds:database-1-2020-05-19-05-58"
},
{
"resourceType": "AWS::RDS::DBClusterSnapshot",
"resourceType": "AWS::RDS::DBClusterSnapshot",
"resourceId": "rds:database-1-2020-05-19-05-58",
"resourceName": "rds:database-1-2020-05-19-05-58"
},
{
"resourceType": "AWS::RDS::DBClusterSnapshot",
"ResourceType": "AWS::RDS::DBCluster",
"ResourceStatus": "CREATE_COMPLETE"
},
{
"PhysicalResourceId": "auroraClusterParameterGroup",
"ResourceType": "AWS::RDS::DBClusterParameterGroup",
"Type": "AWS::RDS::DBCluster",
"Properties": {
"Engine": "aurora-postgresql",
"EngineMode": "provisioned",
"EngineVersion": {
"Ref": "EngineVersion"
Parameters
-
AssociatedRoles
optional - List of DBClusterRole -
AvailabilityZones
optional - List -
BacktrackWindow
optional - Long -
BackupRetentionPeriod
optional - Integer -
CopyTagsToSnapshot
optional - Boolean -
DBClusterIdentifier
optional - String -
DBClusterParameterGroupName
optional - String -
DBSubnetGroupName
optional - String -
DatabaseName
optional - String -
DeletionProtection
optional - Boolean -
EnableCloudwatchLogsExports
optional - List -
EnableHttpEndpoint
optional - Boolean -
EnableIAMDatabaseAuthentication
optional - Boolean -
Engine
required - String -
EngineMode
optional - String -
EngineVersion
optional - String -
GlobalClusterIdentifier
optional - String -
KmsKeyId
optional - String -
MasterUserPassword
optional - String -
MasterUsername
optional - String -
Port
optional - Integer -
PreferredBackupWindow
optional - String -
PreferredMaintenanceWindow
optional - String -
ReplicationSourceIdentifier
optional - String -
RestoreType
optional - String -
ScalingConfiguration
optional - ScalingConfiguration -
SnapshotIdentifier
optional - String -
SourceDBClusterIdentifier
optional - String -
SourceRegion
optional - String -
StorageEncrypted
optional - Boolean -
Tags
optional - List of Tag -
UseLatestRestorableTime
optional - Boolean -
VpcSecurityGroupIds
optional - List
Explanation in CloudFormation Registry
The
AWS::RDS::DBCluster
resource creates an Amazon Aurora DB cluster. For more information, see Managing an Amazon Aurora DB Cluster in the Amazon Aurora User Guide.Note You can only create this resource in AWS Regions where Amazon Aurora is supported.
Updating DB clustersWhen properties labeled "Update requires: Replacement" are updated, AWS CloudFormation first creates a replacement DB cluster, then changes references from other dependent resources to point to the replacement DB cluster, and finally deletes the old DB cluster.
Important We highly recommend that you take a snapshot of the database before updating the stack. If you don't, you lose the data when AWS CloudFormation replaces your DB cluster. To preserve your data, perform the following procedure: Deactivate any applications that are using the DB cluster so that there's no activity on the DB instance.
Create a snapshot of the DB cluster. For more information about creating DB snapshots, see Creating a DB Cluster Snapshot.
If you want to restore your DB cluster using a DB cluster snapshot, modify the updated template with your DB cluster changes and add the
SnapshotIdentifier
property with the ID of the DB cluster snapshot that you want to use. After you restore a DB cluster with aSnapshotIdentifier
property, you must specify the sameSnapshotIdentifier
property for any future updates to the DB cluster. When you specify this property for an update, the DB cluster is not restored from the DB cluster snapshot again, and the data in the database is not changed. However, if you don't specify theSnapshotIdentifier
property, an empty DB cluster is created, and the original DB cluster is deleted. If you specify a property that is different from the previous snapshot restore property, a new DB cluster is restored from the specifiedSnapshotIdentifier
property, and the original DB cluster is deleted.Update the stack.
Currently, when you are updating the stack for an Aurora Serverless DB cluster, you can't include changes to any other properties when you specify one of the following properties:
PreferredBackupWindow
,PreferredMaintenanceWindow
, andPort
. This limitation doesn't apply to provisioned DB clusters.For more information about updating other properties of this resource, see
[ModifyDBCluster](https://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_ModifyDBCluster.html)
. For more information about updating stacks, see AWS CloudFormation Stacks Updates.Deleting DB clustersThe default
DeletionPolicy
forAWS::RDS::DBCluster
resources isSnapshot
. For more information about how AWS CloudFormation deletes resources, see DeletionPolicy Attribute.
Frequently asked questions
What is AWS Amazon RDS Snapshot?
AWS Amazon RDS Snapshot is a resource for Amazon RDS of Amazon Web Service. Settings can be wrote in Terraform and CloudFormation.
Where can I find the example code for the AWS Amazon RDS Snapshot?
For Terraform, the Alan18081/custom-infra, danielssabin/teste and ICpTrial/starterLibrary source code examples are useful. See the Terraform Example section for further details.
For CloudFormation, the jcroall/kaimonkey-demo, mallik-user1/kaimonkey_customized and accurics/KaiMonkey source code examples are useful. See the CloudFormation Example section for further details.