Tuesday, June 8, 2021

Install Apache package in EC2 instance using Terraform

Description: In the previous topic, I have explained, How to create a blank EC2 instance using Terraform. In this topic, I am creating an EC2 instance with a Pre-installed Apache package with the instance.

  • First I am creating an apache install shell script for Linux with the name install-apache.sh as follow and save it on the project folder

# installl-apache.sh #!/bin/bash -xe cd /tmp yum update -y yum install -y httpd echo "Hello from the EC2 instance $(hostname -f)." > /var/www/html/index.html systemctl start httpd

  • After creating the script file, Now I am creating terraform project file to create EC2 Instance  as follow
provider "aws" { access_key = "AKIAU4DZPCIQVSO67EPG" secret_key = "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" region = "us-east-1" } resource "aws_instance" "Webserver" { ami = "ami-0d5eff06f840b45e9" instance_type = "t2.micro" tags = { Name = "Web server" } vpc_security_group_ids = [ "sg-01df6b2fc0470ce65" ] user_data = "${file("install_apache.sh")}" key_name = "Ansible" }

  • Now, I am going to run terraform project using terraform terminal as follow
# terraform init


  • To review terraform project run terraform plan to list action 
# terraform plan



  • After review, Apply project using terraform apply command
# terraform apply


  • Now verify the instance, by browsing the IP address in the browser. Also, make sure to allow port 80 [HTTP] service in the security group which you have used in terraform project


8 comments: