# Define the provider
provider "aws" {
region = "us-east-1" # Change this to your desired AWS region
}
# Create a VPC
resource "aws_vpc" "example" {
cidr_block = "10.0.0.0/16"
}
# Create a subnet within the VPC
resource "aws_subnet" "example" {
vpc_id = aws_vpc.example.id
cidr_block = "10.0.1.0/24"
availability_zone = "us-east-1a" # Change this to your desired availability
zone
map_public_ip_on_launch = true
}
# Create a security group for the EC2 instance
resource "aws_security_group" "example" {
name = "example-security-group"
description = "Example security group for EC2 instance"
# Define inbound rules as needed for your application
# For example, allow SSH access:
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"] # Be cautious about using "0.0.0.0/0" in production
}
# Add more rules for your application as necessary
}
# Create an EC2 instance
resource "aws_instance" "example" {
ami = "ami-XXXXXXXXXXXXXXXXX" # Replace with your desired AMI ID
instance_type = "t2.micro" # Replace with your desired instance type
subnet_id = aws_subnet.example.id
key_name = "your-key-pair-name" # Replace with your SSH key pair name
# Attach the security group to the instance
security_groups = [aws_security_group.example.name]
tags = {
Name = "ExampleEC2Instance"
}
}
# Create an RDS instance
resource "aws_db_instance" "example" {
allocated_storage = 20
storage_type = "gp2"
engine = "mysql"
engine_version = "5.7"
instance_class = "db.t2.micro"
name = "exampledb"
username = "admin"
password = "your-password"
parameter_group_name = "default.mysql5.7"
skip_final_snapshot = true
publicly_accessible = false
vpc_security_group_ids = [aws_security_group.example.id]
tags = {
Name = "ExampleRDSInstance"
}
}