파일을 생성하는 다양한 방법 중 하나는 터미널에서 cat 명령어를 사용하는것입니다.
cat <<{keyword} {filename}
로 시작하여 {keyword}
까지의 내용을 파일에 저장하는 방식으로 파일에 덮어쓰기가 가능합니다.
예를들면 BASH 쉘에서는 아래방식으로 파일작성이 가능합니다.
cat <<EOF > main.tf
resource "local_file" "abc" {
content = "abc!"
filename = "${path.module}/abc.txt"
}
EOF
이 경우main.tf
파일이 아래처럼 생성됩니다.
resource "local_file" "abc" {
content = "abc!"
filename = "${path.module}/abc.txt"
}
만약 BASH쉘이 아니라, ZSH 쉘을 사용하고 있다면 아래의 에러가 발생합니다.
cat <<EOF > main.tf
resource "local_file" "abc" {
content = "abc!"
filename = "${path.module}/abc.txt"
}
EOF
zsh: bad substitution
그 이유는 ZSH 쉘에서는 ${something}
문자를 셸치환으로 해석하려고 시도하기 때문입니다. 이를 해결하기 위해서 아래의 방법을 사용합니다.
$
문자 앞에 \
문자로 이스케이프 해주기
cat <<EOF > main.tf
resource "local_file" "abc" {
content = "abc!"
filename = "\${path.module}/abc.txt"
}
EOF
bash -c
명령어를 사용하는 방법도 추가로 제안되었으나,“”
문자열이 많이 사용되는 terraform code 에는 사용하기힘들어 보입니다.