brunch

You can make anything
by writing

C.S.Lewis

by Master Seo Nov 15. 2022

19탄-19. 테라폼-AWS-반복문으로 s3

다음은 주말 CloudNet 테라폼 스터디 내용 참고하여  정리한 부분입니다.

https://gasidaseo.notion.site/gasidaseo/CloudNet-Blog-c9dfa44a27ff431dafdd2edacc8a1863



<1> 반복문 count 매개변수  - 버킷 3개 생성

<2> 반복문 count 매개변수 - 입력 변수로  s3 만들기




<1> 반복문 count 매개변수  - 버킷 3개 생성


테스트 버킷을 여러가 만들어야 할떄~~


NICKNAME=masterseo



cat <<EOT > s3.tf

provider "aws" {

  region = "ap-northeast-2"

}

resource "aws_s3_bucket" "mybucket" {

  count = 3

  bucket = "$NICKNAME.\${count.index}"

}

EOT


terraform init && terraform plan  && terraform apply -auto-approve



aws_s3_bucket.mybucket[0]: Creation complete after 3s [id=masterseo.0]

aws_s3_bucket.mybucket[1]: Creation complete after 4s [id=masterseo.1]

aws_s3_bucket.mybucket[2]: Creation complete after 4s [id=masterseo.2]


Apply complete! Resources: 3 added, 0 changed, 0 destroyed.

[root@ip-172-31-61-209 test]#



terraform destroy -auto-approve

rm -rf *



참고

https://developer.hashicorp.com/terraform/language/meta-arguments/count





<2> 반복문 count 매개변수 - 입력 변수로  s3 만들기


지정된 이름으로 버킷을 여러개 만들어야 할때 ~~

 버킷 3개 생성


cat <<EOT > variables.tf

variable "bucket_names" {

  description = "Create s3"

  type        = list(string)

  default     = ["seo111", "tae1112", "ho111"]

}

EOT



cat <<EOT > s3.tf

provider "aws" {

  region = "ap-northeast-2"

}

resource "aws_s3_bucket" "mybucket" {

  count = length(var.bucket_names)

  bucket  = var.bucket_names[count.index]

}

EOT



terraform init && terraform plan  && terraform apply -auto-approve


aws s3 ls

2022-11-15 07:23:15 ho111

2022-11-15 07:23:15 seo111

2022-11-15 07:23:16 tae111



3

terraform destroy -auto-approve

rm -rf *





다음


https://brunch.co.kr/@topasvga/2813





https://brunch.co.kr/@topasvga/2421

감사합니다.


                    

매거진의 이전글 19탄-18. 테라폼-AWS-조건문

작품 선택

키워드 선택 0 / 3 0

댓글여부

afliean
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari