We are planning to use Terraform for provisioning EC2 instance which will host Oracle DB. While we are able to achieve this, there are some questions for which I am searching for suggestions / feedback's.
Below is the directory structure we are planning:
|aws
|Environments
|STAGING
|Modules
|Compute
|Networking
|PRODUCTION
|Modules
|Compute
|Networking
Questions:
We are able to create EC2 instances using existing VPC. As VPC is not something that will be owned by my team. Is it enough to keep VPC ID, SUBNET ID, etc as variables in EC2 resource or is there a best practice to keep them in modules for existing VPC ? Overall, our tf codes are not going to manage the VPC's, just use what is being giving to us, but want to make it dynamic so that changes are simple, yet DRY
We are planning for Continuous Development, meaning, developers / requester will have the ability to request for a new ec2 instance with certain version of database. Right now, we are installing db with bootstrap scripts. Though this works as expected. Increasing count for more than 1 instance works, but what if the configuration slightly changes.
For example:
1) If I need more capacity on root volume for the new instance, changing the existing resource shows a plan that will destroy existing resource and recreate. How can we achieve this ? Is duplicating the aws_instance resource, the only way when the configuration changes for new EC2 instance?
2) If new instances launch as t2.large(Instance type passed as variable) while old ones run on t2.micro, will it recreate old instances too if I just increase the count
Also, is there a way to automate this process i.e. append something like a template for new EC2 instance to existing .tf file.
What do we have now
Main.tf
provider "aws" {
access_key = "${var.access_key}"
secret_key = "${var.secret_key}"
region = "${var.region}"
}
resource "aws_instance" "oracle_ec2" {
ami = "${lookup(var.ami, var.region)}"
instance_type = "${var.instance_type}"
key_name = "${var.key_name}"
subnet_id = "${var.subnet_id}"
vpc_security_group_ids = ["${aws_security_group.oracle_sg_new.id}"]
root_block_device {
volume_type = "gp2"
volume_size = 10
delete_on_termination = true
}
ebs_block_device {
device_name = "/dev/xvdb"
volume_type = "gp2"
volume_size = 200
delete_on_termination = true
}
count = "${var.instance_count}"
tags {
Name = "Ora"
}
}