Terraform - AWS VPC, Internet Gateway, Route Table, Subnet, Security Group, Network Interface, Elastic IP, and EC2 Instance
A complete basic terraform configuration file that will create the required cloud infrastructure resources to support an EC2 t2.micro instance running Amazon Linux 2 AMI which renders a simple html page upon visiting the assigned elastic IP.
The minimum AWS cloud infrastructure requirements for this are listed as follows:
- Create VPC
- Create Internet Gateway
- Create Custom Route Table
- Create Subnet
- Associate Subnet with Route Table
- Create Security Group to allow ports
22, 80, 443
- Create Network Interface with IP in created Subnet range
- Assign Elastic IP to the Network Interface
- Create Amazon Linux 2 AMI EC2 instance configured to cloud resources
Additional ideas to implement include:
- Terraform variables
- Terraform outputs
- Terraform and provider versioning (best practices)
- Elastic Load Balancer
- Elastic Container Services
- Fargate Instances
- Learn terraform to create cloud resources to support AWS server instances
- Successfully implement terraform to create required clould resources and server instance
To get a local copy up and running follow these steps.
- Clone the repo
git clone https://github.com/daveryde/terraform-aws-starter.git
- In main.tf, fill in your own region, AWS access, and secret keys to create resources using your AWS account
provider "aws" {
region = "<region>"
access_key = "<access_key>"
secret_key = "<secret_key>"
}
- In the project directory, use terraform to create the resources in your AWS account:
terraform apply
- Copy and paste your assigned elastic IP output from the terminal into a browser to view the html page generated once all cloud resources have been created to support this server instance.
Apply complete! Resources: 9 added, 0 changed, 0 destroyed.
Outputs:
instance_id = "i-026fe87e3a9c7306c"
instance_public_ip = "18.116.190.239" <-- example public IP output
- AWS VPC aws_vpc
- AWS Internet Gateway aws_internet_gateway
- AWS Route Table aws_route_table
- AWS Subnet aws_subnet
- AWS Route Table Association aws_route_table_association
- AWS Security Group aws_security_group
- AWS Network Interface aws_network_interface
- AWS Elastic IP aws_eip
- AWS Instance aws_instance