🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)
🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)

🔧 Programmierung 🕛 kürzlich 6 Min Lesezeit
0

Deploy an EC2 instance inside a custom VPC using Terraform.

↗ Quelle (dev.to)
🗣️ Stimme:

Deploy an EC2 instance inside a custom VPC using Terraform.



This guide will set up the following:




  • A VPC with one Availability Zones (AZs).

  • In the AZ, there will be:

  • A public subnet for resources like NAT gateway and EC2 instance.

  • Internet Gateway for outbound internet traffic from public subnets.

  • Route Tables to handle routing within the subnets.

  • Security Groups to control traffic to/from instances.
    In this project, you will learn how to automatically deploy an EC2 instance inside a custom VPC using Terraform.



Prerequisites




  1. An active AWS account with an IAM user already created

  2. Text editor of your choice (VSCode Recommended)

  3. Terraform Installed: Make sure you have Terraform installed on your machine, you should also have a basic knowledge of how it works.

  4. AWS credentials: You should have AWS credentials configured locally using AWS CLI or through environment variables.

  5. AWS Provider: This guide uses the AWS provider.

  6. Bash scripting knowledge



As you should know by now, Terraform is an IaC tool that helps to automate processes which increase delivery time of software. In this tutorial, Terraform will be employed in setting up an ec2 instance in a VPC and all of its components



Setting up the project environment

The first step is to create a folder on your device and open it up in VSCode. We will be creating 4 terraform files for this project;

Your project directory can be structured like this:

terraform/

├── provider.tf

├── variables.tf

├── main.tf

├── outputs.tf



Here is the outline of all the resources, we will be creating.




  • Create a VPC folder and files

  • Define the Provider Block

  • Create the VPC Block

  • Create the Subnet(s)

  • Create an Internet Gateway

  • Create Route Tables

  • Associate Subnet with Route Tables

  • Create Security Groups

  • Create KeyPairs

  • Create EC2 Instance



Step-by-Step Terraform Configuration



1. Creating your Provider Configuration

The first step is to configure the AWS provider on your provider.tf file

hcl

#Create a Terraform Provider Block

terraform {

required_providers {

aws = {

source = “hashicorp/aws”

version = “~>5.68.0”

}

}

}

provider “aws” {

region = “us-east-1” # Select your desired region

}





3. Creating your Subnet Block



For this demo, we will create only one subnet in one availability zone.

hcl

#Create the Subnet(s)

resource "aws_subnet" "public_subnet" {

vpc_id = aws_vpc.my_vpc.id

cidr_block = var.public_subnet_cidr

availability_zone = var.availability_zone

map_public_ip_on_launch = true

tags = {

Name = "my-pub-sub"

}

}





5. Route Tables



We need a route table for both public and private subnets. Public subnets route through the Internet Gateway, while private subnets route through a NAT Gateway.



hcl

#Create Route Tables

resource "aws_route_table" "public_rt" {

vpc_id = aws_vpc.my_vpc.id

route {

cidr_block = "0.0.0.0/0"

gateway_id = aws_internet_gateway.igw.id

}

tags = {

Name = "public-rt"

}

}





7. Security Groups



Here’s a basic security group allowing inbound SSH access from your IP and allowing all outbound traffic.



hcl

#Create Security Groups

resource "aws_security_group" "my-sg" {

vpc_id = aws_vpc.my_vpc.id # Ensure the security group is created in the same VPC

name = "my security group"

description = "my security group"

ingress {

description = "HTTP"

from_port = 80

to_port = 80

protocol = "tcp"

cidr_blocks = ["0.0.0.0/0"] # Allow traffic from anywhere

}

ingress {

description = "HTTPS"

from_port = 443

to_port = 443

protocol = "tcp"

cidr_blocks = ["0.0.0.0/0"]

}

ingress {

description = "SSH"

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"]

}

tags = {

Name = "my-sg"

}

}





8. Key Pairs

A key pair is a set of cryptographic keys used for authentication and encryption. It consists of a public key and a private key.



Public Key: This key is freely shared and can be used to encrypt data. Only the corresponding private key can decrypt the data.

Private Key: This key is kept secret and should never be shared. It's used to decrypt data encrypted with the public key.



hcl

Generate a new private key

resource "tls_private_key" "terraform_kp" {

algorithm = "RSA"

rsa_bits = 4096

}

Create AWS Key Pair using the public key generated above

resource "aws_key_pair" "my-terraform-kp" {

key_name = "terraform-kp" # New Key Pair Name

public_key = tls_private_key.terraform_kp.public_key_openssh

}

To create a file or folder to save your Private Key

resource "local_file" "terraform_kp" {

content = tls_private_key.terraform_kp.private_key_pem

filename = "terraform-kp.pem" # Save as a .pem file

}





10. Outputs



You can create an outputs.tf to display useful information like the VPC ID and subnet IDs after deployment.

outputs.tf:



hcl

Output Public IP

output "project_server_public_ip" {

value = aws_instance.project_server.public_ip

}

Output Public DNS

output "aws_instance_public_dns" {

value = aws_instance.project_server.public_dns

}





12. How to Deploy



Ø Initialize Terraform:

Navigate to your project directory and run:

bash

terraform init









13. Verify the Resources Created:



Navigate to your AWS management console and search for VPC to see the newly created VPC via terraform





14. Clean Up Resources:



If you’re done and no longer need your resources, you can terminate them to avoid incurring charges.

Run the following command to clean your resources

bash

terraform destroy





Conclusion

By following these steps and customizing the Terraform configuration to your specific needs, you can effectively launch EC2 instances in a VPC using Terraform, and you should be able to SSH into your website using the EC2 instance’s public IP.



If you find this helpful, please click the clap 👏 button below a few times to show your support for the author 👇

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
3 Quellen
GPT-6 Astra Release Today? OpenAI’s Next Major AI Model Is Almost Here
1 Quelle
Apple accuses OpenAI of destroying evidence as trade-secrets fight intensifies
1 Quelle
Major AI platforms go down in unprecedented simultaneous outage
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Deploy an EC2 instance inside a custom VPC using Terraform.

Thematisch verwandte Begriffe: Deploy, instance, inside, custom · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...