Skip to content

Commit

Permalink
fix issue with not getting dir for script file
Browse files Browse the repository at this point in the history
  • Loading branch information
bansikah22 committed Nov 22, 2024
1 parent 1feaafb commit a5d5898
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 7 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ go.work.sum

# env file
.env
.idea
.idea
list-and-access.md
table.md
2 changes: 1 addition & 1 deletion terraform/aws/ec2_instance/basic/docs/deploy.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
ssh -i "auto-generated-key.pem" [email protected]
chmod 400 auto-generated-key.pem if you encounter problems with permissions

destroy ec2 machine
#destroy ec2 machine
terraform destroy -auto-approve
```
Below is a Terraform configuration that includes all the necessary components for a fully functional EC2 instance.
34 changes: 32 additions & 2 deletions terraform/aws/ec2_instance/basic/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,42 @@ resource "tls_private_key" "rsa_4096" {
rsa_bits = 4096
}

resource "local_file" "private_key" {
filename = "${path.module}/private_key.pem"
content = tls_private_key.rsa_4096.private_key_pem
}

# Create an AWS Key Pair using the generated public key
resource "aws_key_pair" "key_pair" {
key_name = var.key_name
public_key = tls_private_key.rsa_4096.public_key_openssh
}

# Retrieve the list of availability zones for the given AWS region
data "aws_availability_zones" "available" {
state = "available"
}

# Example usage for EBS volume
resource "aws_ebs_volume" "ebs" {
availability_zone = data.aws_availability_zones.available.names[0] # Use the first available AZ
size = 20 # Size in GiB
type = "gp2" # General Purpose SSD
tags = {
Name = "prod_dem-ebs-volume"
}
}



# Attach the EBS volume to the EC2 instance
resource "aws_volume_attachment" "ebs_attach" {
device_name = "/dev/xvdf" # Name of the device on the instance
volume_id = aws_ebs_volume.ebs.id # EBS volume ID
instance_id = aws_instance.production_dem.id # EC2 instance ID
}


# Allocate an Elastic IP
resource "aws_eip" "elastic_ip" {
instance = aws_instance.production_dem.id
Expand All @@ -25,10 +55,10 @@ resource "aws_instance" "production_dem" {
vpc_security_group_ids = [aws_security_group.ec2_security_group.id]

# User data script to set up a development environment on the EC2 instance
user_data = "${file("setup-dev-environment.sh")}"
user_data = file("${path.module}/script/setup-dev-environment.sh")

# Tags to identify the EC2 instance
tags = {
Name = "production_dem"
}
}
}
4 changes: 2 additions & 2 deletions terraform/aws/ec2_instance/basic/output.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ output "instance_public_ip" {

# Output the path to the private key file generated for the EC2 instance
output "private_key_path" {
description = "Path to the private key file"
value = local_file.private_key.filename
description = "Path to the private key file"
value = local_file.private_key.filename
}

# Output the ID of the VPC
Expand Down
11 changes: 10 additions & 1 deletion terraform/aws/ec2_instance/basic/route53.tf
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,13 @@ resource "aws_route53_record" "www_a_record" {
type = "A"
ttl = 300
records = [aws_eip.elastic_ip.public_ip]
}
}

# Create a wildcard A record for subdomains
resource "aws_route53_record" "wildcard_a_record" {
zone_id = aws_route53_zone.main.zone_id
name = "*"
type = "A"
ttl = 300
records = [aws_eip.elastic_ip.public_ip]
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,17 @@ pip3 --version
# Clean up and remove unnecessary packages to free up space
sudo apt autoremove -y

# Format the volume (ext4)
sudo mkfs -t ext4 /dev/xvdf

# Create a mount point directory
sudo mkdir -p /mnt/ebs

# Mount the volume
sudo mount /dev/xvdf /mnt/ebs

# Ensure persistence across reboots
echo "/dev/xvdf /mnt/ebs ext4 defaults,nofail 0 2" | sudo tee -a /etc/fstab

# Print a confirmation message after setup
echo "Development environment setup complete."

0 comments on commit a5d5898

Please sign in to comment.