Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ########################_______ With variables ________#################
- [ec2-user@ip-172-31-41-59 zomato-mumbai]$ cat main.tf
- provider "aws" {
- region = "ap-south-1"
- }
- ### Variables ###
- variable "project_name" {
- default = "Zomato"
- }
- variable "project_env" {
- default = "Dev"
- }
- variable "ami_mumbai" {
- default = "ami-0b08bfc6ff7069aff"
- }
- variable "ami_ohio" {
- default = "ami-08333bccc35d71140"
- }
- # Key pair generation
- resource "aws_key_pair" "deployer" {
- key_name = "Zomato"
- public_key = file("zomato.pub")
- }
- # Security group generation
- resource "aws_security_group" "traffic" {
- name_prefix = "${var.project_name} - ${var.project_env} - "
- description = "Allow ssh and http"
- 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}"
- }
- }
- resource "aws_instance" "frontend" {
- ami = var.ami_mumbai
- instance_type = "t2.micro"
- key_name = aws_key_pair.deployer.id
- vpc_security_group_ids = [ aws_security_group.traffic.id ]
- tags = {
- "Name" = "${var.project_name} - ${var.project_env}"
- }
- }
- Creayed the
- ### Output ####
- Plan: 3 to add, 0 to change, 0 to destroy.
- ########################_______ With map using data.aws_reion ________#################
- data "aws_region" "current" {}
- provider "aws" {
- region = "us-east-2"
- }
- ### Variables ###
- variable "project_name" {
- default = "Zomato"
- }
- variable "project_env" {
- default = "Dev"
- }
- variable "ami_id" {
- type = map
- default = {
- "ap-south-1" = "ami-0b08bfc6ff7069aff",
- "us-east-2" = "ami-08333bccc35d71140"
- }
- }
- # Key pair generation
- resource "aws_key_pair" "deployer" {
- key_name = "Zomato"
- public_key = file("zomato.pub")
- }
- # Security group generation
- resource "aws_security_group" "traffic" {
- name_prefix = "${var.project_name}-${var.project_env} - "
- description = "Allow ssh and http"
- 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}"
- }
- }
- resource "aws_instance" "frontend" {
- ami = var.ami_id["${data.aws_region.current.name}"]
- instance_type = "t2.micro"
- key_name = aws_key_pair.deployer.id
- vpc_security_group_ids = [ aws_security_group.traffic.id ]
- tags = {
- "Name" = "${var.project_name} - ${var.project_env}"
- }
- }
- ###Output####
- Plan: 3 to add, 0 to change, 0 to destroy.
- ########################_______ With map using variables ________#################
- provider "aws" {
- region = "us-east-2"
- }
- ### Variables ###
- variable "project_name" {
- default = "Zomato"
- }
- variable "region" {
- default = "us-east-2"
- }
- variable "project_env" {
- default = "Dev"
- }
- variable "ami_id" {
- type = map
- default = {
- "ap-south-1" = "ami-0b08bfc6ff7069aff",
- "us-east-2" = "ami-08333bccc35d71140"
- }
- }
- # Key pair generation
- resource "aws_key_pair" "deployer" {
- key_name = "Zomato"
- public_key = file("zomato.pub")
- }
- # Security group generation
- resource "aws_security_group" "traffic" {
- name_prefix = "${var.project_name}-${var.project_env} - "
- description = "Allow ssh and http"
- 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}"
- }
- }
- resource "aws_instance" "frontend" {
- ami = var.ami_id["${var.region}"]
- instance_type = "t2.micro"
- key_name = aws_key_pair.deployer.id
- vpc_security_group_ids = [ aws_security_group.traffic.id ]
- tags = {
- "Name" = "${var.project_name} - ${var.project_env}"
- }
- }
- ####### Lookup ##########
- provider "aws" {
- region = var.region
- }
- variable "region" {
- default = "ap-south-1"
- }
- variable "classmates_exp" {
- description = "My classmates total IT experience"
- type = map
- default = {
- "Chris" = "2",
- "ashna" = "4",
- "vysakh" = "9"
- }
- }
- output "chris_exp" {
- value = lookup(var.classmates_exp,"Chris",0)
- }
- output "ashna_exp" {
- value = lookup(var.classmates_exp,"ashna",0)
- }
- output "vysak_exp" {
- value = var.classmates_exp.vysakh
- }
- output "prince_exp" {
- value = lookup(var.classmates_exp,"prince",0)
- }
- #### Output ####
- Changes to Outputs:
- + ashna_exp = "4"
- + chris_exp = "2"
- + prince_exp = "0"
- + vysak_exp = "9"
- You can apply this plan to save these new output values to the Terraform state, without changing any real
- infrastructure.
- ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
- Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if
- you run "terraform apply" now.
- [ec2-user@ip-172-31-41-59 lookup]$
- #########________Copying files using lookup_________######
- provider "aws" {
- region = "ap-south-1"
- }
- variable "mime_type" {
- type = map(any)
- default = {
- "css" = "text/css"
- "html" = "text/html"
- "ico" = "image/vnd.microsoft.icon"
- "js" = "application/javascript"
- "json" = "application/json"
- "map" = "application/json"
- "png" = "image/png"
- "svg" = "image/svg+xml"
- "txt" = "text/plain"
- "jpg" = "image/jpeg"
- "woff" = "application/font-woff"
- "woff2" = "application/font-woff2"
- }
- }
- resource "aws_s3_object" "object" {
- bucket = "terraform-s3bucket-zomato"
- for_each = fileset("/home/ec2-user/test", "**/*.*")
- key = each.key
- source = "/home/ec2-user/test/${each.key}"
- content_type = lookup(var.mime_type, element(split(".", each.key), length(split(".", each.key)) - 1))
- }
- ###Output ###
- Apply complete! Resources: 32 added, 0 changed, 0 destroyed.
- [ec2-user@ip-172-31-41-59 lookup]$
Advertisement
Add Comment
Please, Sign In to add comment