테라폼 Terraform 모듈
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 = "dev"["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
}
devops@control:~/aws-example$ cat security-group.tf
resource "aws_security_group" "my_dh_web" {
name = "allow-web"
vpc_id = module.my_vpc.vpc_id
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
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" {
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[0]
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" {
vpc = true
instance = aws_instance.my_instance.id
}