AWS Infrastructure with Terraform

Create Terraform modules for AWS and provision AWS Infrastructure

Create dir
############
shashanksrivastava@197nodnb23093:~$ mkdir aws-terraform
shashanksrivastava@197nodnb23093:~$ cd aws-terraform/
shashanksrivastava@197nodnb23093:~/aws-terraform$ ls
shashanksrivastava@197nodnb23093:~/aws-terraform$ mkdir ec2 s3

Create modules for s3
#################
shashanksrivastava@197nodnb23093:~/aws-terraform$ mkdir modules
shashanksrivastava@197nodnb23093:~/aws-terraform$ cd modules/
shashanksrivastava@197nodnb23093:~/aws-terraform/modules$ mkdir s3
shashanksrivastava@197nodnb23093:~/aws-terraform/modules$ cd s3/
shashanksrivastava@197nodnb23093:~/aws-terraform/modules/s3$ vim main.tf

provider "aws" {
  region  = "${var.aws_region}"
}

resource "aws_s3_bucket" "s3_bucket" {
  bucket = "${var.bucket_name}"
  acl    = "${var.bucket_acl}"

  versioning {
      enabled = "${var.versioning_enabled}"
    }

  lifecycle {
      prevent_destroy = true
    }

  tags {
    name         = "${var.bucket_name}"
    organization = "${var.organization}"
  }
}

Create vars file
###########
shashanksrivastava@197nodnb23093:~/aws-terraform/modules/s3$ vim vars.tf

variable "aws_region" {}
variable "bucket_name" {}
variable "bucket_acl" {}
variable "versioning_enabled" {}
variable "organization" {}

Create s3 bucket file
################
shashanksrivastava@197nodnb23093:~/aws-terraform$ cd s3/
shashanksrivastava@197nodnb23093:~/aws-terraform/s3$ vim main.tf

module "s3" {
  source             = "../modules/s3/"
  aws_region          = "us-east-1"
  organization       = "shashanksrinfo"
  versioning_enabled = "true"
  bucket_name        = "shashanksrinfo1"
  bucket_acl          = "private"
}

Running Terraform init
#################
shashanksrivastava@197nodnb23093:~/aws-terraform/s3$ terraform  init
Initializing modules...
- module.s3

Initializing provider plugins...
............
Terraform has been successfully initialized!

Running Terraform plan
###################
shashanksrivastava@197nodnb23093:~/aws-terraform/s3$ terraform  plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.
------------------------------------------------------------------------
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:
...........................................
Plan: 1 to add, 0 to change, 0 to destroy.

Running Terraform apply
####################
shashanksrivastava@197nodnb23093:~/aws-terraform/s3$ terraform  apply

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:
................................................
Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

module.s3.aws_s3_bucket.s3_bucket: Creating...
............... 
module.s3.aws_s3_bucket.s3_bucket: Still creating... (10s elapsed)
module.s3.aws_s3_bucket.s3_bucket: Creation complete after 17s (ID: shashanksrinfo1)

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Create modules for EC2 
#################
shashanksrivastava@197nodnb23093:~/aws-terraform$ cd modules/
shashanksrivastava@197nodnb23093:~/aws-terraform/modules$ mkdir ec2
shashanksrivastava@197nodnb23093:~/aws-terraform/modules$ cd ec2/
shashanksrivastava@197nodnb23093:~/aws-terraform/modules/ec2$ vim main.tf

provider "aws" {
  region  = "${var.aws_region}"
}

resource "aws_instance" "instance" {
  count                       = "${var.instance_count}"
  ami                         = "${var.ami}"
  instance_type               = "${var.instance_type}"
  subnet_id                   = "${var.subnet_id}"
  vpc_security_group_ids      = ["${aws_security_group.instance.id}"]
  key_name                    = "${var.key_name}"
  associate_public_ip_address = "${var.associate_public_ip_address}"
  root_block_device {
   delete_on_termination = true
   volume_size           = "${var.instance_size}"
   volume_type           = "gp2"
  }
  tags {
   organization = "${var.organization}"
   Name = "${var.instance_name}"
  }
}

