Terraform Series - Launching WebServer with UserData
Today we are going to see:
1) Launching a EC2 Instance.
2) Configure HTTP server using a script which will be executed once the EC2 instance created.
3) Update the Security Group to allow incoming traffic on port "80".
4) Finally verify accessing the public IP/index.html
We are going to create a file which has the steps to configure HTTP server.
#! /bin/bash
sudo yum update -y
sudo yum install -y httpd
sudo systemctl enable httpd
sudo service httpd start
sudo echo '<h1>Welcome to My App - APP-1</h1>' | sudo tee /var/www/html/index.html
sudo mkdir /var/www/html/app1
sudo echo '<!DOCTYPE html> <html> <body style="background-color:rgb(250, 210, 210);">
<h1>Welcome to APP-1 </body></html>' | sudo tee /var/www/html/app1/index.html
sudo curl http://169.254.169.254/latest/dynamic/instance-identity/document -o /var/www/html/app1/metadata.html
--------------------------------------------------------------------------------------
# Terraform Block
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
}
}
}
# Provider Block
provider "aws" {
profile = "default" # AWS Credentials configured via AWS CLI.
region = "us-east-1"
}
# Resource Block
resource "aws_instance" "ec2demo" {
ami = "ami-0b5eea76982371e91" # Amazon Linux in us-east-1
instance_type = "t2.micro"
user_data = file("${path.module}/appinstall.sh") -> Calling the USERDATA Script.
tags = {
"Name" = "webserver"
}
}
resource "aws_security_group_rule" "ec2demoingress" {
type = "ingress"
from_port = 0
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
security_group_id = "sg-0495b116a9de10f0c"
}
Comments
Post a Comment