// ec2-instance.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
}
}
}
provider "aws" {
profile = "js" # AWS Credentials Profile configured on your local desktop terminal $HOME/.aws/credentials
region = "ap-northeast-2"
}
# Resource Block
resource "aws_instance" "ec2demo" {
ami = "ami-0195322846474ddb9" # 리소스 AMI ID
instance_type = "t2.micro"
}
terraform {
required_version = "~> 1.0" # 테라폼 버전 0.14 이거나 이상 0.15, 0.16 etc and < 1.xx
/*
terraform aws provider
LocalName(aws)은 argument로 = {} 사용 필요
정의한 LocalName과 Provider Block의 provider는 일치해야 한다.
*/
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.0"
}
}
}
provider "aws" {
# profile or access key 사용 가능
profile = "js"
region = "ap-northeast-2"
}
/*
aws_instance -> Resource Type
myec2localname -> Resource Local Name : 같은 테라폼 모듈에서 참조되며 unique 해야함
*/
resource "aws_instance" "myec2localname" {
ami = "ami-0195322846474ddb9"
instance_type = "t3.micro"
user_data = file("${path.module}/app1-install.sh") // file() 모듈 내 파일 참조
tags = {
// 인스턴스 명 반영
"Name" = "EC2 Demo"
}
}
variables.tf
# Input Variables
# AWS Region
variable "aws_region" {
description = "Region in which AWS Resources to be created"
type = string
default = "ap-northeast-2"
}
variable "aws_profile" {
description = "Profile for AWS Account"
type = string
default = "js"
}
# AWS EC2 Instance Type
variable "instance_type" {
description = "EC2 Instnace Type"
type = string
default = "t3.micro"
}
# AWS EC2 Instance Key Pair
variable "instance_keypair" {
description = "AWS EC2 Key Pair that need to be associated with EC2 Instance"
type = string
default = "terraform-key"
}
datasource.tf
# Get latest AMI ID for Amazon Linux2 OS
# datasource는 data.점으로 참조 가능 data.aws_ami.amzlinux2.id (id는) 테라폼 attribute
data "aws_ami" "amzlinux2" {
# 최신 AMI 사용
most_recent = true
owners = ["amazon"]
filter {
name = "name"
# AMI NAME
values = ["amzn2-ami-hvm-*-gp2"]
}
filter {
name = "root-device-type"
values = ["ebs"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
filter {
name = "architecture"
values = ["x86_64"]
}
}
# EC2 Instance Public IP
output "instance_publicip" {
description = "EC2 Instance Public IP"
value = aws_instance.myec2vm.public_ip
}
# EC2 Instance Public DNS
output "instance_publicdns" {
description = "EC2 Instance Public DNS"
value = aws_instance.myec2vm.public_dns
}
ec2instance.tf
# EC2 Instance
resource "aws_instance" "myec2vm" {
ami = data.aws_ami.amzlinux2.id
instance_type = var.instance_type
user_data = file("${path.module}/app1-install.sh")
key_name = var.instance_keypair
# ssh & http 보안그룹 설정
vpc_security_group_ids = [ aws_security_group.vpc-terraform-ssh.id, aws_security_group.vpc-terraform-web.id ]
# 생성한 EC2 Name 설정
tags = {
"Name" = "EC2 Demo 2"
}
}
variables.tf
# AWS EC2 Instance Type - List
variable "instance_type_list" {
description = "EC2 Instance Type"
type = list(string)
default = ["t3.micro", "t3.small", "t3.large"]
}
# AWS EC2 Instance Type - Map
variable "instance_type_map" {
description = "EC2 Instance Type"
type = map(string)
default = {
"dev" = "t3.micro"
"qa" = "t3.small"
"prod" = "t3.large"
}
}
ec2instance.tf
instance_type = var.instance_type_list[1] # For List
#nstance_type = var.instance_type_map["prod"] # For Map
ec2instance.tf
resource "aws_instance" "myec2vm" {
ami = data.aws_ami.amzlinux2.id
# instance_type = var.instance_type
# LIST와 Map 사용
instance_type = var.instance_type_list[1] # For List
#nstance_type = var.instance_type_map["prod"] # For Map
user_data = file("${path.module}/app1-install.sh")
key_name = var.instance_keypair
vpc_security_group_ids = [ aws_security_group.vpc-ssh.id, aws_security_group.vpc-web.id ]
# EC2 생성 갯수
count = 2
tags = {
# count index는 0부터 시작
# Count-Demo-0 , Count-Demo-1로 ec2 Name 설정
"Name" = "Count-Demo-${count.index}"
}
}
output.tf
# 여러 인스턴스 생성 시 (count 적용) loop를 통해 output 출력
# Output - For Loop with List
output "for_output_list" {
description = "For Loop with List"
value = [for instance in aws_instance.myec2vm: instance.public_dns]
}
# Output - For Loop with Map
# id는 key pulic_dns는 value
output "for_output_map1" {
description = "For Loop with Map"
value = {for instance in aws_instance.myec2vm: instance.id => instance.public_dns}
}
# Output - For Loop with Map Advanced
# c ~ count
output "for_output_map2" {
description = "For Loop with Map - Advanced"
value = {for c, instance in aws_instance.myec2vm: c => instance.public_dns}
}
# Output Latest Generalized Splat Operator - Returns the List
output "latest_splat_instance_publicdns" {
description = "Generalized latest Splat Operator"
value = aws_instance.myec2vm[*].public_dns
}
결과 예시)
참고 - Terraform on AWS EKS Kubernetes IaC / Kalyan Reddy Daida