devops@control:~/aws-example$ cat varible.tf
variable "aws_region" {
description = "AWS region"
type = string
default = "ap-northeast-1"
}
#variable "ami_image" {
# description = "Ubuntu 20.04 LTS Image"
# type = map(string)
# default = {
# ap-northeast-1 = "ami-09ff2b6ef00accc2e"
# ap-northeast-2 = "ami-0b329fb1f17558744"
# }
#}
variable "instance_type" {
description = "EC2 instance type"
type = string
default = "t3.micro"
}
variable "project_name" {
description = "Name of the project"
type = string
default = "my_project"
}
variable "environment" {
description = "Name of the environment"
type = string
default = "dev"
}
variable "vpc_name" {
description = "Name of VPC"
type = string
default = "my-vpc"
}
variable "vpc_cidr" {
description = "CIDR block for VPC"
type = string
default = "10.0.0.0/16"
}
variable "vpc_azs" {
description = "Availabilty zones for VPC"
type = list(string)
default = ["ap-northeast-2a", "ap-northeast-2c"]
}
variable "vpc_private_subnets" {
description = "Private subnets for VPC"
type = list(string)
default = ["10.0.101.0/24", "10.0.102.0/24"]
}
variable "vpc_public_subnets" {
description = "Public subnets for VPC"
type = list(string)
default = ["10.0.1.0/24", "10.0.2.0/24"]
}
variable "vpc_enable_nat_gateway" {
description = "Enable NAT gateway for VPC"
type = bool
default = false
}
variable "instances_per_subnet" {
description = "Number of EC2 instances in each private subnet"
type = number
default = 2
}
devops@control:~/aws-example$ vi main.tf
devops@control:~/aws-example$ terraform fmt
devops@control:~/aws-example$ cat main.tf
module "my_vpc" {
source = "terraform-aws-modules/vpc/aws"
name = var.vpc_name
cidr = var.vpc_cidr
azs = var.vpc_azs
private_subnets = var.vpc_private_subnets
public_subnets = var.vpc_public_subnets
enable_nat_gateway = var.vpc_enable_nat_gateway
tags = local.common_tags
}
resource "aws_instance" "my_instance" {
count = var.instances_per_subnet * length(module.my_vpc.public_subnets)
ami = data.aws_ami.amazon_linux.id
instance_type = var.instance_type
vpc_security_group_ids = [aws_security_group.my_dh_web.id]
key_name = aws_key_pair.my_sshkey.key_name
subnet_id = module.my_vpc.public_subnets[count.index % length(module.my_vpc.public_subnets)]
tags = local.common_tags
}
resource "aws_key_pair" "my_sshkey" {
key_name = "my_sshkey"
public_key = file("./my_sshkey.pub")
}
resource "aws_eip" "my_eip" {
count = var.instances_per_subnet * length(module.my_vpc.public_subnets)
vpc = true
instance = aws_instance.my_instance[count.index].id
}
devops@control:~/aws-example$ terraform apply -auto-approve
'클라우드 > 테라폼(Terraform)' 카테고리의 다른 글
테라폼 Terraform cloud 원격 상태 저장 2 (0) | 2021.08.06 |
---|---|
테라폼 Terraform cloud 원격 상태 저장 1 (1) | 2021.08.06 |
테라폼 Terraform 모듈 (0) | 2021.08.06 |
테라폼 Terraform 구성 관리 (0) | 2021.08.06 |
테라폼 Terraform 데이터 소스 (0) | 2021.08.05 |