module 에서 여러 요소들이 들어간 리소스 생성
사용하고자 하는 요소들을 output 함수로 정의
다른 리소스를 생성하는 과정에서 output 의 value를 명시
가져오고자 하는 요소
vpc.tf
*****
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "3.19.0"
name = "terraform-vpc"
cidr = "10.10.0.0/16"
azs = ["ap-northeast-2a","ap-northeast-2c"]
private_subnets = ["10.10.1.0/24", "10.10.2.0/24"]
public_subnets = ["10.10.11.0/24", "10.10.12.0/24"]
enable_nat_gateway = true
one_nat_gateway_per_az = true
enable_dns_hostnames = true
public_subnet_tags = {
"kubernetes.io/cluster/${local.cluster_name}" = "shared"
"kubernetes.io/role/elb" = 1
}
private_subnet_tags = {
"kubernetes.io/cluster/${local.cluster_name}" = "shared"
"kubernetes.io/role/internal-elb" = 1
}
}
output.tf
***
output "vpc_cidr_block" {
description = "vpc_cider_block"
value = module.vpc.vpc_cidr_block
}
output "public_subnets_ids" {
description = "public_subnets"
value = module.vpc.public_subnets
}
security.tf
***
resource "aws_security_group" "bastion_sg" {
name = "bastion_sg"
description = "allow all"
vpc_id = module.vpc.vpc_id
ingress {
description = "allow all !! eks"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = [module.vpc.vpc_cidr_block] ## [] 안에 명시
}
egress = [
{
description = "allow all"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = []
aws_security_groups = []
prefix_list_ids = []
self = false
security_groups = []
}
]
tags = {
Name = "allow all"
}
}
resource "aws_instance" "bastion" {
ami = "ami-0ff56409a6e8ea2a0"
availability_zone = "ap-northeast-2a"
instance_type = "t2.micro"
key_name = "rapa"
vpc_security_group_ids = [aws_security_group.bastion_sg.id]
subnet_id = module.vpc.public_subnets[0]
## 첫번째 인덱스(퍼블릭서브넷들의 첫번째) 를 가져오기 위해 [0] 명시
associate_public_ip_address = true
tags = {
Name = "bastion"
}
}