Commit 3e6e7dd5 authored by Anton Babenko's avatar Anton Babenko

Added code for RDS modules

parent 3d4a26f8
.terraform
terraform.tfstate
*.tfstate*
terraform.tfvars
Licensed 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
http://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.
# terraform-aws-rds AWS RDS Terraform module
Terraform module which creates RDS instance on AWS ========================
Terraform module which creates RDS resources on AWS.
These types of resources are supported:
* [DB Instance](https://www.terraform.io/docs/providers/aws/r/db_instance.html)
* [DB Subnet Group](https://www.terraform.io/docs/providers/aws/r/db_subnet_group.html)
* [DB Parameter Group](https://www.terraform.io/docs/providers/aws/r/db_parameter_group.html)
Root module calls these modules which can also be used separately to create independent resources:
* [db_instance](https://github.com/terraform-aws-modules/terraform-aws-rds/tree/master/modules/db_instance) - creates RDS DB instance
* [db_subnet_group](https://github.com/terraform-aws-modules/terraform-aws-rds/tree/master/modules/db_subnet_group) - creates RDS DB subnet group
* [db_parameter_group](https://github.com/terraform-aws-modules/terraform-aws-rds/tree/master/modules/db_parameter_group) - creates RDS DB group
Usage
-----
```hcl
module "db" {
source = "terraform-aws-modules/rds/aws"
identifier = "demodb"
engine = "mysql"
engine_version = "5.7.11"
instance_class = "db.t2.large"
allocated_storage = 5
name = "demodb"
username = "user"
password = "YourPwdShouldBeLongAndSecure!"
port = "3306"
vpc_security_group_ids = ["sg-12345678"]
maintenance_window = "Mon:00:00-Mon:03:00"
backup_window = "03:00-06:00"
tags = {
Owner = "user"
Environment = "dev"
}
# DB subnet group
subnet_ids = ["subnet-12345678", "subnet-87654321"]
# DB parameter group
family = "mysql5.7"
}
```
Examples
--------
* [Complete RDS example](https://github.com/terraform-aws-modules/terraform-aws-rds/tree/master/examples/complete)
Limitations
-----------
* [module db_parameter_group](https://github.com/terraform-aws-modules/terraform-aws-rds/tree/master/modules/db_parameter_group) does not implement setting of parameters
Notes
-----
1. This module does not create RDS security group. Use [terraform-aws-sg](https://github.com/terraform-aws-modules/terraform-aws-sg) module for this.
Authors
-------
Migrated from `terraform-community-modules/tf_aws_rds`, where it was maintained by [these awesome contributors](https://github.com/terraform-community-modules/tf_aws_rds/graphs/contributors).
Module managed by [Anton Babenko](https://github.com/antonbabenko).
License
-------
Apache 2 Licensed. See LICENSE for full details.
\ No newline at end of file
Complete RDS example
====================
Configuration in this directory creates set of RDS resources including DB instance, DB subnet group and DB parameter group.
Data sources are used to discover existing VPC resources (VPC, subnet and security group).
Usage
=====
To run this example you need to execute:
```bash
$ terraform init
$ terraform plan
$ terraform apply
```
Note that this example may create resources which cost money. Run `terraform destroy` when you don't need these resources.
provider "aws" {
region = "eu-west-1"
}
##############################################################
# Data sources to get VPC, subnets and security group details
##############################################################
data "aws_vpc" "default" {
default = true
}
data "aws_subnet_ids" "all" {
vpc_id = "${data.aws_vpc.default.id}"
}
data "aws_security_group" "default" {
vpc_id = "${data.aws_vpc.default.id}"
name = "default"
}
#####
# DB
#####
module "db" {
source = "../../"
identifier = "demodb"
engine = "mysql"
engine_version = "5.7.11"
instance_class = "db.t2.large"
allocated_storage = 5
name = "demodb"
username = "user"
password = "YourPwdShouldBeLongAndSecure!"
port = "3306"
vpc_security_group_ids = ["${data.aws_security_group.default.id}"]
maintenance_window = "Mon:00:00-Mon:03:00"
backup_window = "03:00-06:00"
backup_retention_period = 0 // disable backups to create DB faster
tags = {
Owner = "user"
Environment = "dev"
}
# DB subnet group
subnet_ids = ["${data.aws_subnet_ids.all.ids}"]
# DB parameter group
family = "mysql5.7"
}
# DB instance
output "this_db_instance_address" {
description = "The address of the RDS instance"
value = "${module.db.this_db_instance_address}"
}
output "this_db_instance_arn" {
description = "The ARN of the RDS instance"
value = "${module.db.this_db_instance_arn}"
}
output "this_db_instance_availability_zone" {
description = "The availability zone of the RDS instance"
value = "${module.db.this_db_instance_availability_zone}"
}
output "this_db_instance_endpoint" {
description = "The connection endpoint"
value = "${module.db.this_db_instance_endpoint}"
}
output "this_db_instance_hosted_zone_id" {
description = "The canonical hosted zone ID of the DB instance (to be used in a Route 53 Alias record)"
value = "${module.db.this_db_instance_hosted_zone_id}"
}
output "this_db_instance_id" {
description = "The RDS instance ID"
value = "${module.db.this_db_instance_id}"
}
output "this_db_instance_resource_id" {
description = "The RDS Resource ID of this instance"
value = "${module.db.this_db_instance_resource_id}"
}
output "this_db_instance_status" {
description = "The RDS instance status"
value = "${module.db.this_db_instance_status}"
}
output "this_db_instance_name" {
description = "The database name"
value = "${module.db.this_db_instance_name}"
}
output "this_db_instance_username" {
description = "The master username for the database"
value = "${module.db.this_db_instance_username}"
}
output "this_db_instance_password" {
description = "The database password (this password may be old, because Terraform doesn't track it after initial creation)"
value = "${module.db.this_db_instance_password}"
}
output "this_db_instance_port" {
description = "The database port"
value = "${module.db.this_db_instance_port}"
}
# DB subnet group
output "this_db_subnet_group_id" {
description = "The db subnet group name"
value = "${module.db.this_db_subnet_group_id}"
}
output "this_db_subnet_group_arn" {
description = "The ARN of the db subnet group"
value = "${module.db.this_db_subnet_group_arn}"
}
# DB parameter group
output "this_db_parameter_group_id" {
description = "The db parameter group id"
value = "${module.db.this_db_parameter_group_id}"
}
output "this_db_parameter_group_arn" {
description = "The ARN of the db parameter group"
value = "${module.db.this_db_parameter_group_arn}"
}
##################
# DB subnet group
##################
module "db_subnet_group" {
source = "./modules/db_subnet_group"
identifier = "${var.identifier}"
name_prefix = "${var.identifier}-"
subnet_ids = ["${var.subnet_ids}"]
tags = "${var.tags}"
}
##################################################
# DB subnet group with all subnets in default VPC
##################################################
module "db_parameter_group" {
source = "./modules/db_parameter_group"
identifier = "${var.identifier}"
name_prefix = "${var.identifier}-"
family = "${var.family}"
// parameter = ["${var.parameters}"]
tags = "${var.tags}"
}
##############
# DB instance
##############
module "db_instance" {
source = "./modules/db_instance"
identifier = "${var.identifier}"
engine = "${var.engine}"
engine_version = "${var.engine_version}"
instance_class = "${var.instance_class}"
allocated_storage = "${var.allocated_storage}"
storage_type = "${var.storage_type}"
name = "${var.name}"
username = "${var.username}"
password = "${var.password}"
port = "${var.port}"
vpc_security_group_ids = ["${var.vpc_security_group_ids}"]
db_subnet_group_name = "${module.db_subnet_group.this_db_subnet_group_id}"
parameter_group_name = "${module.db_parameter_group.this_db_parameter_group_id}"
multi_az = "${var.multi_az}"
iops = "${var.iops}"
publicly_accessible = "${var.publicly_accessible}"
allow_major_version_upgrade = "${var.allow_major_version_upgrade}"
auto_minor_version_upgrade = "${var.auto_minor_version_upgrade}"
apply_immediately = "${var.apply_immediately}"
maintenance_window = "${var.maintenance_window}"
skip_final_snapshot = "${var.skip_final_snapshot}"
copy_tags_to_snapshot = "${var.copy_tags_to_snapshot}"
backup_retention_period = "${var.backup_retention_period}"
backup_window = "${var.backup_window}"
tags = "${var.tags}"
}
##############
# DB instance
##############
resource "aws_db_instance" "this" {
identifier = "${var.identifier}"
engine = "${var.engine}"
engine_version = "${var.engine_version}"
instance_class = "${var.instance_class}"
allocated_storage = "${var.allocated_storage}"
storage_type = "${var.storage_type}"
name = "${var.name}"
username = "${var.username}"
password = "${var.password}"
port = "${var.port}"
vpc_security_group_ids = ["${var.vpc_security_group_ids}"]
db_subnet_group_name = "${var.db_subnet_group_name}"
parameter_group_name = "${var.parameter_group_name}"
multi_az = "${var.multi_az}"
iops = "${var.iops}"
publicly_accessible = "${var.publicly_accessible}"
allow_major_version_upgrade = "${var.allow_major_version_upgrade}"
auto_minor_version_upgrade = "${var.auto_minor_version_upgrade}"
apply_immediately = "${var.apply_immediately}"
maintenance_window = "${var.maintenance_window}"
skip_final_snapshot = "${var.skip_final_snapshot}"
copy_tags_to_snapshot = "${var.copy_tags_to_snapshot}"
backup_retention_period = "${var.backup_retention_period}"
backup_window = "${var.backup_window}"
tags = "${merge(var.tags, map("Name", format("%s", var.identifier)))}"
}
# DB instance
output "this_db_instance_address" {
description = "The address of the RDS instance"
value = "${aws_db_instance.this.address}"
}
output "this_db_instance_arn" {
description = "The ARN of the RDS instance"
value = "${aws_db_instance.this.arn}"
}
output "this_db_instance_availability_zone" {
description = "The availability zone of the RDS instance"
value = "${aws_db_instance.this.availability_zone}"
}
output "this_db_instance_endpoint" {
description = "The connection endpoint"
value = "${aws_db_instance.this.endpoint}"
}
output "this_db_instance_hosted_zone_id" {
description = "The canonical hosted zone ID of the DB instance (to be used in a Route 53 Alias record)"
value = "${aws_db_instance.this.hosted_zone_id}"
}
output "this_db_instance_id" {
description = "The RDS instance ID"
value = "${aws_db_instance.this.id}"
}
output "this_db_instance_resource_id" {
description = "The RDS Resource ID of this instance"
value = "${aws_db_instance.this.resource_id}"
}
output "this_db_instance_status" {
description = "The RDS instance status"
value = "${aws_db_instance.this.status}"
}
output "this_db_instance_name" {
description = "The database name"
value = "${aws_db_instance.this.name}"
}
output "this_db_instance_username" {
description = "The master username for the database"
value = "${aws_db_instance.this.username}"
}
output "this_db_instance_password" {
description = "The database password (this password may be old, because Terraform doesn't track it after initial creation)"
value = "${var.password}"
}
output "this_db_instance_port" {
description = "The database port"
value = "${aws_db_instance.this.port}"
}
variable "identifier" {
description = "The name of the RDS instance, if omitted, Terraform will assign a random, unique identifier"
}
variable "allocated_storage" {
description = "The allocated storage in gigabytes"
}
variable "storage_type" {
description = "One of 'standard' (magnetic), 'gp2' (general purpose SSD), or 'io1' (provisioned IOPS SSD). The default is 'io1' if iops is specified, 'standard' if not. Note that this behaviour is different from the AWS web console, where the default is 'gp2'."
default = "gp2"
}
variable "engine" {
description = "The database engine to use"
}
variable "engine_version" {
description = "The engine version to use"
}
variable "instance_class" {
description = "The instance type of the RDS instance"
}
variable "name" {
description = "The DB name to create. If omitted, no database is created initially"
}
variable "username" {
description = "Username for the master DB user"
}
variable "password" {
description = "Password for the master DB user. Note that this may show up in logs, and it will be stored in the state file"
}
variable "port" {
description = "The port on which the DB accepts connections"
}
variable "vpc_security_group_ids" {
description = "List of VPC security groups to associate"
default = []
}
variable "db_subnet_group_name" {
description = "Name of DB subnet group. DB instance will be created in the VPC associated with the DB subnet group. If unspecified, will be created in the default VPC"
default = ""
}
variable "parameter_group_name" {
description = "Name of the DB parameter group to associate"
default = ""
}
variable "multi_az" {
description = "Specifies if the RDS instance is multi-AZ"
default = false
}
variable "iops" {
description = "The amount of provisioned IOPS. Setting this implies a storage_type of 'io1'"
default = 0
}
variable "publicly_accessible" {
description = "Bool to control if instance is publicly accessible"
default = false
}
variable "allow_major_version_upgrade" {
description = "Indicates that major version upgrades are allowed. Changing this parameter does not result in an outage and the change is asynchronously applied as soon as possible"
default = false
}
variable "auto_minor_version_upgrade" {
description = "Indicates that minor engine upgrades will be applied automatically to the DB instance during the maintenance window"
default = true
}
variable "apply_immediately" {
description = "Specifies whether any database modifications are applied immediately, or during the next maintenance window"
default = false
}
variable "maintenance_window" {
description = "The window to perform maintenance in. Syntax: 'ddd:hh24:mi-ddd:hh24:mi'. Eg: 'Mon:00:00-Mon:03:00'"
}
variable "skip_final_snapshot" {
description = "Determines whether a final DB snapshot is created before the DB instance is deleted. If true is specified, no DBSnapshot is created. If false is specified, a DB snapshot is created before the DB instance is deleted, using the value from final_snapshot_identifier"
default = false
}
variable "copy_tags_to_snapshot" {
description = "On delete, copy all Instance tags to the final snapshot (if final_snapshot_identifier is specified)"
default = false
}
variable "backup_retention_period" {
description = "The days to retain backups for"
default = 1
}
variable "backup_window" {
description = "The daily time range (in UTC) during which automated backups are created if they are enabled. Example: '09:46-10:16'. Must not overlap with maintenance_window"
}
variable "tags" {
description = "A mapping of tags to assign to all resources"
default = {}
}
#####################
# DB parameter group
#####################
resource "aws_db_parameter_group" "this" {
count = "${var.count}"
name_prefix = "${var.name_prefix}"
description = "Database parameter group for ${var.identifier}"
family = "${var.family}"
// @todo: implement this
// parameter = ["${var.parameters}"]
// parameter = [
// {
// name = "character_set_server"
// value = "utf8"
// },
// {
// name = "character_set_client"
// value = "utf18"
// },
// ]
parameter {
name = "character_set_server"
value = "utf8"
}
parameter {
name = "character_set_client"
value = "utf8"
}
tags = "${merge(var.tags, map("Name", format("%s", var.identifier)))}"
}
# DB parameter group
output "this_db_parameter_group_id" {
description = "The db parameter group id"
value = "${aws_db_parameter_group.this.id}"
}
output "this_db_parameter_group_arn" {
description = "The ARN of the db parameter group"
value = "${aws_db_parameter_group.this.arn}"
}
variable "count" {
description = "Whether to create this resource or not?"
default = 1
}
variable "name_prefix" {
description = "Creates a unique name beginning with the specified prefix"
}
variable "identifier" {
description = "The identifier of the resource"
}
variable "family" {
description = "The family of the DB parameter group"
}
variable "parameters" {
type = "map"
description = "A map of lists of DB parameters to apply"
default = {}
}
variable "tags" {
type = "map"
description = "A mapping of tags to assign to the resource"
default = {}
}
##################
# DB subnet group
##################
resource "aws_db_subnet_group" "this" {
count = "${var.count}"
name_prefix = "${var.name_prefix}"
description = "Database subnet group for ${var.identifier}"
subnet_ids = ["${var.subnet_ids}"]
tags = "${merge(var.tags, map("Name", format("%s", var.identifier)))}"
}
# DB subnet group
output "this_db_subnet_group_id" {
description = "The db subnet group name"
value = "${var.count == 0 ? "" : aws_db_subnet_group.this.id}"
}
output "this_db_subnet_group_arn" {
description = "The ARN of the db subnet group"
value = "${aws_db_subnet_group.this.arn}"
}
variable "count" {
description = "Whether to create this resource or not?"
default = 1
}
variable "name_prefix" {
description = "Creates a unique name beginning with the specified prefix"
}
variable "identifier" {
description = "The identifier of the resource"
}
variable "subnet_ids" {
type = "list"
description = "A list of VPC subnet IDs"
default = []
}
variable "tags" {
type = "map"
description = "A mapping of tags to assign to the resource"
default = {}
}
# DB instance
output "this_db_instance_address" {
description = "The address of the RDS instance"
value = "${module.db_instance.this_db_instance_address}"
}
output "this_db_instance_arn" {
description = "The ARN of the RDS instance"
value = "${module.db_instance.this_db_instance_arn}"
}
output "this_db_instance_availability_zone" {
description = "The availability zone of the RDS instance"
value = "${module.db_instance.this_db_instance_availability_zone}"
}
output "this_db_instance_endpoint" {
description = "The connection endpoint"
value = "${module.db_instance.this_db_instance_endpoint}"
}
output "this_db_instance_hosted_zone_id" {
description = "The canonical hosted zone ID of the DB instance (to be used in a Route 53 Alias record)"
value = "${module.db_instance.this_db_instance_hosted_zone_id}"
}
output "this_db_instance_id" {
description = "The RDS instance ID"
value = "${module.db_instance.this_db_instance_id}"
}
output "this_db_instance_resource_id" {
description = "The RDS Resource ID of this instance"
value = "${module.db_instance.this_db_instance_resource_id}"
}
output "this_db_instance_status" {
description = "The RDS instance status"
value = "${module.db_instance.this_db_instance_status}"
}
output "this_db_instance_name" {
description = "The database name"
value = "${module.db_instance.this_db_instance_name}"
}
output "this_db_instance_username" {
description = "The master username for the database"
value = "${module.db_instance.this_db_instance_username}"
}
output "this_db_instance_password" {
description = "The database password (this password may be old, because Terraform doesn't track it after initial creation)"
value = "${var.password}"
}
output "this_db_instance_port" {
description = "The database port"
value = "${module.db_instance.this_db_instance_port}"
}
# DB subnet group
output "this_db_subnet_group_id" {
description = "The db subnet group name"
value = "${module.db_subnet_group.this_db_subnet_group_id}"
}
output "this_db_subnet_group_arn" {
description = "The ARN of the db subnet group"
value = "${module.db_subnet_group.this_db_subnet_group_arn}"
}
# DB parameter group
output "this_db_parameter_group_id" {
description = "The db parameter group id"
value = "${module.db_parameter_group.this_db_parameter_group_id}"
}
output "this_db_parameter_group_arn" {
description = "The ARN of the db parameter group"
value = "${module.db_parameter_group.this_db_parameter_group_arn}"
}
variable "identifier" {
description = "The name of the RDS instance, if omitted, Terraform will assign a random, unique identifier"
}
variable "allocated_storage" {
description = "The allocated storage in gigabytes"
}
variable "storage_type" {
description = "One of 'standard' (magnetic), 'gp2' (general purpose SSD), or 'io1' (provisioned IOPS SSD). The default is 'io1' if iops is specified, 'standard' if not. Note that this behaviour is different from the AWS web console, where the default is 'gp2'."
default = "gp2"
}
variable "engine" {
description = "The database engine to use"
}
variable "engine_version" {
description = "The engine version to use"
}
variable "instance_class" {
description = "The instance type of the RDS instance"
}
variable "name" {
description = "The DB name to create. If omitted, no database is created initially"
}
variable "username" {
description = "Username for the master DB user"
}
variable "password" {
description = "Password for the master DB user. Note that this may show up in logs, and it will be stored in the state file"
}
variable "port" {
description = "The port on which the DB accepts connections"
}
variable "vpc_security_group_ids" {
description = "List of VPC security groups to associate"
default = []
}
//variable "db_subnet_group_name" {
// description = "Name of DB subnet group. DB instance will be created in the VPC associated with the DB subnet group. If unspecified, will be created in the default VPC"
// default = ""
//}
//
//variable "parameter_group_name" {
// description = "Name of the DB parameter group to associate"
// default = ""
//}
variable "multi_az" {
description = "Specifies if the RDS instance is multi-AZ"
default = false
}
variable "iops" {
description = "The amount of provisioned IOPS. Setting this implies a storage_type of 'io1'"
default = 0
}
variable "publicly_accessible" {
description = "Bool to control if instance is publicly accessible"
default = false
}
variable "allow_major_version_upgrade" {
description = "Indicates that major version upgrades are allowed. Changing this parameter does not result in an outage and the change is asynchronously applied as soon as possible"
default = false
}
variable "auto_minor_version_upgrade" {
description = "Indicates that minor engine upgrades will be applied automatically to the DB instance during the maintenance window"
default = true
}
variable "apply_immediately" {
description = "Specifies whether any database modifications are applied immediately, or during the next maintenance window"
default = false
}
variable "maintenance_window" {
description = "The window to perform maintenance in. Syntax: 'ddd:hh24:mi-ddd:hh24:mi'. Eg: 'Mon:00:00-Mon:03:00'"
}
variable "skip_final_snapshot" {
description = "Determines whether a final DB snapshot is created before the DB instance is deleted. If true is specified, no DBSnapshot is created. If false is specified, a DB snapshot is created before the DB instance is deleted, using the value from final_snapshot_identifier"
default = true
}
variable "copy_tags_to_snapshot" {
description = "On delete, copy all Instance tags to the final snapshot (if final_snapshot_identifier is specified)"
default = false
}
variable "backup_retention_period" {
description = "The days to retain backups for"
default = 1
}
variable "backup_window" {
description = "The daily time range (in UTC) during which automated backups are created if they are enabled. Example: '09:46-10:16'. Must not overlap with maintenance_window"
}
variable "tags" {
description = "A mapping of tags to assign to all resources"
default = {}
}
# DB subnet group
variable "subnet_ids" {
type = "list"
description = "A list of VPC subnet IDs"
default = []
}
# DB parameter group
variable "family" {
description = "The family of the DB parameter group"
default = ""
}
variable "parameters" {
type = "map"
description = "A map of lists of DB parameters to apply"
default = {}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment