Keypair
variable "EC2_Key" {default="httpdserverkey"}
resource "tls_private_key" "httpdkey" {
algorithm = "RSA"
rsa_bits = 4096
}
// Creating AWS key-pair
resource "aws_key_pair" "li_key" {
key_name = var.EC2_Key
public_key = tls_private_key.httpdkey.public_key_openssh
}
// Creating security group
resource "aws_security_group" "httpdsecurity" {
depends_on = [
aws_key_pair.li_key,
]
name = "httpdsecurity"
description = "allow ssh and httpd"
ingress {
description = "SSH Port"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "HTTPD Port"
from_port = 80
to_port = 80
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 = "httpdsecurity"
}
}
Ubuntu
// Creating EC2 Instance and Installing Required Softwares in it.
resource "aws_instance" "HttpdInstance" {
depends_on = [
aws_security_group.httpdsecurity,
]
ami = "ami-0447a12f28fddb066"
instance_type = "t2.micro"
key_name = var.EC2_Key
security_groups = [ "${aws_security_group.httpdsecurity.name}" ]
connection {
type = "ssh"
user = "ec2-user"
private_key = tls_private_key.httpdkey.private_key_pem
host = aws_instance.HttpdInstance.public_ip
}
provisioner "remote-exec" {
inline = [
"sudo apt-get update",
"sudo apt-get install apache2 -y",
"sudo HttpdServer1 start apache2",
]
}
tags = {
Name = "HttpdServer1"
}
}
Top comments (0)