From 19dfae3a9cb2d4a2ac780343e64343167feb8eba Mon Sep 17 00:00:00 2001 From: Rageshmookoth Date: Tue, 13 Apr 2021 04:46:43 -0400 Subject: [PATCH 01/68] Update provider.tf Added access key variables --- provider.tf | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/provider.tf b/provider.tf index 88f0a18..7e58244 100644 --- a/provider.tf +++ b/provider.tf @@ -1,3 +1,5 @@ provider "aws" { region = "ap-south-1" -} \ No newline at end of file + access_key = "RAG-ACCESS-KEY" + secret_key = "RAG-SECRET-KEY" +} From 0df6d793954a011f25ea1930a5967d336db35406 Mon Sep 17 00:00:00 2001 From: Rageshmookoth Date: Tue, 13 Apr 2021 07:05:34 -0400 Subject: [PATCH 02/68] Update provider.tf Removed security codes --- provider.tf | 2 -- 1 file changed, 2 deletions(-) diff --git a/provider.tf b/provider.tf index 7e58244..3aafc50 100644 --- a/provider.tf +++ b/provider.tf @@ -1,5 +1,3 @@ provider "aws" { region = "ap-south-1" - access_key = "RAG-ACCESS-KEY" - secret_key = "RAG-SECRET-KEY" } From c907774e8745b9f2c8b9786a7d749fff5072f536 Mon Sep 17 00:00:00 2001 From: Ragesh Date: Tue, 13 Apr 2021 09:04:42 -0400 Subject: [PATCH 03/68] initial --- outputs.tf | 6 ++++++ variables.tf | 13 +++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 outputs.tf create mode 100644 variables.tf diff --git a/outputs.tf b/outputs.tf new file mode 100644 index 0000000..7176a74 --- /dev/null +++ b/outputs.tf @@ -0,0 +1,6 @@ +output "jenkins_master_alb_dns_name" { + description = "Jenkins Master Application Load Balancer DNS Name" + value = module.ecs-fargate-service.aws_lb_lb_dns_name +} +/// comments added/// +//chnaged// \ No newline at end of file diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..2d6e0ef --- /dev/null +++ b/variables.tf @@ -0,0 +1,13 @@ +#------------------------------------------------------------------------------ +# Misc +#------------------------------------------------------------------------------ +variable "name_prefix" { + description = "Name prefix for resources on AWS" +} + +#------------------------------------------------------------------------------ +# AWS CREDENTIALS AND REGION +#------------------------------------------------------------------------------ +variable "region" { + description = "AWS Region the infrastructure is hosted in" +} From fe5c6128742aed88fc5f59df5f4a838235f01e26 Mon Sep 17 00:00:00 2001 From: Ragesh Date: Tue, 13 Apr 2021 09:18:35 -0400 Subject: [PATCH 04/68] ec2-build --- ec2.tf | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 ec2.tf diff --git a/ec2.tf b/ec2.tf new file mode 100644 index 0000000..2cbd461 --- /dev/null +++ b/ec2.tf @@ -0,0 +1,7 @@ +resource "aws_instance" "web" { + ami = ami-0bcf5425cdc1d8a85 + instance_type = "t3.micro" + + tags = { + Name = "Terraform-Server" + } \ No newline at end of file From 2a301bb20a3d1624347e73e591fa796b7151ce17 Mon Sep 17 00:00:00 2001 From: Rageshmookoth Date: Tue, 13 Apr 2021 15:15:34 -0400 Subject: [PATCH 05/68] upload --- ec2.tf | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/ec2.tf b/ec2.tf index 2cbd461..20d40c4 100644 --- a/ec2.tf +++ b/ec2.tf @@ -1,7 +1,12 @@ -resource "aws_instance" "web" { - ami = ami-0bcf5425cdc1d8a85 - instance_type = "t3.micro" - - tags = { - Name = "Terraform-Server" - } \ No newline at end of file +provider "aws" { +region = "us-east-1" +} +resource "aws_instance" "Ragesh-n" { + ami = "ami-0bcf5425cdc1d8a85" + instance_type = "t3.micro" + key_name = "terraform-Jenkins" + tags { + Name = "test instance" + Owner = "Ragesh Moorkoth" + } +} \ No newline at end of file From 928e3904a6ee9f8cb875c51194ba30b745b85665 Mon Sep 17 00:00:00 2001 From: Ragesh Date: Tue, 13 Apr 2021 15:17:04 -0400 Subject: [PATCH 06/68] nameupdated --- ec2.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ec2.tf b/ec2.tf index 20d40c4..c9a40a5 100644 --- a/ec2.tf +++ b/ec2.tf @@ -1,7 +1,7 @@ provider "aws" { region = "us-east-1" } -resource "aws_instance" "Ragesh-n" { +resource "aws_instance" "Ragesh-new" { ami = "ami-0bcf5425cdc1d8a85" instance_type = "t3.micro" key_name = "terraform-Jenkins" From 8311af86da5d5d7dd1fb0a17a0d4c4da2f98980a Mon Sep 17 00:00:00 2001 From: Ragesh Date: Tue, 13 Apr 2021 15:19:18 -0400 Subject: [PATCH 07/68] fixed-error --- ec2.tf | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ec2.tf b/ec2.tf index c9a40a5..c90074c 100644 --- a/ec2.tf +++ b/ec2.tf @@ -1,6 +1,6 @@ -provider "aws" { -region = "us-east-1" -} +//provider "aws" { +//region = "us-east-1" +//} resource "aws_instance" "Ragesh-new" { ami = "ami-0bcf5425cdc1d8a85" instance_type = "t3.micro" From 3e388bbe22d6816fa824983530318d77528b6ff6 Mon Sep 17 00:00:00 2001 From: Ragesh Date: Tue, 13 Apr 2021 15:20:59 -0400 Subject: [PATCH 08/68] Version-1 --- ec2.tf | 3 --- 1 file changed, 3 deletions(-) diff --git a/ec2.tf b/ec2.tf index c90074c..27fe34d 100644 --- a/ec2.tf +++ b/ec2.tf @@ -1,6 +1,3 @@ -//provider "aws" { -//region = "us-east-1" -//} resource "aws_instance" "Ragesh-new" { ami = "ami-0bcf5425cdc1d8a85" instance_type = "t3.micro" From 3c21a58beeb1413fbea822246d98e204b031b641 Mon Sep 17 00:00:00 2001 From: Ragesh Date: Tue, 13 Apr 2021 15:28:17 -0400 Subject: [PATCH 09/68] ec2-config.updated --- ec2.tf | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/ec2.tf b/ec2.tf index 27fe34d..eaa8783 100644 --- a/ec2.tf +++ b/ec2.tf @@ -1,9 +1,4 @@ resource "aws_instance" "Ragesh-new" { - ami = "ami-0bcf5425cdc1d8a85" - instance_type = "t3.micro" - key_name = "terraform-Jenkins" - tags { - Name = "test instance" - Owner = "Ragesh Moorkoth" - } -} \ No newline at end of file +ami = "ami-0bcf5425cdc1d8a85" +instance_type = "t3.micro" +key_name = "terraform-Jenkins" \ No newline at end of file From dd5671e4cd982cd50f2b973e5c307bb33911f3ea Mon Sep 17 00:00:00 2001 From: Ragesh Date: Tue, 13 Apr 2021 15:29:34 -0400 Subject: [PATCH 10/68] updated version --- ec2.tf | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ec2.tf b/ec2.tf index eaa8783..b3f3c19 100644 --- a/ec2.tf +++ b/ec2.tf @@ -1,4 +1,5 @@ resource "aws_instance" "Ragesh-new" { ami = "ami-0bcf5425cdc1d8a85" instance_type = "t3.micro" -key_name = "terraform-Jenkins" \ No newline at end of file +key_name = "terraform-Jenkins" +} \ No newline at end of file From 23d1d62bc0d8d7398f0e768b97c065f882397611 Mon Sep 17 00:00:00 2001 From: Ragesh Date: Tue, 13 Apr 2021 15:33:13 -0400 Subject: [PATCH 11/68] deleted-output --- outputs.tf | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 outputs.tf diff --git a/outputs.tf b/outputs.tf deleted file mode 100644 index 7176a74..0000000 --- a/outputs.tf +++ /dev/null @@ -1,6 +0,0 @@ -output "jenkins_master_alb_dns_name" { - description = "Jenkins Master Application Load Balancer DNS Name" - value = module.ecs-fargate-service.aws_lb_lb_dns_name -} -/// comments added/// -//chnaged// \ No newline at end of file From ee4439c81ce9ca3b6d63f05fae257030f65f42dd Mon Sep 17 00:00:00 2001 From: Ragesh Date: Tue, 13 Apr 2021 15:45:35 -0400 Subject: [PATCH 12/68] updated-variable --- ec2.tf | 3 +++ variables.tf | 5 ++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/ec2.tf b/ec2.tf index b3f3c19..dcf75ac 100644 --- a/ec2.tf +++ b/ec2.tf @@ -2,4 +2,7 @@ resource "aws_instance" "Ragesh-new" { ami = "ami-0bcf5425cdc1d8a85" instance_type = "t3.micro" key_name = "terraform-Jenkins" +tags = { + Name = var.instance_name + } } \ No newline at end of file diff --git a/variables.tf b/variables.tf index 2d6e0ef..cc11be1 100644 --- a/variables.tf +++ b/variables.tf @@ -1,8 +1,10 @@ #------------------------------------------------------------------------------ # Misc #------------------------------------------------------------------------------ -variable "name_prefix" { +variable "instance_name" { description = "Name prefix for resources on AWS" + type = string + default = "test-instance" } #------------------------------------------------------------------------------ @@ -10,4 +12,5 @@ variable "name_prefix" { #------------------------------------------------------------------------------ variable "region" { description = "AWS Region the infrastructure is hosted in" + default = "us-east-1" } From 8207d7426105ffb48bf29ff3de6ebc61647bfc7e Mon Sep 17 00:00:00 2001 From: Ragesh Date: Tue, 13 Apr 2021 15:58:15 -0400 Subject: [PATCH 13/68] ec2-location-updated --- ec2.tf | 1 + 1 file changed, 1 insertion(+) diff --git a/ec2.tf b/ec2.tf index dcf75ac..c1eda59 100644 --- a/ec2.tf +++ b/ec2.tf @@ -2,6 +2,7 @@ resource "aws_instance" "Ragesh-new" { ami = "ami-0bcf5425cdc1d8a85" instance_type = "t3.micro" key_name = "terraform-Jenkins" +region = var.region tags = { Name = var.instance_name } From e11997f5fbd09961c09a45007a5b5f26400d3199 Mon Sep 17 00:00:00 2001 From: Ragesh Date: Tue, 13 Apr 2021 16:10:07 -0400 Subject: [PATCH 14/68] updated version --- ec2.tf | 1 - 1 file changed, 1 deletion(-) diff --git a/ec2.tf b/ec2.tf index c1eda59..dcf75ac 100644 --- a/ec2.tf +++ b/ec2.tf @@ -2,7 +2,6 @@ resource "aws_instance" "Ragesh-new" { ami = "ami-0bcf5425cdc1d8a85" instance_type = "t3.micro" key_name = "terraform-Jenkins" -region = var.region tags = { Name = var.instance_name } From 771b3f20725e8411b8993d6126e5fb338c940dbb Mon Sep 17 00:00:00 2001 From: Ragesh Date: Wed, 14 Apr 2021 12:15:26 -0400 Subject: [PATCH 15/68] deleted vpc --- vpc.tf | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 vpc.tf diff --git a/vpc.tf b/vpc.tf deleted file mode 100644 index 175c603..0000000 --- a/vpc.tf +++ /dev/null @@ -1,9 +0,0 @@ -resource "aws_vpc" "main" { - cidr_block = "10.0.0.0/16" - instance_tenancy = "default" - - tags = { - Name = "demo-vpc" - Purpose = "Jenkins Demo" - } -} From b3cce8b534ee0a59a4dbd6df5d66d1eab5c653fe Mon Sep 17 00:00:00 2001 From: Ragesh Date: Wed, 14 Apr 2021 15:04:56 -0400 Subject: [PATCH 16/68] ec2-with-variable --- Jenkins-file | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++ ec2.tf | 5 ++-- 2 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 Jenkins-file diff --git a/Jenkins-file b/Jenkins-file new file mode 100644 index 0000000..0bc1173 --- /dev/null +++ b/Jenkins-file @@ -0,0 +1,67 @@ +pipeline { + agent any + tools { + terraform 'terraform-Rag' + } + parameters { + string(name: 'environment', defaultValue: 'default', description: 'Workspace/environment file to use for deployment') + string(name: 'version', defaultValue: '', description: 'Version variable to pass to Terraform') + choice choices: ['AWS', 'AZURE', 'OracleCloud'], description: '''Select the cloud platform + ''', name: 'Cloud' + choice choices: ['t2.micro', 't2.small', 't2.medium'], description: '''Select the appropriate instance type. + For more information + https://aws.amazon.com/ec2/instance-types/ + ''', name: 'Configuration' + string defaultValue: '', description: 'Input server Name', name: 'ServerName', trim: false + + booleanParam(name: 'autoApprove', defaultValue: false, description: 'Automatically run apply after generating plan?') + } + + environment { + AWS_ACCESS_KEY_ID = credentials('AWS-ACCESS-KEY-ID') + AWS_SECRET_ACCESS_KEY = credentials('AWS-SECRET-KEY') + TF_IN_AUTOMATION = '1' + } + + stages { + stage('Plan') { + steps { + script { + currentBuild.displayName = params.version + } + sh 'terraform init -input=false' + sh 'terraform workspace select ${environment}' + sh "terraform plan -input=false -out tfplan -var 'instance_type=${params.Configuration}' -var 'instance_name=${params.ServerName}' -var 'cloudprovider=${params.Cloud}' -var 'version=${params.version}' --var-file=environments/${params.environment}.tfvars" + sh 'terraform show -no-color tfplan > tfplan.txt' + } + } + + stage('Approval') { + when { + not { + equals expected: true, actual: params.autoApprove + } + } + + steps { + script { + def plan = readFile 'tfplan.txt' + input message: "Do you want to apply the plan?", + parameters: [text(name: 'Plan', description: 'Please review the plan', defaultValue: plan)] + } + } + } + + stage('Apply') { + steps { + sh "terraform apply -input=false tfplan" + } + } + } + + post { + always { + archiveArtifacts artifacts: 'tfplan.txt' + } + } +} \ No newline at end of file diff --git a/ec2.tf b/ec2.tf index dcf75ac..b5973c8 100644 --- a/ec2.tf +++ b/ec2.tf @@ -1,6 +1,7 @@ -resource "aws_instance" "Ragesh-new" { +resource "aws_instance" "Ragesh" { +provider = var.cloudprovider ami = "ami-0bcf5425cdc1d8a85" -instance_type = "t3.micro" +instance_type = var.instance_type key_name = "terraform-Jenkins" tags = { Name = var.instance_name From 3105fbf93558e21d54fe392d16350ee346b74452 Mon Sep 17 00:00:00 2001 From: Ragesh Date: Wed, 14 Apr 2021 15:10:02 -0400 Subject: [PATCH 17/68] provider-update --- Jenkins-file | 5 +++++ provider.tf | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Jenkins-file b/Jenkins-file index 0bc1173..6b36ca0 100644 --- a/Jenkins-file +++ b/Jenkins-file @@ -24,6 +24,11 @@ pipeline { } stages { + stage('Git Checkout'){ + steps{ + git credentialsId: 'GITHUB-RAGEH', url: 'https://github.com/Rageshmookoth/iac-demo' + } + } stage('Plan') { steps { script { diff --git a/provider.tf b/provider.tf index 3aafc50..32e52c1 100644 --- a/provider.tf +++ b/provider.tf @@ -1,3 +1,3 @@ provider "aws" { - region = "ap-south-1" +region = "ap-south-1" } From f067e4a0e5fb098f80f55c831fe654d5341f7193 Mon Sep 17 00:00:00 2001 From: Ragesh Date: Wed, 14 Apr 2021 15:13:46 -0400 Subject: [PATCH 18/68] provider-removed --- ec2.tf | 1 - 1 file changed, 1 deletion(-) diff --git a/ec2.tf b/ec2.tf index b5973c8..bd6af4d 100644 --- a/ec2.tf +++ b/ec2.tf @@ -1,5 +1,4 @@ resource "aws_instance" "Ragesh" { -provider = var.cloudprovider ami = "ami-0bcf5425cdc1d8a85" instance_type = var.instance_type key_name = "terraform-Jenkins" From 613e5f4b045b23bb9e31f98a8d230e8e9fba4aee Mon Sep 17 00:00:00 2001 From: Ragesh Date: Wed, 14 Apr 2021 15:25:41 -0400 Subject: [PATCH 19/68] variable defined --- variables.tf | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/variables.tf b/variables.tf index cc11be1..8da8850 100644 --- a/variables.tf +++ b/variables.tf @@ -2,15 +2,23 @@ # Misc #------------------------------------------------------------------------------ variable "instance_name" { - description = "Name prefix for resources on AWS" - type = string - default = "test-instance" +description = "Name prefix for resources on AWS" +type = string +default = "test-instance" } #------------------------------------------------------------------------------ # AWS CREDENTIALS AND REGION #------------------------------------------------------------------------------ +variable "instance_type" { +description = "AWS EC2 instance type" +default = "t2.micro" +} variable "region" { - description = "AWS Region the infrastructure is hosted in" - default = "us-east-1" +description = "AWS Region the infrastructure is hosted in" +default = "us-east-1" +} +variable "cloudprovider" { +description = "Select the Cloud Provider" +default = "AWS" } From 1d77d7f13bd2676134499323c9ccbc898bc24fac Mon Sep 17 00:00:00 2001 From: Ragesh Date: Wed, 14 Apr 2021 16:22:23 -0400 Subject: [PATCH 20/68] output-added --- output.tf | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 output.tf diff --git a/output.tf b/output.tf new file mode 100644 index 0000000..7bcbd8c --- /dev/null +++ b/output.tf @@ -0,0 +1,3 @@ +output "Server_ip"" { + value = "${public_ip}" +} \ No newline at end of file From 0d34678875fddfaea87b62118de8179d68c4e8f7 Mon Sep 17 00:00:00 2001 From: Ragesh Date: Wed, 14 Apr 2021 16:32:11 -0400 Subject: [PATCH 21/68] output-file-corected --- Jenkins-file | 4 +++- ec2.tf | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Jenkins-file b/Jenkins-file index 6b36ca0..86fbfe2 100644 --- a/Jenkins-file +++ b/Jenkins-file @@ -8,6 +8,8 @@ pipeline { string(name: 'version', defaultValue: '', description: 'Version variable to pass to Terraform') choice choices: ['AWS', 'AZURE', 'OracleCloud'], description: '''Select the cloud platform ''', name: 'Cloud' + choice choices: ['us-west-1', 'ap-south-1', 'eu-central-1'], description: '''Select the region + ''', name: 'Region' choice choices: ['t2.micro', 't2.small', 't2.medium'], description: '''Select the appropriate instance type. For more information https://aws.amazon.com/ec2/instance-types/ @@ -36,7 +38,7 @@ pipeline { } sh 'terraform init -input=false' sh 'terraform workspace select ${environment}' - sh "terraform plan -input=false -out tfplan -var 'instance_type=${params.Configuration}' -var 'instance_name=${params.ServerName}' -var 'cloudprovider=${params.Cloud}' -var 'version=${params.version}' --var-file=environments/${params.environment}.tfvars" + sh "terraform plan -input=false -out tfplan -var 'region=${params.Region}' -var 'instance_type=${params.Configuration}' -var 'instance_name=${params.ServerName}' -var 'cloudprovider=${params.Cloud}'" sh 'terraform show -no-color tfplan > tfplan.txt' } } diff --git a/ec2.tf b/ec2.tf index bd6af4d..7491ece 100644 --- a/ec2.tf +++ b/ec2.tf @@ -2,6 +2,7 @@ resource "aws_instance" "Ragesh" { ami = "ami-0bcf5425cdc1d8a85" instance_type = var.instance_type key_name = "terraform-Jenkins" +region = var.region tags = { Name = var.instance_name } From c761f5b96cbdbb8cd1ce9e4df103256dc5533291 Mon Sep 17 00:00:00 2001 From: Ragesh Date: Wed, 14 Apr 2021 16:34:11 -0400 Subject: [PATCH 22/68] updated --- output.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/output.tf b/output.tf index 7bcbd8c..5b46402 100644 --- a/output.tf +++ b/output.tf @@ -1,3 +1,3 @@ -output "Server_ip"" { +output "Server_ip" { value = "${public_ip}" } \ No newline at end of file From feca3e6a9028b50e1ad1f0c3cda8fcfdf419ef4e Mon Sep 17 00:00:00 2001 From: Ragesh Date: Wed, 14 Apr 2021 16:42:55 -0400 Subject: [PATCH 23/68] updated-code --- output.tf | 2 +- provider.tf | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/output.tf b/output.tf index 5b46402..5be9fbb 100644 --- a/output.tf +++ b/output.tf @@ -1,3 +1,3 @@ output "Server_ip" { - value = "${public_ip}" + value = "${aws_instance.this.private_ip}" } \ No newline at end of file diff --git a/provider.tf b/provider.tf index 32e52c1..165800f 100644 --- a/provider.tf +++ b/provider.tf @@ -1,3 +1,3 @@ provider "aws" { -region = "ap-south-1" +region = var.region } From f2c66a1873b64a8a43852c6d179f0a6c57750600 Mon Sep 17 00:00:00 2001 From: Ragesh Date: Wed, 14 Apr 2021 16:50:02 -0400 Subject: [PATCH 24/68] updated --- ec2.tf | 1 - 1 file changed, 1 deletion(-) diff --git a/ec2.tf b/ec2.tf index 7491ece..bd6af4d 100644 --- a/ec2.tf +++ b/ec2.tf @@ -2,7 +2,6 @@ resource "aws_instance" "Ragesh" { ami = "ami-0bcf5425cdc1d8a85" instance_type = var.instance_type key_name = "terraform-Jenkins" -region = var.region tags = { Name = var.instance_name } From ad961fb85f95c8256a2fa6753ba389f86219768e Mon Sep 17 00:00:00 2001 From: Ragesh Date: Thu, 15 Apr 2021 03:32:15 -0400 Subject: [PATCH 25/68] updated-correct-value --- output.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/output.tf b/output.tf index 5be9fbb..d62051f 100644 --- a/output.tf +++ b/output.tf @@ -1,3 +1,3 @@ output "Server_ip" { - value = "${aws_instance.this.private_ip}" + value = "${aws_instance.Ragesh.private_ip}" } \ No newline at end of file From 19d26a8e230a026df4b3f32de50eb868866ccb72 Mon Sep 17 00:00:00 2001 From: Ragesh Date: Thu, 15 Apr 2021 11:03:48 -0400 Subject: [PATCH 26/68] ami-changed --- ec2.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ec2.tf b/ec2.tf index bd6af4d..55286f4 100644 --- a/ec2.tf +++ b/ec2.tf @@ -1,5 +1,5 @@ resource "aws_instance" "Ragesh" { -ami = "ami-0bcf5425cdc1d8a85" +ami = "ami-0742b4e673072066f" instance_type = var.instance_type key_name = "terraform-Jenkins" tags = { From 0358e8503f85d31da12b363a6da68cadf9746dc4 Mon Sep 17 00:00:00 2001 From: Ragesh Date: Thu, 15 Apr 2021 11:06:56 -0400 Subject: [PATCH 27/68] us-east-ami-id update --- ec2.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ec2.tf b/ec2.tf index 55286f4..35cd9fe 100644 --- a/ec2.tf +++ b/ec2.tf @@ -1,5 +1,5 @@ resource "aws_instance" "Ragesh" { -ami = "ami-0742b4e673072066f" +ami = "ami-0577b787189839998" instance_type = var.instance_type key_name = "terraform-Jenkins" tags = { From 9bcd10719c8bc802639bdc9d354a354c4b8b05f1 Mon Sep 17 00:00:00 2001 From: Ragesh Date: Fri, 16 Apr 2021 05:15:55 -0400 Subject: [PATCH 28/68] AMI ID update as STD image --- ec2.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ec2.tf b/ec2.tf index 35cd9fe..0bec860 100644 --- a/ec2.tf +++ b/ec2.tf @@ -1,5 +1,5 @@ resource "aws_instance" "Ragesh" { -ami = "ami-0577b787189839998" +ami = "ami-09d9c5cdcfb8fc655" instance_type = var.instance_type key_name = "terraform-Jenkins" tags = { From 11b5b30d421d09137b5afb9eeb10fcd29713d3b7 Mon Sep 17 00:00:00 2001 From: Ragesh Date: Sat, 1 May 2021 10:16:27 -0400 Subject: [PATCH 29/68] added AMI ID --- ec2.tf | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ec2.tf b/ec2.tf index 0bec860..eb96fba 100644 --- a/ec2.tf +++ b/ec2.tf @@ -1,8 +1,10 @@ resource "aws_instance" "Ragesh" { -ami = "ami-09d9c5cdcfb8fc655" + ami = "ami-045e6fa7127ab1ac4" instance_type = var.instance_type key_name = "terraform-Jenkins" +size == var.size tags = { Name = var.instance_name + Owner = var.Owner } } \ No newline at end of file From a5c037e5cc9839e6a3c52b8721394e695dbe766c Mon Sep 17 00:00:00 2001 From: Ragesh Date: Sat, 1 May 2021 10:32:22 -0400 Subject: [PATCH 30/68] updated size variable --- ec2.tf | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ec2.tf b/ec2.tf index eb96fba..8f45577 100644 --- a/ec2.tf +++ b/ec2.tf @@ -2,7 +2,8 @@ resource "aws_instance" "Ragesh" { ami = "ami-045e6fa7127ab1ac4" instance_type = var.instance_type key_name = "terraform-Jenkins" -size == var.size +volume_size = var.size + tags = { Name = var.instance_name Owner = var.Owner From a5893bcf687942f7bc8a7ed39fa610fc4882377d Mon Sep 17 00:00:00 2001 From: Ragesh Date: Sat, 1 May 2021 10:41:09 -0400 Subject: [PATCH 31/68] delete output file --- output.tf | 3 --- variables.tf | 8 ++++++++ 2 files changed, 8 insertions(+), 3 deletions(-) delete mode 100644 output.tf diff --git a/output.tf b/output.tf deleted file mode 100644 index d62051f..0000000 --- a/output.tf +++ /dev/null @@ -1,3 +0,0 @@ -output "Server_ip" { - value = "${aws_instance.Ragesh.private_ip}" -} \ No newline at end of file diff --git a/variables.tf b/variables.tf index 8da8850..2e89609 100644 --- a/variables.tf +++ b/variables.tf @@ -22,3 +22,11 @@ variable "cloudprovider" { description = "Select the Cloud Provider" default = "AWS" } +variable "size" { +description = "Provide size" +default = "100" +} +variable "Owner" { +description = "Owner name" +default = "Ragesh" +} \ No newline at end of file From 7d2add36a8edee5c52ec1871938af698b8e80b50 Mon Sep 17 00:00:00 2001 From: Ragesh Date: Mon, 3 May 2021 15:56:09 -0400 Subject: [PATCH 32/68] volume-size-removed --- ec2.tf | 2 -- 1 file changed, 2 deletions(-) diff --git a/ec2.tf b/ec2.tf index 8f45577..651e93a 100644 --- a/ec2.tf +++ b/ec2.tf @@ -2,8 +2,6 @@ resource "aws_instance" "Ragesh" { ami = "ami-045e6fa7127ab1ac4" instance_type = var.instance_type key_name = "terraform-Jenkins" -volume_size = var.size - tags = { Name = var.instance_name Owner = var.Owner From 2c46c09aa62d8f5f3bb00dd4ebdd806822a0240c Mon Sep 17 00:00:00 2001 From: Ragesh Date: Thu, 6 May 2021 10:20:28 -0400 Subject: [PATCH 33/68] output-committed --- output.tf | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 output.tf diff --git a/output.tf b/output.tf new file mode 100644 index 0000000..8c23c80 --- /dev/null +++ b/output.tf @@ -0,0 +1,3 @@ +output "ip" { + value = "${aws_instance.Ragesh.*.public_ip}" +} \ No newline at end of file From ddf793dac7b7ed2bacf4634da37f55c9da99ac21 Mon Sep 17 00:00:00 2001 From: Ragesh Date: Thu, 6 May 2021 15:25:46 -0400 Subject: [PATCH 34/68] ansibleupate --- Jenkins-file | 5 +++++ ec2.tf | 3 +++ playbook.yml | 13 +++++++++++++ 3 files changed, 21 insertions(+) create mode 100644 playbook.yml diff --git a/Jenkins-file b/Jenkins-file index 86fbfe2..70eeb50 100644 --- a/Jenkins-file +++ b/Jenkins-file @@ -64,6 +64,11 @@ pipeline { sh "terraform apply -input=false tfplan" } } + stage('output') { + steps { + sh "terraform output ip" + } + } } post { diff --git a/ec2.tf b/ec2.tf index 651e93a..1ecf340 100644 --- a/ec2.tf +++ b/ec2.tf @@ -6,4 +6,7 @@ tags = { Name = var.instance_name Owner = var.Owner } +} +provisioner "local-exec" { + command = "sleep 120; ansible-playbook -i '${aws_instance.Ragesh.*.public_ip}' playbook.yml" } \ No newline at end of file diff --git a/playbook.yml b/playbook.yml new file mode 100644 index 0000000..d6c8fd9 --- /dev/null +++ b/playbook.yml @@ -0,0 +1,13 @@ +--- +- hosts: all +become: yes +become_user: root +become_method: sudo +tasks: + - name: Install nginx + apt: + name: nginx + state: latest + - name: Restart Nginx + service: name=nginx state=restarted + become: yes \ No newline at end of file From ea96c3b641a049955aee32e96227974d041de403 Mon Sep 17 00:00:00 2001 From: Ragesh Date: Sat, 8 May 2021 10:38:26 -0400 Subject: [PATCH 35/68] removed provisioner --- ec2.tf | 3 --- 1 file changed, 3 deletions(-) diff --git a/ec2.tf b/ec2.tf index 1ecf340..05825cb 100644 --- a/ec2.tf +++ b/ec2.tf @@ -7,6 +7,3 @@ tags = { Owner = var.Owner } } -provisioner "local-exec" { - command = "sleep 120; ansible-playbook -i '${aws_instance.Ragesh.*.public_ip}' playbook.yml" -} \ No newline at end of file From 6822310bb1d5ce73f573615cf1f79de52f4bbfd2 Mon Sep 17 00:00:00 2001 From: Ragesh Date: Mon, 10 May 2021 00:04:53 -0400 Subject: [PATCH 36/68] key-pair-name-updated --- ec2.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ec2.tf b/ec2.tf index 05825cb..59f85cc 100644 --- a/ec2.tf +++ b/ec2.tf @@ -1,7 +1,7 @@ resource "aws_instance" "Ragesh" { ami = "ami-045e6fa7127ab1ac4" instance_type = var.instance_type -key_name = "terraform-Jenkins" +key_name = "POC-STD-KEY-PAIR" tags = { Name = var.instance_name Owner = var.Owner From 065b6d1521e269be4d5aef840c19ac319a505fac Mon Sep 17 00:00:00 2001 From: Ragesh Date: Mon, 10 May 2021 04:54:22 -0400 Subject: [PATCH 37/68] ansible-provisioner addeded --- ec2.tf | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ec2.tf b/ec2.tf index 59f85cc..ef3ced1 100644 --- a/ec2.tf +++ b/ec2.tf @@ -7,3 +7,8 @@ tags = { Owner = var.Owner } } + +provisioner "local-exec" { + command = "ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -u ec2-user -i '${aws_instance.Ragesh.*.public_ip}' --private-key POC-STD-KEY-PAIR.pem playbook.yml" + } +} From 3af044e1ce844320147a022065511f01fba09a90 Mon Sep 17 00:00:00 2001 From: Ragesh Date: Mon, 10 May 2021 07:00:07 -0400 Subject: [PATCH 38/68] ansibe provisioner update --- ec2.tf | 1 - 1 file changed, 1 deletion(-) diff --git a/ec2.tf b/ec2.tf index ef3ced1..4e71cd2 100644 --- a/ec2.tf +++ b/ec2.tf @@ -11,4 +11,3 @@ tags = { provisioner "local-exec" { command = "ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -u ec2-user -i '${aws_instance.Ragesh.*.public_ip}' --private-key POC-STD-KEY-PAIR.pem playbook.yml" } -} From 69e904bb15509efcedbd0d0572c1b38d61edf6bf Mon Sep 17 00:00:00 2001 From: Ragesh Date: Mon, 10 May 2021 07:44:46 -0400 Subject: [PATCH 39/68] provisioner updated --- ec2.tf | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/ec2.tf b/ec2.tf index 4e71cd2..ecaf8c5 100644 --- a/ec2.tf +++ b/ec2.tf @@ -6,8 +6,9 @@ tags = { Name = var.instance_name Owner = var.Owner } -} - -provisioner "local-exec" { + provisioner "local-exec" { command = "ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -u ec2-user -i '${aws_instance.Ragesh.*.public_ip}' --private-key POC-STD-KEY-PAIR.pem playbook.yml" } +} + + From 961ed7b00982dbef58bfc9b33948bacea1250e5a Mon Sep 17 00:00:00 2001 From: Ragesh Date: Mon, 10 May 2021 08:07:36 -0400 Subject: [PATCH 40/68] apache installation steps updated --- playbook.yml | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/playbook.yml b/playbook.yml index d6c8fd9..ffe14d2 100644 --- a/playbook.yml +++ b/playbook.yml @@ -1,13 +1,13 @@ --- - hosts: all -become: yes -become_user: root -become_method: sudo -tasks: - - name: Install nginx - apt: - name: nginx + become: yes + become_user: root + become_method: sudo + tasks: + - name: Install httpd + yum: + name: httpd state: latest - - name: Restart Nginx - service: name=nginx state=restarted - become: yes \ No newline at end of file + - name: Restart httpd + service: name=httpd state=restarted + become: yes From 93d00af07cd08e9fbf3b9a0a370ca7041c47d215 Mon Sep 17 00:00:00 2001 From: Ragesh Date: Mon, 10 May 2021 08:12:20 -0400 Subject: [PATCH 41/68] ansible-update --- ec2.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ec2.tf b/ec2.tf index ecaf8c5..b388134 100644 --- a/ec2.tf +++ b/ec2.tf @@ -7,7 +7,7 @@ tags = { Owner = var.Owner } provisioner "local-exec" { - command = "ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -u ec2-user -i '${aws_instance.Ragesh.*.public_ip}' --private-key POC-STD-KEY-PAIR.pem playbook.yml" + command = "ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -u ec2-user -i '${aws_instance.Ragesh.*.public_ip}', --private-key POC-STD-KEY-PAIR.pem playbook.yml" } } From 8ae1bee3effde769f87b17b2661d066544c078df Mon Sep 17 00:00:00 2001 From: Ragesh Date: Mon, 10 May 2021 08:31:14 -0400 Subject: [PATCH 42/68] debug logging enabled --- ec2.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ec2.tf b/ec2.tf index b388134..01c7b14 100644 --- a/ec2.tf +++ b/ec2.tf @@ -7,7 +7,7 @@ tags = { Owner = var.Owner } provisioner "local-exec" { - command = "ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -u ec2-user -i '${aws_instance.Ragesh.*.public_ip}', --private-key POC-STD-KEY-PAIR.pem playbook.yml" + command = "ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -u ec2-user -i '${aws_instance.Ragesh.*.public_ip}', --private-key POC-STD-KEY-PAIR.pem playbook.yml >> /tmp/ansible-output" } } From 7d3581b2febab7003473e1c5bd5be953b49b3dc8 Mon Sep 17 00:00:00 2001 From: Ragesh Date: Mon, 10 May 2021 08:36:16 -0400 Subject: [PATCH 43/68] updated ansible command --- ec2.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ec2.tf b/ec2.tf index 01c7b14..a5842f3 100644 --- a/ec2.tf +++ b/ec2.tf @@ -7,7 +7,7 @@ tags = { Owner = var.Owner } provisioner "local-exec" { - command = "ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -u ec2-user -i '${aws_instance.Ragesh.*.public_ip}', --private-key POC-STD-KEY-PAIR.pem playbook.yml >> /tmp/ansible-output" + command = "ansible-playbook -u ec2-user -i '${aws_instance.Ragesh.*.public_ip}', --private-key POC-STD-KEY-PAIR.pem playbook.yml >>/tmp/ansible-output" } } From c7348b55fda6b4c28297ea05803ae3114a0f21d3 Mon Sep 17 00:00:00 2001 From: Ragesh Date: Mon, 10 May 2021 11:37:31 -0400 Subject: [PATCH 44/68] ansible-updated --- ec2.tf | 3 --- 1 file changed, 3 deletions(-) diff --git a/ec2.tf b/ec2.tf index a5842f3..64c4a23 100644 --- a/ec2.tf +++ b/ec2.tf @@ -6,9 +6,6 @@ tags = { Name = var.instance_name Owner = var.Owner } - provisioner "local-exec" { - command = "ansible-playbook -u ec2-user -i '${aws_instance.Ragesh.*.public_ip}', --private-key POC-STD-KEY-PAIR.pem playbook.yml >>/tmp/ansible-output" } -} From 517fea3a35f5aa0cbfcea534e2e933773ccf5885 Mon Sep 17 00:00:00 2001 From: Ragesh Date: Mon, 10 May 2021 16:47:21 -0400 Subject: [PATCH 45/68] inventory-file-created --- ec2.tf | 13 +++++++++++++ inventory.template | 2 ++ 2 files changed, 15 insertions(+) create mode 100644 inventory.template diff --git a/ec2.tf b/ec2.tf index 64c4a23..2809391 100644 --- a/ec2.tf +++ b/ec2.tf @@ -6,6 +6,19 @@ tags = { Name = var.instance_name Owner = var.Owner } + provisioner "local-exec" { + command = "ansible-playbook -u ec2-user -i inventory --private-key POC-STD-KEY-PAIR.pem playbook.yml" } + } + + +resource "local_file" "ansible_inventory_hosts" { + content = templatefile("inventory.template", + { + web_public_ip = ${aws_instance.Ragesh.*.public_ip}, + } + ) + filename = "inventory" +} diff --git a/inventory.template b/inventory.template new file mode 100644 index 0000000..a930526 --- /dev/null +++ b/inventory.template @@ -0,0 +1,2 @@ +[webserver] +${web_public_ip} \ No newline at end of file From 10e18805a52e081518c3340bc982a2b86b8222da Mon Sep 17 00:00:00 2001 From: Ragesh Date: Mon, 10 May 2021 16:50:42 -0400 Subject: [PATCH 46/68] reference --- ec2.tf | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ec2.tf b/ec2.tf index 2809391..8d5b82b 100644 --- a/ec2.tf +++ b/ec2.tf @@ -22,3 +22,5 @@ resource "local_file" "ansible_inventory_hosts" { filename = "inventory" } +### https://a4ank.medium.com/aws-terraform-ansible-end-to-end-automation-9072f2ecf624 +####https://medium.com/on-the-cloud/one-click-environment-creation-with-terraform-ansible-in-under-10-6e8d9284f60 \ No newline at end of file From 1a2d65c96a7abfb6b18ef9b41750601511806a7d Mon Sep 17 00:00:00 2001 From: Ragesh Date: Tue, 11 May 2021 03:37:47 -0400 Subject: [PATCH 47/68] resources ec2 --- ec2.tf | 26 -------------------------- resource.tf | 28 ++++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 26 deletions(-) delete mode 100644 ec2.tf create mode 100644 resource.tf diff --git a/ec2.tf b/ec2.tf deleted file mode 100644 index 8d5b82b..0000000 --- a/ec2.tf +++ /dev/null @@ -1,26 +0,0 @@ -resource "aws_instance" "Ragesh" { - ami = "ami-045e6fa7127ab1ac4" -instance_type = var.instance_type -key_name = "POC-STD-KEY-PAIR" -tags = { - Name = var.instance_name - Owner = var.Owner - } - provisioner "local-exec" { - command = "ansible-playbook -u ec2-user -i inventory --private-key POC-STD-KEY-PAIR.pem playbook.yml" - } - } - - -resource "local_file" "ansible_inventory_hosts" { - content = templatefile("inventory.template", - { - web_public_ip = ${aws_instance.Ragesh.*.public_ip}, - - } - ) - filename = "inventory" -} - -### https://a4ank.medium.com/aws-terraform-ansible-end-to-end-automation-9072f2ecf624 -####https://medium.com/on-the-cloud/one-click-environment-creation-with-terraform-ansible-in-under-10-6e8d9284f60 \ No newline at end of file diff --git a/resource.tf b/resource.tf new file mode 100644 index 0000000..9fd0ba9 --- /dev/null +++ b/resource.tf @@ -0,0 +1,28 @@ +resource "aws_instance" "Ragesh" { +ami = "ami-045e6fa7127ab1ac4" +instance_type = var.instance_type +key_name = "POC-STD-KEY-PAIR" +tags = { + Name = var.instance_name + Owner = var.Owner + } +} +resource "null_resource" "nulllocal1" { +provisioner "local-exec" { + command = "echo ${aws_instance.Ragesh.*.public_ip} > publicip.txt" + } +} +resource "null_resource" "nulllocal2" { +depends_on = [ + aws_instance.Ragesh, + null_resource.nulllocal1, + ] + # using ansible, declarative approach of configuration management + provisioner "local-exec" { + command ="ansible-playbook -i inventory playbook.yml --private-key=POC-STD-KEY-PAIR --user ec2-user" + } +} + + +### https://a4ank.medium.com/aws-terraform-ansible-end-to-end-automation-9072f2ecf624 +####https://medium.com/on-the-cloud/one-click-environment-creation-with-terraform-ansible-in-under-10-6e8d9284f60 \ No newline at end of file From 19443c17f80f445879e4275ecfc12dbbf66c9ea7 Mon Sep 17 00:00:00 2001 From: Ragesh Date: Tue, 11 May 2021 03:47:03 -0400 Subject: [PATCH 48/68] output updated --- output.tf | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/output.tf b/output.tf index 8c23c80..0370499 100644 --- a/output.tf +++ b/output.tf @@ -1,3 +1,11 @@ output "ip" { - value = "${aws_instance.Ragesh.*.public_ip}" + value = "${aws_instance.Ragesh.public_ip}" +} +resource "local_file" "ansible_inventory_hosts" { + content = templatefile("inventory.template", + { + web_public_ip = aws_instance.Ragesh.public_ip, + } + ) + filename = "inventory" } \ No newline at end of file From 6141336a0e3377c265335b0052b31a459a7894c2 Mon Sep 17 00:00:00 2001 From: Ragesh Date: Tue, 11 May 2021 03:51:10 -0400 Subject: [PATCH 49/68] upated outputfile --- output.tf | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/output.tf b/output.tf index 0370499..e5efcf7 100644 --- a/output.tf +++ b/output.tf @@ -1,10 +1,11 @@ -output "ip" { +output "AWS-instance-Public-IP" { value = "${aws_instance.Ragesh.public_ip}" } resource "local_file" "ansible_inventory_hosts" { content = templatefile("inventory.template", { web_public_ip = aws_instance.Ragesh.public_ip, + web_id = aws_instance.Ragesh.id, } ) filename = "inventory" From fd01d4729edb2523eea6a30a877c792901b0ea10 Mon Sep 17 00:00:00 2001 From: Ragesh Date: Tue, 11 May 2021 03:57:30 -0400 Subject: [PATCH 50/68] output version update --- output.tf | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/output.tf b/output.tf index e5efcf7..62af66f 100644 --- a/output.tf +++ b/output.tf @@ -4,8 +4,8 @@ output "AWS-instance-Public-IP" { resource "local_file" "ansible_inventory_hosts" { content = templatefile("inventory.template", { - web_public_ip = aws_instance.Ragesh.public_ip, - web_id = aws_instance.Ragesh.id, + web_public_ip = "${aws_instance.Ragesh.public_ip}", + web_id = "${aws_instance.Ragesh.id}", } ) filename = "inventory" From 1f8f10d882bbe2205d3da64fa830e47d70e70018 Mon Sep 17 00:00:00 2001 From: Ragesh Date: Tue, 11 May 2021 04:08:35 -0400 Subject: [PATCH 51/68] Updated output --- output.tf | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/output.tf b/output.tf index 62af66f..ca90cf1 100644 --- a/output.tf +++ b/output.tf @@ -1,11 +1,10 @@ output "AWS-instance-Public-IP" { - value = "${aws_instance.Ragesh.public_ip}" + value = aws_instance.Ragesh.public_ip } resource "local_file" "ansible_inventory_hosts" { content = templatefile("inventory.template", { - web_public_ip = "${aws_instance.Ragesh.public_ip}", - web_id = "${aws_instance.Ragesh.id}", + web_public_ip = aws_instance.Ragesh.public_ip, } ) filename = "inventory" From 0ed9412e35476fe21136d6e0d1fd58a20e3035b3 Mon Sep 17 00:00:00 2001 From: Ragesh Date: Fri, 14 May 2021 08:29:34 -0400 Subject: [PATCH 52/68] variable and output file updated --- output.tf | 10 +++++++++- variables.tf | 6 ++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/output.tf b/output.tf index ca90cf1..77c9504 100644 --- a/output.tf +++ b/output.tf @@ -1,5 +1,5 @@ output "AWS-instance-Public-IP" { - value = aws_instance.Ragesh.public_ip + value = "${aws_instance.Ragesh.public_ip}" } resource "local_file" "ansible_inventory_hosts" { content = templatefile("inventory.template", @@ -8,4 +8,12 @@ resource "local_file" "ansible_inventory_hosts" { } ) filename = "inventory" +} +resource "null_resource" "step3" { +depends_on = [ + null_resource.step1, + ] +provisioner "local-exec" { + command = "echo ${aws_instance.web.public_ip}" + } } \ No newline at end of file diff --git a/variables.tf b/variables.tf index 2e89609..4f04892 100644 --- a/variables.tf +++ b/variables.tf @@ -29,4 +29,10 @@ default = "100" variable "Owner" { description = "Owner name" default = "Ragesh" +} +variable "private_key" { + default = "POC-STD-KEY-PAIR" +} +variable "ansible_user" { + default = "ec2-user" } \ No newline at end of file From f173382a7c95d8edc8d35e3a8580901bd70faaff Mon Sep 17 00:00:00 2001 From: Ragesh Date: Fri, 14 May 2021 09:38:12 -0400 Subject: [PATCH 53/68] output --- output.tf | 8 -------- 1 file changed, 8 deletions(-) diff --git a/output.tf b/output.tf index 77c9504..f8858ad 100644 --- a/output.tf +++ b/output.tf @@ -9,11 +9,3 @@ resource "local_file" "ansible_inventory_hosts" { ) filename = "inventory" } -resource "null_resource" "step3" { -depends_on = [ - null_resource.step1, - ] -provisioner "local-exec" { - command = "echo ${aws_instance.web.public_ip}" - } -} \ No newline at end of file From c128f90e6df46f950bb5b23f2528e334abe5acd1 Mon Sep 17 00:00:00 2001 From: Ragesh Date: Fri, 14 May 2021 10:09:39 -0400 Subject: [PATCH 54/68] variable updated --- resource.tf | 13 ++++++++----- variables.tf | 2 +- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/resource.tf b/resource.tf index 9fd0ba9..ab045e4 100644 --- a/resource.tf +++ b/resource.tf @@ -7,22 +7,25 @@ tags = { Owner = var.Owner } } -resource "null_resource" "nulllocal1" { +resource "null_resource" "step1" { provisioner "local-exec" { - command = "echo ${aws_instance.Ragesh.*.public_ip} > publicip.txt" + command = "echo ${aws_instance.Ragesh.public_ip} > publicip.txt" } } -resource "null_resource" "nulllocal2" { +resource "null_resource" "step2" { depends_on = [ aws_instance.Ragesh, - null_resource.nulllocal1, + null_resource.step1, ] # using ansible, declarative approach of configuration management provisioner "local-exec" { - command ="ansible-playbook -i inventory playbook.yml --private-key=POC-STD-KEY-PAIR --user ec2-user" + command ="ansible-playbook -i inventory playbook.yml --private-key=${var.private_key} --user ${var.ansible_user}" } } + + + ### https://a4ank.medium.com/aws-terraform-ansible-end-to-end-automation-9072f2ecf624 ####https://medium.com/on-the-cloud/one-click-environment-creation-with-terraform-ansible-in-under-10-6e8d9284f60 \ No newline at end of file diff --git a/variables.tf b/variables.tf index 4f04892..9deff8e 100644 --- a/variables.tf +++ b/variables.tf @@ -31,7 +31,7 @@ description = "Owner name" default = "Ragesh" } variable "private_key" { - default = "POC-STD-KEY-PAIR" + default = "POC-STD-KEY-PAIR.pem" } variable "ansible_user" { default = "ec2-user" From 00698a563e568b64de8578642e29d721296417f6 Mon Sep 17 00:00:00 2001 From: Ragesh Date: Fri, 14 May 2021 10:13:06 -0400 Subject: [PATCH 55/68] public ip redirection updated --- resource.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resource.tf b/resource.tf index ab045e4..efb223d 100644 --- a/resource.tf +++ b/resource.tf @@ -9,7 +9,7 @@ tags = { } resource "null_resource" "step1" { provisioner "local-exec" { - command = "echo ${aws_instance.Ragesh.public_ip} > publicip.txt" + command = "echo ${aws_instance.Ragesh.public_ip}" } } resource "null_resource" "step2" { From c19d18bef8a916119e9c95e75e7bfce4b4bec07d Mon Sep 17 00:00:00 2001 From: Ragesh Date: Fri, 14 May 2021 10:24:14 -0400 Subject: [PATCH 56/68] sleep implemented --- resource.tf | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/resource.tf b/resource.tf index efb223d..857f0aa 100644 --- a/resource.tf +++ b/resource.tf @@ -12,10 +12,16 @@ provisioner "local-exec" { command = "echo ${aws_instance.Ragesh.public_ip}" } } +resource "null_resource" "wait" { + provisioner "local-exec" { + command = "sleep 300 " + } +} resource "null_resource" "step2" { depends_on = [ aws_instance.Ragesh, null_resource.step1, + null_resource.wait, ] # using ansible, declarative approach of configuration management provisioner "local-exec" { From 94c3486a3dedbedf8162e6c0ae263fb5bd498ddf Mon Sep 17 00:00:00 2001 From: Ragesh Date: Fri, 14 May 2021 10:30:26 -0400 Subject: [PATCH 57/68] output variable name updated --- output.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/output.tf b/output.tf index f8858ad..c328e8c 100644 --- a/output.tf +++ b/output.tf @@ -1,4 +1,4 @@ -output "AWS-instance-Public-IP" { +output "ip" { value = "${aws_instance.Ragesh.public_ip}" } resource "local_file" "ansible_inventory_hosts" { From 771cd30c2596213d2c21de5e923eecd650c769d8 Mon Sep 17 00:00:00 2001 From: Ragesh Date: Fri, 14 May 2021 10:43:05 -0400 Subject: [PATCH 58/68] resource-updated --- resource.tf | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/resource.tf b/resource.tf index 857f0aa..68a68ea 100644 --- a/resource.tf +++ b/resource.tf @@ -7,22 +7,22 @@ tags = { Owner = var.Owner } } -resource "null_resource" "step1" { -provisioner "local-exec" { - command = "echo ${aws_instance.Ragesh.public_ip}" - } -} +#resource "null_resource" "step1" { +#provisioner "local-exec" { +# command = "echo ${aws_instance.Ragesh.public_ip}" +# } +#} resource "null_resource" "wait" { provisioner "local-exec" { - command = "sleep 300 " + command = "sleep 180" } } resource "null_resource" "step2" { -depends_on = [ - aws_instance.Ragesh, - null_resource.step1, - null_resource.wait, - ] +#depends_on = [ +# aws_instance.Ragesh, +# null_resource.step1, +# null_resource.wait, +# ] # using ansible, declarative approach of configuration management provisioner "local-exec" { command ="ansible-playbook -i inventory playbook.yml --private-key=${var.private_key} --user ${var.ansible_user}" From a776ca94b576d4d3f4ec9fdb613c7c860c5bc235 Mon Sep 17 00:00:00 2001 From: Ragesh Date: Fri, 14 May 2021 11:04:20 -0400 Subject: [PATCH 59/68] resources --- resource.tf | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/resource.tf b/resource.tf index 68a68ea..a2c82da 100644 --- a/resource.tf +++ b/resource.tf @@ -12,14 +12,14 @@ tags = { # command = "echo ${aws_instance.Ragesh.public_ip}" # } #} -resource "null_resource" "wait" { - provisioner "local-exec" { - command = "sleep 180" - } -} +#resource "null_resource" "wait" { +# provisioner "local-exec" { +# command = "sleep 180" +# } +#} resource "null_resource" "step2" { -#depends_on = [ -# aws_instance.Ragesh, +depends_on = [ + aws_instance.Ragesh, # null_resource.step1, # null_resource.wait, # ] From 6127f2a0ee3adf2f49bc50744b5ce6e79bbe9291 Mon Sep 17 00:00:00 2001 From: Ragesh Date: Fri, 14 May 2021 11:09:32 -0400 Subject: [PATCH 60/68] resource --- resource.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resource.tf b/resource.tf index a2c82da..e16124e 100644 --- a/resource.tf +++ b/resource.tf @@ -22,7 +22,7 @@ depends_on = [ aws_instance.Ragesh, # null_resource.step1, # null_resource.wait, -# ] + ] # using ansible, declarative approach of configuration management provisioner "local-exec" { command ="ansible-playbook -i inventory playbook.yml --private-key=${var.private_key} --user ${var.ansible_user}" From e57130ab0a45fec1889b382ef7f68d9cef497dd2 Mon Sep 17 00:00:00 2001 From: Ragesh Date: Fri, 14 May 2021 11:18:59 -0400 Subject: [PATCH 61/68] resource dependency added --- Jenkins-file | 2 +- resource.tf | 15 +++++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/Jenkins-file b/Jenkins-file index 70eeb50..73becb6 100644 --- a/Jenkins-file +++ b/Jenkins-file @@ -66,7 +66,7 @@ pipeline { } stage('output') { steps { - sh "terraform output ip" + sh "export ip = `cat /tmp/inventory`" } } } diff --git a/resource.tf b/resource.tf index e16124e..50d8693 100644 --- a/resource.tf +++ b/resource.tf @@ -12,17 +12,20 @@ tags = { # command = "echo ${aws_instance.Ragesh.public_ip}" # } #} -#resource "null_resource" "wait" { -# provisioner "local-exec" { -# command = "sleep 180" -# } -#} -resource "null_resource" "step2" { +resource "null_resource" "wait" { depends_on = [ aws_instance.Ragesh, # null_resource.step1, # null_resource.wait, ] + provisioner "local-exec" { + command = "sleep 180" + } +} +resource "null_resource" "Ansible-play" { +depends_on = [ + null_resource.wait, + ] # using ansible, declarative approach of configuration management provisioner "local-exec" { command ="ansible-playbook -i inventory playbook.yml --private-key=${var.private_key} --user ${var.ansible_user}" From f60da72db738e313020c7bd04678281eb9fe2aad Mon Sep 17 00:00:00 2001 From: Ragesh Date: Fri, 14 May 2021 11:40:13 -0400 Subject: [PATCH 62/68] inventoryfile updated --- Jenkins-file | 2 +- inventory.template | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/Jenkins-file b/Jenkins-file index 73becb6..5fe7b76 100644 --- a/Jenkins-file +++ b/Jenkins-file @@ -66,7 +66,7 @@ pipeline { } stage('output') { steps { - sh "export ip = `cat /tmp/inventory`" + sh "export ip=`cat /tmp/inventory`" } } } diff --git a/inventory.template b/inventory.template index a930526..f03f2b9 100644 --- a/inventory.template +++ b/inventory.template @@ -1,2 +1 @@ -[webserver] ${web_public_ip} \ No newline at end of file From eea15247c11142105643495033f89f46a98d3da6 Mon Sep 17 00:00:00 2001 From: Ragesh Date: Fri, 14 May 2021 14:50:38 -0400 Subject: [PATCH 63/68] jenkins-file-updated --- Jenkins-file | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/Jenkins-file b/Jenkins-file index 5fe7b76..21d7a03 100644 --- a/Jenkins-file +++ b/Jenkins-file @@ -10,6 +10,8 @@ pipeline { ''', name: 'Cloud' choice choices: ['us-west-1', 'ap-south-1', 'eu-central-1'], description: '''Select the region ''', name: 'Region' + choice choices: ['Ragesh', 'Moiz'], description: '''Server owner + ''', name: 'Owner' choice choices: ['t2.micro', 't2.small', 't2.medium'], description: '''Select the appropriate instance type. For more information https://aws.amazon.com/ec2/instance-types/ @@ -64,16 +66,19 @@ pipeline { sh "terraform apply -input=false tfplan" } } - stage('output') { + stage('Output') { steps { - sh "export ip=`cat /tmp/inventory`" + sh "export ip=`cat inventory`" } } + + } - post { - always { - archiveArtifacts artifacts: 'tfplan.txt' - } + success { + mail to: 'rageshmoorkoth@gmail.com', + subject: "Server build completed", + body: "Server name ${params.ServerName}" } +} } \ No newline at end of file From 3c2e6db962e454ce1ec90916ded70a63202d342d Mon Sep 17 00:00:00 2001 From: Ragesh Date: Sat, 15 May 2021 08:08:37 -0400 Subject: [PATCH 64/68] jenkins-pipeline --- Jenkins-file | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Jenkins-file b/Jenkins-file index 21d7a03..6f94898 100644 --- a/Jenkins-file +++ b/Jenkins-file @@ -66,9 +66,9 @@ pipeline { sh "terraform apply -input=false tfplan" } } - stage('Output') { + stage('Remove State file') { steps { - sh "export ip=`cat inventory`" + sh "rm -fr terraform.tfstate" } } From 415ed972d34f30ff90c38f2b6536d68fcc854256 Mon Sep 17 00:00:00 2001 From: Ragesh Date: Sun, 30 May 2021 02:07:52 -0400 Subject: [PATCH 65/68] jenkins-files update --- Jenkins-file | 18 ++++----- jenkins-configuration | 91 +++++++++++++++++++++++++++++++++++++++++++ resource.tf | 2 +- variables.tf | 4 ++ 4 files changed, 105 insertions(+), 10 deletions(-) create mode 100644 jenkins-configuration diff --git a/Jenkins-file b/Jenkins-file index 6f94898..0e3fb92 100644 --- a/Jenkins-file +++ b/Jenkins-file @@ -4,10 +4,10 @@ pipeline { terraform 'terraform-Rag' } parameters { - string(name: 'environment', defaultValue: 'default', description: 'Workspace/environment file to use for deployment') - string(name: 'version', defaultValue: '', description: 'Version variable to pass to Terraform') - choice choices: ['AWS', 'AZURE', 'OracleCloud'], description: '''Select the cloud platform - ''', name: 'Cloud' + //string(name: 'environment', defaultValue: 'default', description: 'Workspace/environment file to use for deployment') + //string(name: 'version', defaultValue: '', description: 'Version variable to pass to Terraform') + //choice choices: ['AWS', 'AZURE', 'OracleCloud'], description: '''Select the cloud platform + //''', name: 'Cloud' choice choices: ['us-west-1', 'ap-south-1', 'eu-central-1'], description: '''Select the region ''', name: 'Region' choice choices: ['Ragesh', 'Moiz'], description: '''Server owner @@ -35,12 +35,12 @@ pipeline { } stage('Plan') { steps { - script { - currentBuild.displayName = params.version - } + //script { + // currentBuild.displayName = params.version + //} sh 'terraform init -input=false' - sh 'terraform workspace select ${environment}' - sh "terraform plan -input=false -out tfplan -var 'region=${params.Region}' -var 'instance_type=${params.Configuration}' -var 'instance_name=${params.ServerName}' -var 'cloudprovider=${params.Cloud}'" + //sh 'terraform workspace select ${environment}' + sh "terraform plan -input=false -out tfplan -var 'region=${params.Region}' -var 'instance_type=${params.Configuration}' -var 'instance_name=${params.ServerName}'" sh 'terraform show -no-color tfplan > tfplan.txt' } } diff --git a/jenkins-configuration b/jenkins-configuration new file mode 100644 index 0000000..1abebea --- /dev/null +++ b/jenkins-configuration @@ -0,0 +1,91 @@ +pipeline { + agent any + tools { + terraform 'terraform-Rag' + } + parameters { + //string(name: 'environment', defaultValue: 'default', description: 'Workspace/environment file to use for deployment') + //string(name: 'version', defaultValue: '', description: 'Version variable to pass to Terraform') + //choice choices: ['Oracle Linux', 'Microsoft Windows'], description: '''Select the Operating System + //''', name: 'OS' + choice choices: ['us-west-1', 'ap-south-1', 'eu-central-1'], description: '''Select the region + ''', name: 'Region' + choice choices: ['Ragesh', 'Moiz','Sourabh'], description: '''Server owner + ''', name: 'Owner' + choice choices: ['t2.micro', 't2.small', 't2.medium'], description: '''Select the appropriate instance type. + For more information + https://aws.amazon.com/ec2/instance-types/ + ''', name: 'Configuration' + string defaultValue: '', description: 'Input server Name', name: 'ServerName', trim: false + + booleanParam(name: 'autoApprove', defaultValue: false, description: 'Automatically run apply after generating plan?') + } + + environment { + AWS_ACCESS_KEY_ID = credentials('AWS-ACCESS-KEY-ID') + AWS_SECRET_ACCESS_KEY = credentials('AWS-SECRET-KEY') + TF_IN_AUTOMATION = '1' + } + stages { + stage('Git Checkout'){ + steps{ + git credentialsId: 'GITHUB-RAGEH', url: 'https://github.com/Rageshmookoth/iac-demo' + } + } + stage('OS Image select'){ + + steps{ + + } + + } + + stage('Plan') { + steps { + //script { + // currentBuild.displayName = params.version + //} + sh 'terraform init -input=false' + //sh 'terraform workspace select ${environment}' + sh "terraform plan -input=false -out tfplan -var 'region=${params.Region}' -var 'instance_type=${params.Configuration}' -var 'instance_name=${params.ServerName}'" + sh 'terraform show -no-color tfplan > tfplan.txt' + } + } + + stage('Approval') { + when { + not { + equals expected: true, actual: params.autoApprove + } + } + + steps { + script { + def plan = readFile 'tfplan.txt' + input message: "Do you want to apply the plan?", + parameters: [text(name: 'Plan', description: 'Please review the plan', defaultValue: plan)] + } + } + } + + stage('Apply') { + steps { + sh "terraform apply -input=false tfplan" + } + } + stage('Remove State file') { + steps { + sh "rm -fr terraform.tfstate" + } + } + + + } + post { + success { + mail to: 'rageshmoorkoth@gmail.com', + subject: "Server build completed", + body: "Server name ${params.ServerName}" + } +} +} \ No newline at end of file diff --git a/resource.tf b/resource.tf index 50d8693..ebc92b4 100644 --- a/resource.tf +++ b/resource.tf @@ -1,5 +1,5 @@ resource "aws_instance" "Ragesh" { -ami = "ami-045e6fa7127ab1ac4" +ami = var.ami instance_type = var.instance_type key_name = "POC-STD-KEY-PAIR" tags = { diff --git a/variables.tf b/variables.tf index 9deff8e..e510afe 100644 --- a/variables.tf +++ b/variables.tf @@ -18,6 +18,10 @@ variable "region" { description = "AWS Region the infrastructure is hosted in" default = "us-east-1" } +variable "ami" { +description = "AMI Image" +default = "ami-045e6fa7127ab1ac4" +} variable "cloudprovider" { description = "Select the Cloud Provider" default = "AWS" From fa79ce2ca251dde0c8d23afeb85e6503c7d8b7df Mon Sep 17 00:00:00 2001 From: Ragesh Date: Sun, 30 May 2021 02:55:24 -0400 Subject: [PATCH 66/68] Tag info updated --- Jenkins-file | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Jenkins-file b/Jenkins-file index 0e3fb92..1f1fb0e 100644 --- a/Jenkins-file +++ b/Jenkins-file @@ -12,6 +12,9 @@ pipeline { ''', name: 'Region' choice choices: ['Ragesh', 'Moiz'], description: '''Server owner ''', name: 'Owner' + choice choices: ['DEV-GROUP', 'IT-GROUP'], description: '''Server Group + ''', name: 'Group' + string defaultValue: '', description: 'Project', name: 'Project', trim: false choice choices: ['t2.micro', 't2.small', 't2.medium'], description: '''Select the appropriate instance type. For more information https://aws.amazon.com/ec2/instance-types/ @@ -40,7 +43,7 @@ pipeline { //} sh 'terraform init -input=false' //sh 'terraform workspace select ${environment}' - sh "terraform plan -input=false -out tfplan -var 'region=${params.Region}' -var 'instance_type=${params.Configuration}' -var 'instance_name=${params.ServerName}'" + sh "terraform plan -input=false -out tfplan -var 'region=${params.Region}' -var 'instance_type=${params.Configuration}' -var 'instance_name=${params.ServerName}' -var 'Group=${params.Group}' -var 'Owner=${params.Owner}' -var 'Project=${params.Project}'" sh 'terraform show -no-color tfplan > tfplan.txt' } } From c052d4771c43c824e2a172df46a0d4bfbf59b713 Mon Sep 17 00:00:00 2001 From: Ragesh Date: Sun, 30 May 2021 03:06:48 -0400 Subject: [PATCH 67/68] variable 9pdated --- variables.tf | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/variables.tf b/variables.tf index e510afe..5131ea8 100644 --- a/variables.tf +++ b/variables.tf @@ -39,4 +39,14 @@ variable "private_key" { } variable "ansible_user" { default = "ec2-user" +} +variable "Group" { +description = "Group Tag" +type = string +default = "No-Group" +} +variable "Project" { +description = "Project name Tag" +type = string +default = "No-Project" } \ No newline at end of file From f11d210825d8d42b3ceaa2d06ee9f7861350b91f Mon Sep 17 00:00:00 2001 From: Ragesh Date: Sun, 30 May 2021 03:14:45 -0400 Subject: [PATCH 68/68] resources file updated --- resource.tf | 2 ++ 1 file changed, 2 insertions(+) diff --git a/resource.tf b/resource.tf index ebc92b4..b014aeb 100644 --- a/resource.tf +++ b/resource.tf @@ -5,6 +5,8 @@ key_name = "POC-STD-KEY-PAIR" tags = { Name = var.instance_name Owner = var.Owner + Group = var.Group + Project = var.Project } } #resource "null_resource" "step1" {