2回答
- 新しい順
- 投票が多い順
- コメントが多い順
0
To reach out an instance from the internet, your ec2 needs to:
- have a public ip address
- be deployed on a public subnet (with the routing pointing to an internet gateway)
You said that you are trying to connect to the public ip, so i assume that you have already done the point 1, otherwise add associate_public_ip_address to the aws_instance resource. To accomplish the point 2, first create the internet gateway:
resource "aws_internet_gateway" "gw" {
vpc_id = aws_vpc.your_vpc_resource_name.id
tags = {
Name = "main"
}
}
then create a routing table that point to it:
resource "aws_route_table" "main" {
vpc_id = aws_vpc.your_vpc_resource_name.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.gw.id
}
tags = {
Name = "main"
}
}
then associate it to the subnet where the EC2 has been deployed:
resource "aws_route_table_association" "main" {
subnet_id = aws_subnet.your_vpc_subnet_name.id
route_table_id = aws_route_table.main.id
}
After that, your connection should works.
回答済み 1年前
0
Sure, you need even a vpc and a subnet, here it's a full example:
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
tags = {
Name = "main"
}
}
resource "aws_subnet" "a" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
availability_zone = "us-east-2a"
tags = {
Name = "Main"
}
}
resource "aws_internet_gateway" "gw" {
vpc_id = aws_vpc.main.id
tags = {
Name = "main"
}
}
resource "aws_route_table" "main" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.gw.id
}
tags = {
Name = "main"
}
}
resource "aws_route_table_association" "main" {
subnet_id = aws_subnet.a.id
route_table_id = aws_route_table.main.id
}
Then you need to create a security group and the EC2 with the code that you have already written. regards
回答済み 1年前
Thanks for your advise, Do I need to declare aws_vpc and aws_subnet resource also ?
│ on main.tf line 6, in resource "aws_internet_gateway" "gw": │ 6: vpc_id = aws_vpc.your_vpc_resource_name.id │ │ A managed resource "aws_vpc" "your_vpc_resource_name" has not been declared in the root module.