Terraform EC2 생성
1) Provider 설정
provider "aws" {
region = "ap-northeast-2"
}
2) EC2 AMI Instacne 및 type 설정
- AMI 설정
- Type 설정
resource "aws_instance" "web-2a"{
ami = "ami-0a0de518b1fc4524c"
instance_type = "t2.micro"
}
3) Terraform AWS 적용
1.1 terraform Project 초기화
# terraform init
1.2 terraform Project 계획 확인
- 작성한 코드를 실제 AWS에 생성이 가능한지 확인하는 작업
# terraform plan
1.3 terraform Project 리소스 적용
- 작성한 코드를 실제 AWS에 리소스 적용
# terraform apply
* EC AMI , Instance_type 동일 생성
4) EC2 Instance 상세 설정
1.1 EC2 Instance Tag
- EC2 Instance Name Tag 설정
resource "aws_instance" "web-2a"{
ami = "ami-0a0de518b1fc4524c"
instance_type = "t2.micro"
tags = {
Name = "web-2a"
}
}
# terraform plan
# terrafomr apply
1.3 EC2 Security-Group
https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group
- EC2 Security-Group 설정시 VPC 변수 생성
# VPC_ID 정보 #
variable "vpc_id"{
default = "vpc-6237a409"
}
- EC2 Security-Group 설정
VPC 정보 참조 : var.vpc_id
#AWS_Security_Group#
resource "aws_security_group" "allow_web" {
name = "allow_web"
description = "Allow web inbound traffic"
vpc_id = var.vpc_id
ingress {
description = "TLS from VPC"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "allow_web"
}
}
1.4 EC2 Subnet 설정
- EC2 Subnet_Iid 설정(2a,2c zone)
- var.subnet_id[0] : subnet_id 첫번째 값
variable "subnet_id"{
default = ["subnet-79d65a12" , "subnet-5392621c"]
}
- var.subnet_id[0] : subnet_id 첫번째 값
resource "aws_instance" "web-2a"{
ami = "ami-0a0de518b1fc4524c"
instance_type = "t2.micro"
vpc_security_group_ids = [aws_security_group.allow_web]
availability_zone = "us-west-2a"
subnet_id =var.subnet_id[0]
tags = {
Name = "web-2a"
}
}
1.5 EC2 EBS 볼륨 확장
- EBS 30GB 확장
resource "aws_instance" "web-2a"{
ami = "ami-0a0de518b1fc4524c"
instance_type = "t2.micro"
vpc_security_group_ids = [aws_security_group.allow_web.id]
availability_zone = "ap-northeast-2a"
subnet_id =var.subnet_id[0]
# EC2 EBS Volumes Increament
root_block_device {
volume_size = 30
volume_type = "gp2"
}
tags = {
Name = "web-2a"
}
}
1.6 EC2 User_DATA 설정
- User_Data Script : EC2 Instance 설치 패키시 설치
> file 생성(install.sh)
#!/bin/bash
yum -y update
yum -y install httpd
systemctlctl start httpd
systemctlctl enable httpd
curl http://169.254.169.254/latest/meta-data/instance-id -o /var/www/html/index.html
resource "aws_instance" "web-2a"{
ami = "ami-0a0de518b1fc4524c"
instance_type = "t2.micro"
vpc_security_group_ids = [aws_security_group.allow_web.id]
availability_zone = "ap-northeast-2a"
subnet_id =var.subnet_id[0]
user_data = file("./install.sh")
tags = {
Name = "web-2a"
}
}
'Terrform' 카테고리의 다른 글
[terraform] terraform module( VPC) 사용 (0) | 2024.11.13 |
---|---|
AWS Terraform tfstate 파일 원격 관리(S3, DynamoDB) (0) | 2024.11.11 |
Terraform VPC 생성 (0) | 2021.08.03 |
Terraform Application Load Balancer(ALB)생성 (0) | 2021.08.02 |
Terraform 정의 (0) | 2021.07.28 |
댓글