Skip to content

Commit 758d27e

Browse files
committed
finish ec2 instance handson
1 parent c9f506b commit 758d27e

File tree

10 files changed

+270
-0
lines changed

10 files changed

+270
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Exclude Terraform files that shouldn't be shared
2+
*.tfstate
3+
*.tfstate.backup
4+
5+
# Exclude local variables files (e.g., secrets, passwords, etc.)
6+
*.auto.tfvars
7+
*.terraform/
8+
*.terraform.lock.hcl
9+
*.tfvars
10+
11+
# Ignore editor and system files
12+
*.swp
13+
*.bak
14+
*.tmp
15+
.DS_Store
16+
.idea/
17+
.vscode/
18+
*.log
19+
20+
# Ignore Terraform plan files
21+
*.tfplan
22+
23+
terraform.tfvars
24+
*.pem
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
data "aws_ami" "server_ami" {
2+
most_recent = true
3+
owners = ["099720109477"] # Change to a list of strings
4+
5+
filter {
6+
name = "name"
7+
values = ["ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-*"]
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
common commands
2+
Destroying the infrastructure.
3+
```bash
4+
tf destroy -auto-approve
5+
6+
tf fmt ## formatting file
7+
8+
ssh -i "auto-generated-key.pem" ubuntu@<public_ip_of instance>
9+
```
10+
A datasource is just a query to the aws api to receive information needed to deploy a resource
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Generate an RSA 4096-bit private key
2+
resource "tls_private_key" "rsa_4096" {
3+
algorithm = "RSA"
4+
rsa_bits = 4096
5+
}
6+
7+
# Create a local keypair with the private keypem format
8+
resource "local_file" "private_key" {
9+
filename = "${path.module}/var.key_name.pem"
10+
content = tls_private_key.rsa_4096.private_key_pem
11+
}
12+
13+
# Create an AWS Key Pair using the generated public key
14+
resource "aws_key_pair" "key_pair" {
15+
key_name = var.key_name
16+
public_key = tls_private_key.rsa_4096.public_key_openssh
17+
}
18+
19+
# Resource for instance
20+
resource "aws_instance" "nbt_node" {
21+
ami = data.aws_ami.server_ami.id
22+
instance_type = var.aws_instance_type
23+
key_name = aws_key_pair.key_pair.key_name
24+
security_groups = [aws_security_group.nbt_security_group.id]
25+
subnet_id = aws_subnet.nbt_public_subnet.id
26+
27+
root_block_device {
28+
volume_size = 20
29+
#volume_type = "gp2"
30+
}
31+
32+
# User data script to set up a development environment on the EC2 instance
33+
user_data = file("userdata.tpl")
34+
tags = {
35+
Name = "nbt_node"
36+
}
37+
38+
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Create a VPC
2+
resource "aws_vpc" "nbt_vpc" {
3+
cidr_block = "10.123.0.0/16"
4+
enable_dns_hostnames = true
5+
enable_dns_support = true
6+
7+
tags = {
8+
Name = "nbt-vpc"
9+
}
10+
}
11+
12+
# Create a public subnet and referencing resource
13+
resource "aws_subnet" "nbt_public_subnet" {
14+
vpc_id = aws_vpc.nbt_vpc.id
15+
cidr_block = "10.123.1.0/24"
16+
map_public_ip_on_launch = true
17+
availability_zone = "eu-central-1a"
18+
19+
tags = {
20+
Name = "nbt-public-subnet"
21+
}
22+
}
23+
24+
# Create an internet gateway
25+
resource "aws_internet_gateway" "nbt_igw" {
26+
vpc_id = aws_vpc.nbt_vpc.id
27+
28+
tags = {
29+
Name = "nbt-igw"
30+
}
31+
}
32+
33+
# Create a route table
34+
resource "aws_route_table" "nbt_public_rt" {
35+
vpc_id = aws_vpc.nbt_vpc.id
36+
37+
# route {
38+
# cidr_block = "0.0.0.0/0"
39+
# gateway_id = aws_internet_gateway.nbt_igw.id
40+
# }
41+
42+
tags = {
43+
Name = "nbt-public-rt"
44+
}
45+
}
46+
47+
# Create a route but can be created directly in the route table
48+
resource "aws_route" "default_route" {
49+
route_table_id = aws_route_table.nbt_public_rt.id
50+
destination_cidr_block = "0.0.0.0/0"
51+
gateway_id = aws_internet_gateway.nbt_igw.id
52+
}
53+
54+
# Create a route table association
55+
resource "aws_route_table_association" "nbt_public_assoc" {
56+
subnet_id = aws_subnet.nbt_public_subnet.id
57+
route_table_id = aws_route_table.nbt_public_rt.id
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
output "key_name" {
2+
value = aws_key_pair.key_pair.key_name
3+
description = "Name of the key pair"
4+
}
5+
6+
output "private_key" {
7+
value = file(aws_key_pair.key_pair.private_key_path)
8+
sensitive = true
9+
description = "Path to the private key file"
10+
}
11+
12+
output "public_ip" {
13+
value = aws_instance.nbt_node.public_ip
14+
description = "Public IP address of the EC2 instance"
15+
}
16+
17+
output "instance_id" {
18+
value = aws_instance.nbt_node.id
19+
description = "ID of the EC2 instance"
20+
}
21+
22+
output "aws_ami_id" {
23+
value = aws_instance.nbt_node.ami
24+
description = "AMI ID of the EC2 instance"
25+
}
26+
27+
output "aws_instance_type" {
28+
value = aws_instance.nbt_node.instance_type
29+
description = "Instance type of the EC2 instance"
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
terraform {
2+
required_providers {
3+
aws = {
4+
source = "hashicorp/aws"
5+
version = "~> 5.0"
6+
}
7+
}
8+
}
9+
10+
# Configure the AWS Provider
11+
provider "aws" {
12+
region = var.aws_region
13+
access_key = var.aws_access_key
14+
secret_key = var.aws_secret_key
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Create a security group
2+
resource "aws_security_group" "nbt_security_group" {
3+
name = "nbt-sg"
4+
description = "Allow SSH and HTTP inbound traffic"
5+
vpc_id = aws_vpc.nbt_vpc.id
6+
7+
ingress {
8+
description = "SSH"
9+
from_port = 22
10+
to_port = 22
11+
protocol = "tcp"
12+
cidr_blocks = ["0.0.0.0/0"]
13+
}
14+
15+
ingress {
16+
description = "HTTP"
17+
from_port = 80
18+
to_port = 80
19+
protocol = "tcp"
20+
cidr_blocks = ["0.0.0.0/0"]
21+
}
22+
23+
egress {
24+
from_port = 0
25+
to_port = 0
26+
protocol = "-1"
27+
cidr_blocks = ["0.0.0.0/0"]
28+
}
29+
30+
tags = {
31+
Name = "nbt-sg"
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
sudo apt-get update -y &&
3+
sudo apt-get install -y \
4+
apt-transport-https \
5+
ca-certificates \
6+
curl \
7+
gnupg-agent \
8+
software-properties-common &&
9+
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - &&
10+
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" &&
11+
sudo apt-get update -y &&
12+
sudo sudo apt-get install docker-ce docker-ce-cli containerd.io -y &&
13+
sudo usermod -aG docker ubuntu
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Define a variable for the AWS region
2+
variable "aws_region" {
3+
description = "AWS region"
4+
type = string
5+
default = "eu-central-1"
6+
}
7+
8+
# Define a variable for the key pair name
9+
variable "key_name" {
10+
description = "Name of the key pair"
11+
type = string
12+
default = "auto-generated-key"
13+
}
14+
15+
# aws access key
16+
variable "aws_access_key" {
17+
description = "AWS access key"
18+
type = string
19+
}
20+
21+
# aws secret key
22+
variable "aws_secret_key" {
23+
description = "AWS secret key"
24+
type = string
25+
}
26+
27+
# aws ami for instance
28+
variable "aws_ami" {
29+
description = "AWS ami for instance"
30+
type = string
31+
default = "ami-03cea216f9d507835"
32+
}
33+
34+
# aws instance type
35+
variable "aws_instance_type" {
36+
description = "AWS instance type"
37+
type = string
38+
default = "t2.micro"
39+
}

0 commit comments

Comments
 (0)