Skip to content

Commit

Permalink
create terraform ec2
Browse files Browse the repository at this point in the history
  • Loading branch information
Doris-Siu committed Mar 3, 2024
1 parent 1471ce0 commit 16083f4
Show file tree
Hide file tree
Showing 11 changed files with 235 additions and 0 deletions.
Binary file modified .DS_Store
Binary file not shown.
34 changes: 34 additions & 0 deletions terraform-ec2/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Local .terraform directories
**/.terraform/*

# .tfstate files
*.tfstate
*.tfstate.*

# Crash log files
crash.log
crash.*.log

# Exclude all .tfvars files, which are likely to contain sensitive data, such as
# password, private keys, and other secrets. These should not be part of version
# control as they are data points which are potentially sensitive and subject
# to change depending on the environment.
*.tfvars
*.tfvars.json

# Ignore override files as they are usually used to override resources locally and so
# are not checked in
override.tf
override.tf.json
*_override.tf
*_override.tf.json

# Include override files you do wish to add to version control using negated pattern
# !example_override.tf

# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
# example: *tfplan*

# Ignore CLI configuration files
.terraformrc
terraform.rc
44 changes: 44 additions & 0 deletions terraform-ec2/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions terraform-ec2/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}

provider "aws" {
region = "eu-west-2"
access_key = var.aws_access_key_id
secret_key = var.aws_secret_access_key
}

# module "vpc" {
# source = "./modules/vpc"
# cidr_block = "10.0.0.0/16"
# }

# module "security" {
# source = "./modules/security"
# vpc_id = module.vpc.my_vpc.id
# }

module "backend_instance" {
source = "./modules/ec2"
# ami_id = "ami-123456"
# instance_type = "t2.micro"
# subnet_id = module.vpc.private_subnet.id
# security_group_id = module.security.allow_web.id
# instance_name = "backend_instance"
}
37 changes: 37 additions & 0 deletions terraform-ec2/modules/ec2/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
resource "aws_instance" "app_instance" {
ami = var.ami_id
instance_type = var.instance_type
subnet_id = var.subnet_id
security_groups = [var.security_group_id]
key_name = "d-key"

tags = {
Name = "my_video_server"
}
}

resource "null_resource" "name" {
provisioner "remote-exec" {
connection {
type = "ssh"
user = "ec2-user"
private_key = file("/Users/dorissiu/Desktop/d-key.pem")
host = aws_instance.app_instance.public_ip
}

inline = [
"sudo yum update -y",
"sudo yum install docker -y",
"sudo systemctl start docker",
"sudo systemctl enable docker",
"sudo docker pull doris321/video-app:latest",
"sudo docker run -dp 80:5000 doris321/video-app"
]
}
# wait for ec2 to be created
depends_on = [aws_instance.app_instance]
}




25 changes: 25 additions & 0 deletions terraform-ec2/modules/ec2/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
variable "ami_id" {
type = string
description = "AWS ec2 ami_id"
default = "ami-0171207a7acd2a570"
}

variable "instance_type" {
type = string
description = "AWS ec2 instance_type"
default = "t2.micro"
}

variable "subnet_id" {
type = string
description = "AWS ec2 subnet_id"
default = "subnet-0dd915739859e4a31"
}

variable "security_group_id" {
type = string
description = "AWS ec2 security_group_id"
default = "sg-04c4f80b88dfa07ee"
}


33 changes: 33 additions & 0 deletions terraform-ec2/modules/security/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
resource "aws_security_group" "ec2_backend_sg" {
name = "ec2_backend_sg"
description = "SG for backend server"
vpc_id = var.vpc_id

egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}

ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

ingress {
from_port = 3389
to_port = 3389
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
5 changes: 5 additions & 0 deletions terraform-ec2/modules/security/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
variable "vpc_id" {
type = string
description = "AWS vpc_id"
default = "vpc-0d0953aaac41d4376"
}
9 changes: 9 additions & 0 deletions terraform-ec2/modules/vpc/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
resource "aws_vpc" "my_vpc" {
cidr_block = var.cidr_block
enable_dns_support = true
enable_dns_hostnames = true

tags = {
Name = "my_vpc"
}
}
5 changes: 5 additions & 0 deletions terraform-ec2/modules/vpc/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
variable "cidr_block" {
type = string
description = "AWS VPC cidr_block"
default = "10.0.0.0/16"
}
10 changes: 10 additions & 0 deletions terraform-ec2/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
variable "aws_access_key_id" {
type = string
description = "AWS Access Key ID"
}

variable "aws_secret_access_key" {
type = string
sensitive = true
description = "AWS Secret Access Key"
}

0 comments on commit 16083f4

Please sign in to comment.