Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ######## Packer in one region creation #########
- [ec2-user@ip-172-31-41-59 zomato-test-mumbai]$ cat main.pkr.hcl
- variable "project_name" {
- default = "Zomato"
- }
- variable "project_env" {
- default = "Dev"
- }
- variable "region" {
- default = "ap-south-1"
- }
- variable "ami_id" {
- default = "ami-0b08bfc6ff7069aff"
- }
- variable "instance_type" {
- default = "t2.micro"
- }
- locals {
- image-timestamp = "${formatdate("DD-MMM-YYYY-hh-mm", timestamp())}"
- }
- locals {
- ami_name = "${var.project_name}-${var.project_env}-${local.image-timestamp}"
- }
- source "amazon-ebs" "webserver" {
- ami_name = local.ami_name
- instance_type = var.instance_type
- region = var.region
- source_ami = var.ami_id
- ssh_username = "ec2-user"
- tags = {
- "Name" = "${local.ami_name}",
- "Project" = "${var.project_name}",
- "Env" = "${var.project_env}"
- }
- }
- build {
- sources = ["sourve.amazon-ebs.webserver"]
- provisioner "shell" {
- script = "userdata.sh"
- execute_command = "sudo {{.Path}}"
- }
- }
- ####Output#####
- ==> Wait completed after 3 minutes 19 seconds
- ==> Builds finished. The artifacts of successful builds are:
- --> amazon-ebs.webserver: AMIs were created:
- ap-south-1: ami-08d7a1a325fe6bdf1
- ################ Testing Random ID ############
- locals {
- ami_name = "${var.project_name}-${var.project_env}-${uuidv4()}"
- }
- [ec2-user@ip-172-31-41-59 zomato-test-mumbai]$ packer console .
- > local.ami_name
- > Zomato-Dev-2701a25a-a605-41eb-b96a-2de8396de7ea
- > exit
- [ec2-user@ip-172-31-41-59 zomato-test-mumbai]$ packer console .
- > local.ami_name
- > Zomato-Dev-eb3b0051-392b-412a-a053-91be8938faf5
- > exit
- #######################_________ Instance spin up in one region _________________##################
- provider "aws" {
- region = "ap-south-1"
- default_tags {
- tags = {
- "Project" = var.project_name,
- "Env" = var.project_env
- }
- }
- }
- variable "project_name" {
- default = "Zomato"
- }
- variable "project_env" {
- default = "Dev"
- }
- variable "domain" {
- type = string
- default = "webserver-mumbai"
- }
- variable "instance_type" {
- default = "t2.micro"
- }
- # Fetching the details from AWS
- data "aws_ami" "example" {
- owners = ["self"]
- most_recent = true
- filter {
- name = "name"
- values = ["${var.project_name}-${var.project_env}-*"]
- }
- filter {
- name = "tag:Env"
- values = ["${var.project_env}"]
- }
- filter {
- name = "tag:Project"
- values = ["${var.project_name}"]
- }
- }
- data "aws_route53_zone" "selected" {
- name = "chrisich.fun"
- }
- ### Elastic IP allocation #####
- resource "aws_eip" "web" {
- instance = aws_instance.frontend.id
- vpc = true
- }
- #### Record Creation #####
- resource "aws_route53_record" "domain" {
- zone_id = data.aws_route53_zone.selected.id
- name = var.domain
- type = "A"
- ttl = "5"
- records = [aws_eip.web.public_ip]
- }
- #### SG Creattion #####
- resource "aws_security_group" "frontend" {
- name_prefix = "${var.project_name}-${var.project_env}-"
- description = "allows http & ssh traffic"
- ingress {
- from_port = 80
- to_port = 80
- protocol = "tcp"
- cidr_blocks = ["0.0.0.0/0"]
- ipv6_cidr_blocks = ["::/0"]
- }
- ingress {
- 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}-fronetnd"
- }
- lifecycle {
- create_before_destroy = true
- }
- }
- #### Key pair creation #####
- resource "aws_key_pair" "deployer" {
- key_name = "Oasis"
- public_key = file("oasis.pub")
- }
- resource "aws_instance" "frontend" {
- ami = data.aws_ami.example.id
- instance_type = var.instance_type
- key_name = aws_key_pair.deployer.key_name
- vpc_security_group_ids = [aws_security_group.frontend.id]
- tags = {
- "Name" = "${var.project_name}-${var.project_env}-fronetnd"
- }
- }
- output "URL" {
- value = "http://${var.domain}.${data.aws_route53_zone.selected.name}"
- }
- ###############_______________ Multi region packer _____________#######################
- variable "project_name" {
- default = "Zomato"
- }
- variable "project_env" {
- default = "Dev"
- }
- variable "region" {
- type = map(string)
- default = {
- "region1" = "ap-south-1",
- "region2" = "us-east-2"
- }
- }
- variable "ami_id" {
- type = map(string)
- default = {
- "region1" = "ami-0b08bfc6ff7069aff"
- "region2" = "ami-08333bccc35d71140"
- }
- }
- variable "instance_type" {
- default = "t2.micro"
- }
- locals {
- image-timestamp = "${formatdate("DD-MMM-YYYY-hh-mm", timestamp())}"
- }
- locals {
- ami_name = "${var.project_name}-${var.project_env}-${uuidv4()}"
- }
- source "amazon-ebs" "webserver-mumbai" {
- ami_name = local.ami_name
- instance_type = var.instance_type
- region = var.region.region1
- source_ami = var.ami_id.region1
- ssh_username = "ec2-user"
- tags = {
- "Name" = "${local.ami_name}",
- "Project" = "${var.project_name}",
- "Env" = "${var.project_env}"
- }
- }
- source "amazon-ebs" "webserver-ohio" {
- ami_name = local.ami_name
- instance_type = var.instance_type
- region = var.region.region2
- source_ami = var.ami_id.region2
- ssh_username = "ec2-user"
- tags = {
- "Name" = "${local.ami_name}",
- "Project" = "${var.project_name}",
- "Env" = "${var.project_env}"
- }
- }
- build {
- sources = ["source.amazon-ebs.webserver-mumbai", "source.amazon-ebs.webserver-ohio"]
- provisioner "shell" {
- script = "userdata.sh"
- execute_command = "sudo {{.Path}}"
- }
- }
- ##############______ Ohio Provision __________________#########
- ec2-user@ip-172-31-41-59 zomato-test-mumbai]$ cat ../prod-zomato/main.tf
- provider "aws" {
- region = "us-east-2"
- default_tags {
- tags = {
- "Project" = var.project_name,
- "Env" = var.project_env
- }
- }
- }
- variable "project_name" {
- default = "Zomato"
- }
- variable "project_env" {
- default = "Dev"
- }
- variable "domain" {
- type = string
- default = "webserver-ohio"
- }
- variable "instance_type" {
- default = "t2.micro"
- }
- # Fetching the details from AWS
- data "aws_ami" "example" {
- owners = ["self"]
- most_recent = true
- filter {
- name = "name"
- values = ["${var.project_name}-${var.project_env}-*"]
- }
- filter {
- name = "tag:Env"
- values = ["${var.project_env}"]
- }
- filter {
- name = "tag:Project"
- values = ["${var.project_name}"]
- }
- }
- data "aws_route53_zone" "selected" {
- name = "chrisich.fun"
- }
- ### Elastic IP allocation #####
- resource "aws_eip" "web" {
- instance = aws_instance.frontend.id
- vpc = true
- }
- resource "aws_route53_record" "domain" {
- zone_id = data.aws_route53_zone.selected.id
- name = var.domain
- type = "A"
- ttl = "5"
- records = [aws_eip.web.public_ip]
- }
- resource "aws_security_group" "frontend" {
- name_prefix = "${var.project_name}-${var.project_env}-"
- description = "allows http & ssh traffic"
- ingress {
- from_port = 80
- to_port = 80
- protocol = "tcp"
- cidr_blocks = ["0.0.0.0/0"]
- ipv6_cidr_blocks = ["::/0"]
- }
- ingress {
- 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}-fronetnd"
- }
- lifecycle {
- create_before_destroy = true
- }
- }
- resource "aws_key_pair" "deployer" {
- key_name = "Oasis"
- public_key = file("oasis.pub")
- }
- resource "aws_instance" "frontend" {
- ami = data.aws_ami.example.id
- instance_type = var.instance_type
- key_name = aws_key_pair.deployer.key_name
- vpc_security_group_ids = [aws_security_group.frontend.id]
- tags = {
- "Name" = "${var.project_name}-${var.project_env}-fronetnd"
- }
- }
- output "URL" {
- value = "http://${var.domain}.${data.aws_route53_zone.selected.name}"
- }
- ####Output#####
- [ec2-user@ip-172-31-41-59 prod-zomato]$ terraform output
- URL = "http://webserver-ohio.chrisich.fun"
Advertisement
Add Comment
Please, Sign In to add comment