-terraform init / plan / apply 실행시
.terraform : init 실행시 생성
.terraform.lock.hcl : 잠금파일형태, 여러명(팀)이 함께 협업할때 충돌방지
.terraform.tfstate : json형태파일, 인프라스트락처의 현재 상태를 저장. (상태저장 파일)
⇒ 변수는 main.tf 파일에 입력 설정 할 수 도 있고,
별도의 변수파일.tf 파일을 생성해서 main.tf 와 같은 위치에 두고 작업 진행해도됨.
변수 유형
string : 문자
number : 숫자
bool : true , false 형태
any : 모든 유형을 포함.
default : 설정을 안하면 기본으로 적용됨.
type = list(string)
test = [ “test1”, “test2”]
test = {
test1 = “test1”
test2 = “test2”
}
test = [“test1”, 1 ]
test = [“test1”, 1 ]
name = “var1”
port [ 80,22,443 ]
- provider "aws" {
--region = "ap-northeast-2"
}
resource "aws_s3_bucket" "main-bucket" {
--bucket = "terraform-bucket-s3-phj"
--tags = {
----Name = "terraform-bucket1"
--}
}

데이터 저장 / 다운로드 / 권한
- provider "aws" {
--region = "ap-northeast-2"
}
resource "aws_iam_user" "main-user" {
--name = "hong-gildong"
}

- resource "aws_iam_group" "iam-group" {
--name = "terraform-group"
}
- provider "aws" {
--region = "ap-northeast-2"
}
resource "aws_iam_role" "main-role" {
--name = "terraform-role"
--path = "/"
--assume_role_policy = <<-EOF
--{
--"Version": "2012-10-17",
--"Statement": [
----{
------"Sid": "",
------"Effect": "Allow",
------"Principal": {
-------"Service": "ec2.amazonaws.com"
------},
------"Action": "sts:AssumeRole"
---}
--]
-}
EOF
}
- resource "aws_iam_role_policy" "main-policy-s3" {
--name = "terraform-s3-policy"
--role = aws_iam_role.main-role.id
--policy = <<-EOF
--{
--"Statement": [
----{
----"Sid": "AllowAppArtifactsReadAccess",
----"Action": [
--------"s3:GetObject"
------],
------"Resource": [
--------"*"
------],
------"Effect": "Allow"
----}
--]
}
EOF
}

- resource "aws_iam_instance_profile" "main-profile" {
--name = "terraform-profile"
--role = aws_iam_role.main-role.id
}
- resource "aws_iam_user_policy" "main-user-policy" {
--name = "super-admin"
--user = aws_iam_user.main-user.name
--policy = <<-EOF
--{
--"Version": "2012-10-17",
--"Statement": [
----{
------"Effect": "Allow",
------"Action": [
--------"*"
------],
------"Resource": [
--------"*"
----]
--}
-]
}
EOF
}
- resource "aws_iam_group_membership" "group-mem" {
--name = aws_iam_group.main-group.name
--users = [
----aws_iam_user.main-user.name
--]
--group = aws_iam_group.main-group.id
}
파일3개로 [ maif.tf /
variable.tf ( 변수 선언 , type = string ) /
terraform.tfvars ( 선언한 변수 값 , name = “test”) ]
- provider "aws" {
--region = "ap-northeast-2"
}
resource "aws_s3_bucket" "main-bucket" {
--bucket = var.bucket_name
}
변수를 선언하여 s3-bucket 생성
variable.tf
- variable "bucket_name" {
----type = string
}
terraform.tfvars
bucket_name = "terraform-bucket1"
변수내용을 지정
웹 페이지를 만드는 인스턴스 생성
- resource "aws_instance" "main-web" {
--ami = var.image_id
--instance_type = "t2.micro"
--vpc_security_group_ids = [" {aws_security_group.web_sg.id} "]
--user_data = <<-EOF
---#!/bin/bash
---**sudo** httpd -k start
---EOF
--tags = {
----Name = "${var.tag_name}_server"
--}
}
- resource "aws_security_group" "main-web_sg" {
--name = "standalone_web_sg"
--ingress = {
----from_port = var.httpd_port
----to_port = var.httpd_port
----protocol = tcp
----cidr_blocks = ["${var.cidr_blocks["cidr_all"]}"]
--}
}
- variable "region" {
----description = "seoul-KOREA"
----default = "ap-northeast-2"
}
- variable "tag_name" {
----type = string
----default = "web"
}
- variable "image_id" {
----description = "ec2-instance"
----type = string
----default = "ami-0d6e6a06d11d7777d"
}
- variable "httpd_port" {
----description = "webserver_http_port"
----default = 80
}
- variable "cidr_blocks" {
----description = "cidr_block_map"
----type = map
----default = {
--------cidr_all = "0.0.0.0/0"
--------cidr_vpc = "10.10.0.0/16"
--------cidr_sub1 = "10.10.1.0/24"
--------cidr_sub2 = "10.10.2.0/24"
--------cidr_sub3 = "10.10.3.0/24"
----}
}