반응형
테라폼 인스턴스(private) 생성
메인 모듈
$ vim main.tf
#####레디스 서버
module "redisserver" {
source = "../../../modules/ec2/instance/private/"
prefix = module.vpc.out_prefix
suffix = module.vpc.out_suffix
env = module.vpc.out_env
region = module.vpc.out_region
instance_count = 1
name = "redis1"
group_name = "redis"
#aws_amis = "ami-0742b4e673072066f"
instance_type = "t3a.medium"
disk_size = "8"
key_name = "keypem"
subnet_id = module.vpc.out_private
vpc_security_group_ids = ["sg-013dc", "sg-043cf"]
#associate_public_ip_address = true
customer_tags = {
Environment = "env-prod",
CreateUser = "terraform",
Owner = "sangchul",
Project = "blog",
Role = "ec2",
Service = "instance",
CreatedDate = timestamp()
}
}
인스턴스 모듈
인스턴스, EIP 리소스 생성
#####인스턴스 생성
resource "aws_instance" "this" {
count = var.instance_count
instance_type = var.instance_type
ami = lookup(var.aws_amis, var.region)
key_name = var.key_name
subnet_id = element(var.subnet_id, count.index)
vpc_security_group_ids = var.vpc_security_group_ids
associate_public_ip_address = var.associate_public_ip_address
monitoring = true
root_block_device {
volume_size = var.disk_size
}
# connection {
# host = self.private_ip
# #host = self.public_ip
# type = "ssh"
# user = "ec2-user"
# private_key = file("~/aws-key/keppem.pem")
# #key_file = var.ssh_key_file
# }
# provisioner "remote-exec" {
# inline = [
# "sudo yum update -y",
# "sudo yum install -y httpd",
# "sudo service httpd start",
# ]
# }
lifecycle {
create_before_destroy = true
}
tags = merge(
var.customer_tags, {
Name = "${var.name}${count.index+1}",
Group = "group-${var.group_name}"
}
)
}
#####EIP 생성
resource "aws_eip" "this" {
count = var.instance_count
instance = element(aws_instance.this.*.id, count.index)
vpc = true
provisioner "remote-exec" {
connection {
host = self.private_ip
#host = self.public_ip
type = "ssh"
user = "ec2-user"
private_key = file("~/aws-key/keppem.pem")
#key_file = var.ssh_key_file
}
inline = [
"sudo yum update -y",
"sudo yum install -y httpd",
"sudo service httpd start",
]
}
tags = merge(
var.customer_tags, {
Name = "${var.name}${count.index+1}",
Service = "eip"
}
)
}
$ vim vars.tf
variable "env" {}
variable "prefix" {}
variable "suffix" {}
variable "region" {}
variable "private_key_path" {}
variable "aws_amis" {
type = map
default = {
"ap-southeast-1" = "ami-03ca998611da0fe12"
"ap-northeast-2" = "ami-09282971cf2faa4c9"
"us-east-1" = "ami-0742b4e673072066f"
}
}
variable "disk_size" {}
variable "instance_count" {}
variable "name" {}
variable "group_name" {}
variable "instance_type" {}
variable "key_name" {}
variable "subnet_id" {}
variable "vpc_security_group_ids" {}
variable "associate_public_ip_address" {}
variable "customer_tags" {}
** 외부 통신을 위해 라우팅 테이블 추가 설정이 필요함
728x90
반응형
'퍼블릭 클라우드' 카테고리의 다른 글
[Ansible] ansible 인벤토리 설정 파일 (0) | 2021.05.07 |
---|---|
[Ansible] MacOS에 Ansible을 설치하기 (0) | 2021.05.07 |
[Terraform] 테라폼 인스턴스(public) 생성 (0) | 2021.04.30 |
[Terraform] 테라폼 리소스 태그 지정(tags) -2 (0) | 2021.04.27 |
[Terraform] 테라폼 리소스 태그 지정(tags) -1 (0) | 2021.04.27 |