Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # backend Configuration
- provider "aws" {
- region = var.region
- default_tags {
- tags = {
- "Project" = var.project_name,
- "Env" = var.project_env
- }
- }
- }
- terraform {
- backend "s3" {
- bucket = "terraform-s3bucket-zomato"
- key = "dev/terraform.tfstate"
- region = "ap-south-1"
- }
- }
- # Secutity group creation
- resource "aws_security_group" "frontend" {
- name_prefix = "${var.project_name}-${var.project_env}-"
- description = "Allow ssh,http,https"
- ingress {
- description = "HTTPS"
- from_port = 443
- to_port = 443
- protocol = "tcp"
- cidr_blocks = ["0.0.0.0/0"]
- ipv6_cidr_blocks = ["::/0"]
- }
- ingress {
- description = "HTTP"
- from_port = 80
- to_port = 80
- protocol = "tcp"
- cidr_blocks = ["0.0.0.0/0"]
- ipv6_cidr_blocks = ["::/0"]
- }
- ingress {
- description = "SSH"
- from_port = 22
- to_port = 22
- protocol = "tcp"
- cidr_blocks = ["0.0.0.0/0"]
- ipv6_cidr_blocks = ["::/0"]
- }
- egress {
- from_port = 0
- to_port = 0
- protocol = "-1"
- cidr_blocks = ["0.0.0.0/0"]
- ipv6_cidr_blocks = ["::/0"]
- }
- tags = {
- Name = "${var.project_name}-${var.project_env}"
- }
- }
- # Ec2-instance creation
- resource "aws_instance" "shopping" {
- ami = var.ami
- instance_type = var.instance_type
- availability_zone = var.az
- key_name = var.key
- user_data = file("userdata.sh")
- vpc_security_group_ids = [aws_security_group.frontend.id]
- tags = {
- "Name" = "Shopping-app-template",
- }
- }
- # Fetching the details of the latest ami
- data "aws_ami" "shopping" {
- owners = ["self"]
- most_recent = true
- filter {
- name = "name"
- values = ["shopping-app-version-*"]
- }
- filter {
- name = "tag:Env"
- values = ["Dev"]
- }
- filter {
- name = "tag:Project"
- values = ["Zomato"]
- }
- }
- # Creating the Dev Instance from the latest AMI
- resource "aws_instance" "dev" {
- ami = data.aws_ami.shopping.id
- instance_type = var.instance_type
- availability_zone = var.az
- key_name = var.key
- vpc_security_group_ids = [aws_security_group.frontend.id]
- tags = {
- "Name" = "${var.project_name}-${var.project_env}-"
- }
- lifecycle {
- create_before_destroy = true
- }
- }
- # Fetching the details of the public hosted zone
- data "aws_route53_zone" "domain" {
- name = var.domain_name
- }
- # EIP allocation
- resource "aws_eip" "ip" {
- instance = aws_instance.dev.id
- vpc = true
- }
- # Record add
- resource "aws_route53_record" "www" {
- zone_id = data.aws_route53_zone.domain.zone_id
- name = var.domain_prefix
- type = "A"
- ttl = "5"
- records = [aws_eip.ip.public_ip]
- }
- ################### Variables.tf code ######################
- [ec2-user@ip-172-31-35-236 s3-versioning]$ cat variables.tf
- variable "project_name" {
- type = string
- default = "Zomato"
- }
- variable "project_env" {
- type = string
- default = "Dev"
- }
- variable "region" {
- description = "Region of the aws console where we execute the code"
- type = string
- default = "ap-south-1"
- }
- variable "domain_prefix" {
- type = string
- default = "blog"
- }
- variable "domain_name" {
- description = "Name of the domain"
- type = string
- default = "chrisich.fun"
- }
- variable "instance_type" {
- description = "instance type to launch"
- type = string
- default = "t2.micro"
- }
- variable "ami" {
- default = "ami-0b08bfc6ff7069aff"
- }
- variable "az" {
- description = "which AZ"
- type = string
- default = "ap-south-1a"
- }
- variable "key" {
- type = string
- default = "mumbai-new"
- }
- ###################### Output.tf code ###################
- [ec2-user@ip-172-31-35-236 s3-versioning]$ cat output.tf
- output "public_ip_zomato" {
- value = aws_eip.ip.public_ip
- }
- output "http_url_zomato" {
- value = "http://${aws_instance.dev.public_dns}"
- }
- output "AMI_ID_latest" {
- value = data.aws_ami.shopping.id
- }
- output "frontend_URL" {
- value = "http://${var.domain_prefix}.${var.domain_name}"
- }
- output "public_ip_template" {
- value = aws_instance.shopping.public_ip
- }
- ############################# Terraform Output ############################
- [ec2-user@ip-172-31-35-236 s3-versioning]$ terraform output
- AMI_ID_latest = "ami-03a12834ecdf42438"
- frontend_URL = "http://blog.chrisich.fun"
- http_url_zomato = "http://ec2-13-200-54-5.ap-south-1.compute.amazonaws.com"
- public_ip_template = "43.205.233.33"
- public_ip_zomato = "13.200.54.5"
- [ec2-user@ip-172-31-35-236 s3-versioning]$
- ############################## Terraform STATE show ############################
- [ec2-user@ip-172-31-35-236 s3-versioning]$ terraform state show data.aws_ami.shopping
- # data.aws_ami.shopping:
- data "aws_ami" "shopping" {
- architecture = "x86_64"
- arn = "arn:aws:ec2:ap-south-1::image/ami-03a12834ecdf42438"
- block_device_mappings = [
- {
- device_name = "/dev/xvda"
- ebs = {
- "delete_on_termination" = "true"
- "encrypted" = "false"
- "iops" = "3000"
- "snapshot_id" = "snap-09d60775e858703f1"
- "throughput" = "125"
- "volume_size" = "8"
- "volume_type" = "gp3"
- }
- no_device = ""
- virtual_name = ""
- },
- ]
- boot_mode = "uefi-preferred"
- creation_date = "2023-05-12T11:06:09.000Z"
- description = "shopping-app-version-3"
- ena_support = true
- hypervisor = "xen"
- id = "ami-03a12834ecdf42438"
- image_id = "ami-03a12834ecdf42438"
- image_location = "945577359662/shopping-app-version-3"
- image_type = "machine"
- imds_support = "v2.0"
- include_deprecated = false
- most_recent = true
- name = "shopping-app-version-3"
- owner_id = "945577359662"
- owners = [
- "self",
- ]
- platform_details = "Linux/UNIX"
- product_codes = []
- public = false
- root_device_name = "/dev/xvda"
- root_device_type = "ebs"
- root_snapshot_id = "snap-09d60775e858703f1"
- sriov_net_support = "simple"
- state = "available"
- state_reason = {
- "code" = "UNSET"
- "message" = "UNSET"
- }
- tags = {
- "Env" = "Dev"
- "Name" = "shopping-app-version-3"
- "Project" = "Zomato"
- }
- usage_operation = "RunInstances"
- virtualization_type = "hvm"
- filter {
- name = "name"
- values = [
- "shopping-app-version-*",
- ]
- }
- filter {
- name = "tag:Env"
- values = [
- "Dev",
- ]
- }
- filter {
- name = "tag:Project"
- values = [
- "Zomato",
- ]
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment