Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions terraform-aws-ec2-userdata/apache_config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#! /bin/bash
sudo yum update -y
sudo yum install -y httpd.x86_64
sudo yum service httpd start
sudo yum service httpd enable
echo "<h1>Deployed via Terraform</h1>" | yum tee /var/www/html/index.html
6 changes: 6 additions & 0 deletions terraform-aws-ec2-userdata/output.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
output "public_ip" {
value = "${aws_instance.user_data_example.public_ip}"
}
output "user_data_example_input_file" {
value = "${aws_instance.user_data_example_input_file.public_ip}"
}
4 changes: 4 additions & 0 deletions terraform-aws-ec2-userdata/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
provider "aws" {
region = "${var.region}"
version = "~> 2.0"
}
28 changes: 28 additions & 0 deletions terraform-aws-ec2-userdata/security_group.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
resource "aws_security_group" "allow_ssh" {
name = "allow_SSH"
description = "Allow SSH inbound traffic"
#vpc_id = aws_vpc.vpc_demo.id

ingress {
# SSH Port 22 allowed from any IP
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

ingress {
# SSH Port 80 allowed from any IP
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"]
}
}
17 changes: 17 additions & 0 deletions terraform-aws-ec2-userdata/user-data-file-input.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

resource "aws_instance" "user_data_example_input_file" {
ami = lookup(var.ami_id, var.region)
instance_type = var.instance_type
# subnet_id = aws_subnet.public_1.id

# Security group assign to instance
vpc_security_group_ids = [aws_security_group.allow_ssh.id]

# key name
key_name = var.key_name
user_data = "${file("apache_config.sh")}"

tags = {
Name = "Ec2-User-data-with-file"
}
}
25 changes: 25 additions & 0 deletions terraform-aws-ec2-userdata/user_data.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

resource "aws_instance" "user_data_example" {
ami = lookup(var.ami_id, var.region)
instance_type = var.instance_type
# subnet_id = aws_subnet.public_1.id

# Security group assign to instance
vpc_security_group_ids = [aws_security_group.allow_ssh.id]

# key name
key_name = var.key_name

user_data = <<EOF
#! /bin/bash
sudo yum update -y
sudo yum install -y httpd.x86_64
sudo service httpd start
sudo service httpd enable
echo "<h1>Deployed via Terraform</h1>" | sudo tee /var/www/html/index.html
EOF

tags = {
Name = "Ec2-User-data"
}
}
20 changes: 20 additions & 0 deletions terraform-aws-ec2-userdata/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
variable "region" {
type = "string"
default = "us-east-1"
}
variable "ami_id" {
type = "map"
default = {
us-east-1 = "ami-035b3c7efe6d061d5"
eu-west-2 = "ami-132b3c7efe6sdfdsfd"
eu-central-1 = "ami-9787h5h6nsn75gd33"
}
}
variable "instance_type" {
type = "string"
default = "t2.micro"
}
variable "key_name" {
type = "string"
default = "ec2-demo"
}