Create security group file
##################
shashanksrivastava@197nodnb23093:~/aws-terraform/modules/ec2$ vim sg.tf
resource "aws_security_group" "instance" {
 name        = "${var.sg_name}"
 description = "sg for instance"
 vpc_id      = "${var.vpc_id}"
 ingress {
   from_port        = 22
   to_port          = 22
   protocol         = "tcp"
   cidr_blocks      = ["0.0.0.0/0"]
 }
 egress {
   from_port        = 0
   to_port          = 0
   protocol         = -1
   cidr_blocks      = ["0.0.0.0/0"]
 }
 tags {
   organization = "${var.organization}"
   Name = "${var.instance_name}"
 }
}

Create vars file
###########
shashanksrivastava@197nodnb23093:~/aws-terraform/modules/ec2$ vim vars.tf
variable "aws_region" {}
variable "instance_count" {}
variable "ami" {}
variable "instance_type" {}
variable "subnet_id" {}
variable "key_name" {}
variable "associate_public_ip_address" {}
variable "instance_name" {}
variable "instance_size" {}
variable "organization" {}
variable "vpc_id" {}
variable "sg_name" {}

Create EC2 instance file
#################
shashanksrivastava@197nodnb23093:~/aws-terraform$ cd ec2/
shashanksrivastava@197nodnb23093:~/aws-terraform/ec2$ vim main.tf

module "instances" {
  source             = "../modules/ec2"
  aws_region         = "us-east-1"
  organization       = "shashanksrinfo"
  subnet_id         = "subnet-92f48cbc"
  key_name           = "shashanksrinfo"
  instance_count     = 1
  associate_public_ip_address = true
  instance_name      = "shashanksrinfo"
  instance_size      = 10
  instance_type      = "t2.micro"
  ami                = "ami-011b3ccf1bd6db744"
  vpc_id             = "vpc-92941de8"
  sg_name            = "shashanksrinfo"
}

Running Terraform init
#################
shashanksrivastava@197nodnb23093:~/aws-terraform/ec2$ terraform init
Initializing modules...
- module.instances

Initializing provider plugins...
.............
* provider.aws: version = "~> 2.4"

Terraform has been successfully initialized!

Running Terraform apply
#################
shashanksrivastava@197nodnb23093:~/aws-terraform/ec2$ terraform apply
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  + module.instances.aws_instance.instance
...........................................    
Plan: 2 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

module.instances.aws_security_group.instance: Creating...
.............................
module.instances.aws_security_group.instance: Still creating... (10s elapsed)
module.instances.aws_security_group.instance: Creation complete after 10s
module.instances.aws_instance.instance: Creating...
.................................
module.instances.aws_instance.instance: Still creating... (10s elapsed)
module.instances.aws_instance.instance: Still creating... (20s elapsed)
module.instances.aws_instance.instance: Still creating... (30s elapsed)
module.instances.aws_instance.instance: Still creating... (40s elapsed)
module.instances.aws_instance.instance: Creation complete after 47s

Comments

  1. A very interesting article! I have been thinking about innovations in my company for a long time. I did not know what I could do for a long time. Recently, I started to read a little about cloud solutions. I'm thinking about working with https://www.pro4people.com/about-us/. I saw that they have cool solutions, not only in this but also in other fields

    ReplyDelete
  2. Well Explained. Get more information from live experts through AWS Online Training

    ReplyDelete
  3. Your blog is in a convincing manner, thanks for sharing such an information with lots of your effort and time kubernetes online training

    ReplyDelete

Post a Comment

Popular posts from this blog

DevOps Interview Questions

Calico Certified Operator: AWS Expert Questions & Answers

CKAD Certification Exam Preparation Guide and Tips