๋ฐ์ํ
- CloudNeta์ ๊ฐ์๋ค๋์ด ์งํํ์๋ Terraform 101 Study์ ์ฐธ๊ฐํ๋ฉฐ ์์ฑํ ๋ด์ฉ์ ๋๋ค.
- ์คํฐ๋ ๊ต์ฌ โ ํ ๋ผํผ์ผ๋ก ์์ํ๋ IaC
[1์ฃผ์ฐจ ๋์ ๊ณผ์ ] 1. EC2 ์น์๋ฒ ๋ฐฐํฌ
Ubuntu ์ apache(httpd) ๋ฅผ ์ค์นํ๊ณ index.html ์์ฑ(๋๋ค์ ์ถ๋ ฅ)ํ๋ userdata ๋ฅผ ์์ฑํด์ ์ค์ ๋ฐฐํฌ ํ ์น ์ ์
provider "aws" { region = "ap-northeast-2" } resource "aws_instance" "apache" { ami = "ami-0c9c942bd7bf113a2" instance_type = "t2.micro" vpc_security_group_ids = [aws_security_group.apache.id] user_data = <<-EOF #!/bin/bash sudo apt install -y apache2 sudo echo "Hello, T101 Study I am hyuckang" > /var/www/html/index.html sudo systemctl start apache2.service EOF tags = { Name = "apache" } } resource "aws_security_group" "apache" { name = "apache_sg" ingress { from_port = 80 to_port = 80 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } egress { from_port = 0 to_port = 65535 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } tags = { Name = "apache_sg" } } output "apache_public_dns" { value = aws_instance.apache.public_dns }
[1์ฃผ์ฐจ ๋์ ๊ณผ์ ] 2. AWS S3/DynamoDB ๋ฐฑ์๋
AWS S3/DynamoDB ๋ฐฑ์๋ ์ค์ ์ค์ต
# terraform state๋ฅผ ์ ์ฅํ๋ S3์ lock์ ๊ด๋ฆฌํ๋ dynamoDB ์์ฑํ๋ CLI export BUCKET_NAME="hyuckang-tf101" export TABLE_NAME="hyuckang-tf101" export REGION="ap-northeast-2" # state๋ฅผ ์ ์ฅํ S3 ๋ฒํท ์์ฑ aws s3api create-bucket \ --bucket $BUCKET_NAME --region $REGION --create-bucket-configuration LocationConstraint=$REGION # ๋กค๋ฐฑ์ด ๊ฐ๋ฅํ๋๋ก state์ ๋ฒ์ ์ ๊ด๋ฆฌํ๋ ๋ฒ์ ๋์ enable aws s3api put-bucket-versioning \ --bucket $BUCKET_NAME --versioning-configuration Status=Enabled # Lock์ ๊ด๋ฆฌํ๋ dynamoDB ํ ์ด๋ธ ์์ฑ aws dynamodb create-table \ --table-name $TABLE_NAME \ --attribute-definitions \ AttributeName=LockID,AttributeType=S \ --key-schema \ AttributeName=LockID,KeyType=HASH \ --provisioned-throughput \ ReadCapacityUnits=1,WriteCapacityUnits=1
# backend๋ก S3์ dynamoDB๋ฅผ ์ง์ provider "aws" { region = "ap-northeast-2" } terraform { backend "s3" { bucket = "hyuckang-tf101" key = "hyuckang-tf101/terraform.tfstate" region = "ap-northeast-2" dynamodb_table = "hyuckang-tf101" } }
[1์ฃผ์ฐจ ๋์ ๊ณผ์ ] 3. lifecycle์ precondition ์ค์ต
lifecycle์ precondition ์ค์ต ๋ด์ฉ์์ step0.txt ~ step6.txt ์ด 7๊ฐ์ ํ์ผ ์ด๋ฆ ์ค ํ๋๊ฐ ์ผ์น ์ ๊ฒ์ฆ ์กฐ๊ฑด ๋ง์กฑ์ผ๋ก ์ฝ๋ ์์ฑ
variable "file_name" { default = "step5.txt" } resource "local_file" "step6" { content = "lifecycle - step 6" filename = "${path.module}/${var.file_name}" lifecycle { precondition { condition = contains(["step0.txt", "step1.txt", "step2.txt", "step3.txt", "step4.txt", "step5.txt", "step6.txt"], var.file_name) error_message = "file name is not 'step0~6.txt'" } } }
[1์ฃผ์ฐจ ๋์ ๊ณผ์ ] 4. AWS ์๋น์ค ๋ฆฌ์์ค ๋ฐฐํฌ + ๋ฆฌ์์ค ์์ฑ ๊ทธ๋ํ ํ์ธ
Hashicorp AWS Provider Document ์ Example Usage ์ค ์๋ฌด๊ฑฐ๋ 1๊ฐ์ AWS ์๋น์ค ๋ฆฌ์์ค ๋ฐฐํฌ ์ค์ต
provider "aws" { region = "ap-northeast-2" } resource "aws_vpc" "hyuckang" { cidr_block = "10.10.0.0/16" } resource "aws_subnet" "hyuckang" { vpc_id = aws_vpc.hyuckang.id cidr_block = "10.10.10.0/24" } resource "aws_route_table" "hyuckang" { vpc_id = aws_vpc.hyuckang.id } resource "aws_route_table_association" "hyuckang" { subnet_id = aws_subnet.hyuckang.id route_table_id = aws_route_table.hyuckang.id }
๋ฐ์ํ
'๐ IaC > Terraform' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Terraform 101 Study - 2์ฃผ์ฐจ(2) ๋์ ๊ณผ์ (0) | 2023.07.14 |
---|---|
TF 101 Study 2์ฃผ์ฐจ ์ ๋ฆฌ - data source, variable, local, output, for_each, for, dynamic (0) | 2023.07.14 |
Terraform 101 Study - 1์ฃผ์ฐจ(1) ๋ด์ฉ์ ๋ฆฌ (0) | 2023.07.09 |
[Terraform] Provider (0) | 2022.09.09 |
[Terraform] ์์กด์ฑ(dependency) ๊ด๋ฆฌ (0) | 2022.09.05 |