Terraform_sample_apache_docker - tetsuyaf1100/hello-world GitHub Wiki

main.tf

resource "aws_security_group" "example_ec2"{
  name = "example_ec2"

  ingress{
    from_port = 80
    to_port = 80
    protocol = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  # SSH access from anywhere
  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  egress{
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

resource "aws_instance" "example" {
  ami = "ami-0c3fd0f5d33134a76"
  instance_type = "t3.micro"
  vpc_security_group_ids = [aws_security_group.example_ec2.id]
  key_name = "aws5"

  user_data = <<EOF
  #!/bin/bash

  # install httpd
  yum install -y httpd
  systemctl start httpd.service

  # install docker
  sudo yum update -y
  sudo yum install -y docker
  sudo systemctl start docker
  sudo gpasswd -a ec2-user docker

  # install docker-compose
  sudo su
  curl -L --fail https://github.com/docker/compose/releases/download/1.22.0/run.sh > /usr/local/bin/docker-compose
  chmod +x /usr/local/bin/docker-compose
 sed -e "s%\(^PATH.*$\)%\1:/usr/local/bin%" -i ~/.bash_profile
  
EOF

  tags = {
    Name = "example"
  }
}

output "example_public_dns"{
  value = aws_instance.example.public_dns
}