- [실습] Terraform - 첫 실행2024년 04월 27일
- yeongki0944
- 작성자
- 2024.04.27.:27
1. AWS CLI - Role 설정
EC2에 AWS CLI Access Key or Role이 없는 경우
aws ec2 describe-images --owners amazon --filters "Name=name,Values=amzn2-ami-hvm-2.0.*-x86_64-gp2" "Name=state,Values=available" --query 'Images|sort_by(@, &CreationDate)[-1].[ImageId, Name]' --output text
2. main.tf 작성
AL2ID=`aws ec2 describe-images --owners amazon --filters "Name=name,Values=amzn2-ami-hvm-2.0.*-x86_64-gp2" "Name=state,Values=available" --query 'Images|sort_by(@, &CreationDate)[-1].[ImageId]' --output text` echo $AL2ID
# tf 파일 수정 provider "aws" { region = "ap-northeast-1" } resource "aws_vpc" "example" { cidr_block = "10.100.0.0/16" } resource "aws_subnet" "example" { vpc_id = aws_vpc.example.id cidr_block = "10.100.1.0/24" map_public_ip_on_launch = true } resource "aws_instance" "example" { ami = "$AL2ID" instance_type = "t2.micro" subnet_id = aws_subnet.example.id tags = { Name = "aews-study" } } EOT
3. ssh 새로 열어 watch로 모니터링
# 모니터링 while true; do aws ec2 describe-instances --query "Reservations[*].Instances[*].{PublicIPAdd:PublicIpAddress,InstanceName:Tags[?Key=='Name']|[0].Value,Status:State.Name}" --filters Name=instance-state-name,Values=running --output text ; echo "------------------------------" ; sleep 1; done
4. terraform init, plan, apply
# 초기화 terraform init ls -al tree .terraform # plan 확인 terraform plan # apply 실행 terraform apply Enter a value: yes 입력
...
5. terraform destroy
# 리소스 삭제 terraform destroy # 리소스 삭제 : yes없이 terraform destroy -auto-approve
..
다음글이전글이전 글이 없습니다.댓글
스킨 업데이트 안내
현재 이용하고 계신 스킨의 버전보다 더 높은 최신 버전이 감지 되었습니다. 최신버전 스킨 파일을 다운로드 받을 수 있는 페이지로 이동하시겠습니까?
("아니오" 를 선택할 시 30일 동안 최신 버전이 감지되어도 모달 창이 표시되지 않습니다.)