Skip to content

Commit

Permalink
[FEAT] install docker on ec2 by terraform
Browse files Browse the repository at this point in the history
  • Loading branch information
ohksj77 committed Mar 16, 2024
1 parent 385f48e commit 96a5846
Showing 1 changed file with 56 additions and 4 deletions.
60 changes: 56 additions & 4 deletions terraform/terraform.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ resource "aws_subnet" "private-subnet-c" {
availability_zone = "ap-northeast-2c"
}

resource "aws_subnet" "public-subnet-c" {
resource "aws_subnet" "public-subnet-b" {
vpc_id = aws_vpc.vpc_network.id
tags = merge(var.tags, {})
cidr_block = cidrsubnet(aws_vpc.vpc_network.cidr_block, 8, 3)
Expand All @@ -40,7 +40,7 @@ resource "aws_route_table" "r" {
}

resource "aws_route_table_association" "a" {
subnet_id = aws_subnet.public-subnet-c.id
subnet_id = aws_subnet.public-subnet-b.id
route_table_id = aws_route_table.r.id
}

Expand All @@ -63,10 +63,25 @@ resource "aws_elb" "elb" {
}

subnets = [
aws_subnet.public-subnet-c.id
aws_subnet.public-subnet-b.id
]
}

resource "tls_private_key" "key" {
algorithm = "RSA"
rsa_bits = 4096
}

resource "aws_key_pair" "generated_key" {
key_name = "twtw-key"
public_key = tls_private_key.key.public_key_openssh
}

resource "local_file" "private_key_pem" {
content = tls_private_key.key.private_key_pem
filename = "${path.module}/twtw-key.pem"
}

resource "aws_instance" "instance-a" {
tags = merge(var.tags, {})
subnet_id = aws_subnet.private-subnet-a.id
Expand All @@ -78,6 +93,25 @@ resource "aws_instance" "instance-a" {
vpc_security_group_ids = [
aws_security_group.security-group-a.id,
]

connection {
type = "ssh"
user = "ubuntu"
private_key = file("${local_file.private_key_pem.filename}")
host = self.public_ip
}

provisioner "remote-exec" {
inline = [
"sudo apt update",
"sudo apt install -y docker.io",
"sudo systemctl start docker",
"sudo systemctl enable docker",
"sudo curl -L \"https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)\" -o /usr/local/bin/docker-compose",
"sudo chmod +x /usr/local/bin/docker-compose",
"sudo usermod -aG docker ubuntu",
]
}
}

resource "aws_instance" "instance-c" {
Expand All @@ -98,6 +132,25 @@ resource "aws_instance" "instance-c" {
"sudo usermod -aG docker ubuntu"
]
}

connection {
type = "ssh"
user = "ubuntu"
private_key = file("${local_file.private_key_pem.filename}")
host = self.public_ip
}

provisioner "remote-exec" {
inline = [
"sudo apt update",
"sudo apt install -y docker.io",
"sudo systemctl start docker",
"sudo systemctl enable docker",
"sudo curl -L \"https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)\" -o /usr/local/bin/docker-compose",
"sudo chmod +x /usr/local/bin/docker-compose",
"sudo usermod -aG docker ubuntu",
]
}
}

resource "aws_security_group" "security-group-a" {
Expand Down Expand Up @@ -172,7 +225,6 @@ resource "aws_db_instance" "db_instance" {
instance_class = "db.m5d.large"
engine = "mysql"
db_name = "TWTW"
availability_zone = "ap-northeast-2b"
db_subnet_group_name = aws_db_subnet_group.twtw_db_subnet_group.name
allocated_storage = 20
}
Expand Down

0 comments on commit 96a5846

Please sign in to comment.