https://gasidaseo.notion.site/gasidaseo/CloudNet-Blog-c9dfa44a27ff431dafdd2edacc8a1863
1
mkdir state
cd state
2
cat <<EOT > vpc.tf
provider "aws" {
region = "ap-northeast-2"
}
resource "aws_vpc" "myvpc" {
cidr_block = "10.10.0.0/16"
tags = {
Name = "t101-study"
}
}
EOT
3
terraform init && terraform plan && terraform apply -auto-approve
4
[root@ip-172-31-61-209 state]# ls
terraform.tfstate vpc.tf
[root@ip-172-31-61-209 state]# more terraform.tfstate | jq
"serial": 2,
// 시리얼이 2이다.
5
테그 변경해보기
cat <<EOT > vpc.tf
provider "aws" {
region = "ap-northeast-2"
}
resource "aws_vpc" "myvpc" {
cidr_block = "10.10.0.0/16"
tags = {
Name = "tf-state"
}
}
EOT
terraform plan && terraform apply -auto-approve
6
ls
[root@ip-172-31-61-209 state]# ls
terraform.tfstate terraform.tfstate.backup vpc.tf
7
diff terraform.tfstate terraform.tfstate.backup
[root@ip-172-31-61-209 state]# diff terraform.tfstate terraform.tfstate.backup
4c4
< "serial": 6,
---
> "serial": 4,
7c7,52
< "resources": [],
---
8
정리
시리얼 값이 변경 된다.
상태 파일 백업파일이 생긴다.
9
삭제
terraform destroy -auto-approve
1
각 팀원이 동일한 테라폼 상태 파일 사용을 위해서, 공유 위치에 저장이 필요
2
상태 파일 잠금이 되어야 한다.
잠금 기능 없이 두 팀원이 동시에 테라폼 실행 시 여러 테라폼 프로세스가 상태 파일을 동시에 업데이트하여 충돌 가능.
잠금기능은 다이나모 디비의 lock으로 관리한다.
1
참고
https://docs.aws.amazon.com/ko_kr/amazondynamodb/latest/developerguide/GettingStartedDynamoDB.html
2
# 테이블 생성
aws dynamodb create-table \
--table-name Music \
--attribute-definitions \
AttributeName=Artist,AttributeType=S \
AttributeName=SongTitle,AttributeType=S \
--key-schema \
AttributeName=Artist,KeyType=HASH \
AttributeName=SongTitle,KeyType=RANGE \
--provisioned-throughput \
ReadCapacityUnits=5,WriteCapacityUnits=5 \
--table-class STANDARD
# 테이블 생성 확인
aws dynamodb list-tables --output text
TABLENAMES Music
aws dynamodb describe-table --table-name Music | jq
aws dynamodb describe-table --table-name Music --output table
3
콘솔 확인 > 다이나모 디비
music이라는 테이블이 있음
4
데이터 쓰기 방법 2가지
https://docs.aws.amazon.com/ko_kr/amazondynamodb/latest/developerguide/getting-started-step-2.html
# DynamoDB API
aws dynamodb put-item \
--table-name Music \
--item \
'{"Artist": {"S": "No One You Know"}, "SongTitle": {"S": "Call Me Today"}, "AlbumTitle": {"S": "Somewhat Famous"}, "Awards": {"N": "1"}}'
aws dynamodb put-item \
--table-name Music \
--item \
'{"Artist": {"S": "No One You Know"}, "SongTitle": {"S": "Howdy"}, "AlbumTitle": {"S": "Somewhat Famous"}, "Awards": {"N": "2"}}'
# PartiQL for DynamoDB
aws dynamodb execute-statement --statement "INSERT INTO Music \
VALUE \
{'Artist':'Acme Band','SongTitle':'Happy Day', 'AlbumTitle':'Songs About Life', 'Awards':'10'}"
aws dynamodb execute-statement --statement "INSERT INTO Music \
VALUE \
{'Artist':'Acme Band','SongTitle':'PartiQL Rocks', 'AlbumTitle':'Another Album Title', 'Awards':'8'}"
5
읽기
https://docs.aws.amazon.com/ko_kr/amazondynamodb/latest/developerguide/getting-started-step-3.html
# DynamoDB API
aws dynamodb get-item --consistent-read \
--table-name Music \
--key '{ "Artist": {"S": "Acme Band"}, "SongTitle": {"S": "Happy Day"}}' | jq
# PartiQL for DynamoDB
aws dynamodb execute-statement --statement "SELECT * FROM Music \
WHERE Artist='Acme Band' AND SongTitle='Happy Day'" | jq
aws dynamodb execute-statement --statement "SELECT * FROM Music \
WHERE Artist='Acme Band' AND SongTitle='Happy Day'" --output table
aws dynamodb execute-statement --statement "SELECT * FROM Music" --output table
aws dynamodb execute-statement --statement "SELECT * FROM Music" --output text
6
업데이트
# PartiQL for DynamoDB
aws dynamodb execute-statement --statement "UPDATE Music \
SET AlbumTitle='Updated Album Title' \
WHERE Artist='Acme Band' AND SongTitle='Happy Day' \
RETURNING ALL NEW *"
7
데이터 쿼리
# PartiQL for DynamoDB
aws dynamodb execute-statement --statement "SELECT * FROM Music \
WHERE Artist='Acme Band'" | jq
8
테이블 삭제
aws dynamodb delete-table --table-name Music
1
mkdir backend
cd backend
2
NICKNAME=masterseo
3
s3만들기
버저닝 활성화
아웃풋에서 버킷 arn을 출력한다.
4
cat <<EOT > backend.tf
provider "aws" {
region = "ap-northeast-2"
}
resource "aws_s3_bucket" "mys3bucket" {
bucket = "$NICKNAME-t101study-tfstate"
}
# Enable versioning so you can see the full revision history of your state files
resource "aws_s3_bucket_versioning" "mys3bucket_versioning" {
bucket = aws_s3_bucket.mys3bucket.id
versioning_configuration {
status = "Enabled"
}
}
output "s3_bucket_arn" {
value = aws_s3_bucket.mys3bucket.arn
description = "The ARN of the S3 bucket"
}
EOT
5
# 배포
terraform init && terraform plan && terraform apply -auto-approve
terraform state list
aws_s3_bucket.mys3bucket
aws_s3_bucket_versioning.mys3bucket_versioning
# S3 버킷 확인
aws s3 ls
[root@ip-172-31-61-209 backend]# aws s3 ls
2022-10-26 09:43:28 masterseo-t101study-tfstate
6
DynamoDB 테이블 생성 : 테라폼에서 DynamoDB 잠금을 사용하기 위해서는 LockID 라는 기본 키가 있는 테이블을 생성해야됨
# 코드 파일 수정
cat <<EOT > backend.tf
provider "aws" {
region = "ap-northeast-2"
}
resource "aws_s3_bucket" "mys3bucket" {
bucket = "$NICKNAME-t101study-tfstate"
}
# Enable versioning so you can see the full revision history of your state files
resource "aws_s3_bucket_versioning" "mys3bucket_versioning" {
bucket = aws_s3_bucket.mys3bucket.id
versioning_configuration {
status = "Enabled"
}
}
resource "aws_dynamodb_table" "mydynamodbtable" {
name = "terraform-locks"
billing_mode = "PAY_PER_REQUEST"
hash_key = "LockID"
attribute {
name = "LockID"
type = "S"
}
}
output "s3_bucket_arn" {
value = aws_s3_bucket.mys3bucket.arn
description = "The ARN of the S3 bucket"
}
output "dynamodb_table_name" {
value = aws_dynamodb_table.mydynamodbtable.name
description = "The name of the DynamoDB table"
}
EOT
7
# 배포
terraform plan && terraform apply -auto-approve
Apply complete! Resources: 3 added, 0 changed, 0 destroyed.
Outputs:
dynamodb_table_name = "terraform-locks"
s3_bucket_arn = "arn:aws:s3:::masterseo-t101study-tfstate"
[root@ip-172-31-61-209 backend]#
8
terraform state list
[root@ip-172-31-61-209 backend]# terraform state list
aws_dynamodb_table.mydynamodbtable
aws_s3_bucket.mys3bucket
aws_s3_bucket_versioning.mys3bucket_versioning
9
# DynamoDB 테이블 생성 확인
aws dynamodb list-tables --output text
[root@ip-172-31-61-209 backend]# aws dynamodb list-tables --output text
TABLENAMES terraform-locks
aws dynamodb describe-table --table-name terraform-locks | jq
aws dynamodb describe-table --table-name terraform-locks --output table
다음
https://brunch.co.kr/@topasvga/2769
https://brunch.co.kr/@topasvga/2421
감사합니다.