Requirement for above code is as below:
The company wants the Architecture to have the following services:
- Create a template with a VPC, 2 subnets and 1 instance in each subnet
- Attach Security groups, internet gateway and network interface to the instance
**Public IP not coming in instances from below Terraform code:
**
# Block Settings
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
}
}
}
# Provider
provider "aws" {
profile = "default"
region = "us-east-2"
}
# VPC
resource "aws_vpc" "TF_VPC" {
cidr_block = "170.31.0.0/16"
tags = {
Name = "TF_VPC"
}
}
# Subnets
resource "aws_subnet" "TF_Subnet1" {
vpc_id = aws_vpc.TF_VPC.id
cidr_block = "170.31.1.0/24"
availability_zone = "us-east-2a"
tags = {
Name = "TF_Subnet1"
}
}
resource "aws_subnet" "TF_Subnet2" {
vpc_id = aws_vpc.TF_VPC.id
cidr_block = "170.31.2.0/24"
availability_zone = "us-east-2b"
tags = {
Name = "TF_Subnet2"
}
}
# Security Group
resource "aws_security_group" "TF_SG" {
vpc_id = aws_vpc.TF_VPC.id
# Allow SSH access from anywhere
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# Allow HTTP access from anywhere
ingress {
from_port = 80
to_port = 80
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"]
}
tags = {
Name = "TF_SG"
}
}
# Internet Gateway
resource "aws_internet_gateway" "TF_IGW" {
vpc_id = aws_vpc.TF_VPC.id
tags = {
Name = "TF_IGW"
}
}
# Route Table
resource "aws_route_table" "TF_RT" {
vpc_id = aws_vpc.TF_VPC.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.TF_IGW.id
}
tags = {
Name = "TF_RT"
}
}
# Route Table Association
resource "aws_route_table_association" "TF_RTA1" {
subnet_id = aws_subnet.TF_Subnet1.id
route_table_id = aws_route_table.TF_RT.id
}
resource "aws_route_table_association" "TF_RTA2" {
subnet_id = aws_subnet.TF_Subnet2.id
route_table_id = aws_route_table.TF_RT.id
}
# Network Interface
resource "aws_network_interface" "TF_NI1" {
subnet_id = aws_subnet.TF_Subnet1.id
private_ips = ["170.31.1.5"]
security_groups = [aws_security_group.TF_SG.id]
tags = {
Name = "TF_NI1"
}
}
resource "aws_network_interface" "TF_NI2" {
subnet_id = aws_subnet.TF_Subnet2.id
private_ips = ["170.31.2.5"]
security_groups = [aws_security_group.TF_SG.id]
tags = {
Name = "TF_NI2"
}
}
# EC2 Instances
resource "aws_instance" "TF_instance1" {
ami = "ami-024e6efaf93d85776"
instance_type = "t2.micro"
key_name = "assign.ohio"
network_interface {
network_interface_id = aws_network_interface.TF_NI1.id
device_index = 0
}
tags = {
Name = "TF_instance1"
}
}
resource "aws_instance" "TF_instance2" {
ami = "ami-024e6efaf93d85776"
instance_type = "t2.micro"
key_name = "assign.ohio"
network_interface {
network_interface_id = aws_network_interface.TF_NI2.id
device_index = 0
}
tags = {
Name = "TF_instance2"
}
}
2
Answers
You need to associate public IP on your EC2 instance, example below:
Check the docs for reference.
It would be good if you ping the error you are receiving.
Alternatively, you can create Elastic IP and attach to the instance.
https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/eip