Skip to content

Commit ea7c43a

Browse files
author
ramureddy
committed
add amazon eks cluster plane with storage,network managed nodegroup modules
0 parents  commit ea7c43a

24 files changed

+929
-0
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
*.tfvars
2+
*.tfstate
3+
*.tfstate.backup
4+
*.terraform
5+
*tfplan*
6+
*tfplan
7+

Jenkinsfile

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import groovy.json.JsonOutput
2+
env.terraform_version = '0.12.2'
3+
pipeline {
4+
5+
parameters {
6+
choice(name: 'action', choices: 'create\ndestroy', description: 'Create/update or destroy the eks cluster.')
7+
string(name: 'aws_region', defaultValue : 'us-west-2', description: "AWS region.")
8+
string(name: 'env', defaultValue: 'dev', description: "lab environment")
9+
string(name: 'rolename', defaultValue: 'aws-jenkins', description: "default aws role for jenkins")
10+
string(name: 'role-account', defaultValue: '534992115889', description: "default aws role account for jenkins")
11+
string(name: 'cluster', defaultValue: 'tl-eks-terraform', description: "eks cluster name")
12+
string(name: 'cidrblock', defaultValue : '10.123.0.0/16', description: "First 2 octets of vpc network; eg 10.0")
13+
string(name: 'cidr_public', defaultValue: '["10.123.1.0/24","10.123.2.0/24"]', description: "cidr block for public subnets")
14+
string(name: 'cidr_private', defaultValue: '["10.123.3.0/24","10.123.4.0/24"]', description: "cidr block for private subnets")
15+
16+
17+
}
18+
19+
options {
20+
disableConcurrentBuilds()
21+
timeout(time: 1, unit: 'HOURS')
22+
}
23+
24+
agent { label 'master' }
25+
26+
stages {
27+
28+
stage('Setup') {
29+
steps {
30+
script {
31+
currentBuild.displayName = "#" + env.BUILD_NUMBER + " " + params.action + params.cluster
32+
//plan = params.cluster + '.plan'
33+
}
34+
}
35+
}
36+
37+
stage('Install dependencies') {
38+
steps {
39+
sh "sudo yum install wget zip python-pip -y"
40+
sh "cd /tmp"
41+
sh "curl -o terraform.zip https://releases.hashicorp.com/terraform/'$terraform_version'/terraform_'$terraform_version'_linux_amd64.zip"
42+
sh "unzip terraform.zip"
43+
sh "sudo mv terraform /usr/bin"
44+
sh "rm -rf terraform.zip"
45+
sh "terraform version"
46+
}
47+
}
48+
49+
stage('Git checkout') {
50+
steps {
51+
dir('tl-eks-terraform') {
52+
git branch: 'master', credentialsId: '', url: 'https://github.nikecom/CIS/tl-eks-terraform.git'
53+
}
54+
}
55+
}
56+
57+
58+
stage('Terraform dev plan') {
59+
when {
60+
expression { params.action == 'create' && params.env == 'dev' }
61+
}
62+
steps {
63+
dir('tl-eks-terraform/environments/dev') {
64+
script {
65+
withAWS([profile:${params.env}, region:${params.aws_region}, role:${params.rolename}, roleAccount:${params.role-account}]) {
66+
67+
sh "terraform init"
68+
sh "terraform plan -out tfplan"
69+
}
70+
}
71+
}
72+
}
73+
}
74+
75+
76+
stage('Terraform prod plan') {
77+
when {
78+
expression { params.action == 'create' && params.env == 'prod' }
79+
}
80+
steps {
81+
dir('tl-eks-terraform/environments/prod') {
82+
script {
83+
withAWS([profile:${params.env}, region:${params.aws_region}, role:${params.rolename}, roleAccount:${params.role-account}]) {
84+
sh "terraform init"
85+
sh "terraform plan -out tfplan"
86+
}
87+
}
88+
}
89+
}
90+
}
91+
92+
stage('TF Apply') {
93+
when {
94+
expression { params.action == 'create' && params.env == 'dev'}
95+
}
96+
steps {
97+
dir('tl-eks-terraform/environments/dev') {
98+
script {
99+
input "Create/update Terraform stack eks-${params.cluster} in aws?"
100+
101+
withAWS([profile:${params.env}, region:${params.aws_region}, role:${params.rolename}, roleAccount:${params.role-account}]) {
102+
sh "terraform apply -input=false -auto-approve tfplan"
103+
104+
}
105+
}
106+
}
107+
}
108+
}
109+
110+
stage('TF Destroy') {
111+
when {
112+
expression { params.action == 'destroy' }
113+
}
114+
steps {
115+
dir('tl-eks-terraform') {
116+
script {
117+
input "Destroy Terraform stack eks-${params.cluster} in aws?"
118+
119+
withAWS([profile:${params.env}, region:${params.aws_region}, role:${params.rolename}, roleAccount:${params.role-account}]) {
120+
sh "terraform destroy -auto-approve"
121+
122+
}
123+
}
124+
}
125+
}
126+
}
127+
128+
}
129+
130+
}

README.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# tl-eks-terraform
2+
Resources required for deployment of tl accounts on EKS using terraform IAC
3+
4+
**Requirement**
5+
--------------------------
6+
terraform v0.12, python-pydot, python-pydot-ng and graphviz for resource graph visualization
7+
8+
AWS provider version auto installed from terrafrom init and set to versoon "~2.0"
9+
10+
11+
**Usage**
12+
-----------------------------------
13+
14+
Use aws configure to load your api access keys to default credentials file at "~/.aws/credentials"
15+
16+
Create 2 profiles dev and prod and use the same for aws credentials as well.
17+
18+
terraform variables will be defaulted to region=us-west-2 and profile=dev if not specified during the terraform plan
19+
20+
21+
terraform validate
22+
23+
terraform init
24+
25+
terraform plan -out=tfplan
26+
27+
terraform apply tfplan
28+
29+
terraform destroy -auto-approve
30+
31+
32+
**Resource graph**
33+
--------------------------------
34+
Resource graph of the terraform plan. Use graphviz to analyze it
35+
36+
sudo apt install -y python-pydot python-pydot-ng graphviz
37+
38+
terraform graph | dot -Tsvg > images/resource-graph.svg
39+
40+
![](images/resource-graph.svg)
41+
42+
43+
**Output**
44+
-----------------------------------------------------
45+
46+
Apply complete! Resources: 33 added, 0 changed, 0 destroyed.
47+
48+
The state of your infrastructure has been saved to the path
49+
below. This state is required to modify and destroy your
50+
infrastructure, so keep it safe. To inspect the complete state
51+
use the `terraform show` command.
52+
53+
State path: terraform.tfstate
54+
55+
56+
tl-dev-terraform-IGW = igw-031d081a7b416705c
57+
58+
tl-dev-terraform-dynamodb_name = tl-eks-dev-terraformstate
59+
60+
tl-dev-terraform-eks-clusterSecuritygroup = tf-tl-eks-cluster
61+
62+
tl-dev-terraform-eks-endpoint = https://B19BF0FE0520390101CA88F1981AE46C.gr7.us-west-2.eks.amazonaws.com
63+
64+
tl-dev-terraform-eks-kubeconfig-cert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Vid1lpS3pyb2VqalhDNlFJeDlWL1FuCnlPOVlUdWJ6b1VvWFZIMGVqMnBzcWN4dmdabHlmVFlndlJmMEttbC9YZ3dKQ3BYZ2R3aGUxMllBcVhKT0JLYVQKdURFNUoyeFFTcGIzV1EvK2xEQkZqQXc5bkJpclNBU25kUWxQWXhhQzc5NncyTHg5bVUrdE4zY2VLWTFoWWxJUwpqcVBEYk05TGtKdndTYWxzeWE1VUxnd3FXbVpqSTQzekJ6VjVRZXRPalZRVW1qUTh1SVhIR0hjZHF6Yz0KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo=
65+
66+
tl-dev-terraform-eks-workernodesecuritygroup = tf-tl-eks-nodegroup
67+
68+
tl-dev-terraform-natGW = 52.34.227.104
69+
70+
tl-dev-terraform-privatesubnets = [
71+
"subnet-0d0ee7cf55ed1253d",
72+
"subnet-0e5f335c6d4a3b851",
73+
]
74+
75+
tl-dev-terraform-publicsubnets = [
76+
"subnet-0dc6ef0e3d1a6d783",
77+
"subnet-0888f3492d5c91bef",
78+
]
79+
80+
tl-dev-terraform-vpc = vpc-05ad72d1d7c3608ab
81+
82+
tl-dev-terraformbucket_name = arn:aws:s3:::tl-eks-dev-terraformstate
83+
84+

environments/dev/main.tf

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
##################################################################
2+
#-------Author: Ramu Reddy --------------------------------
3+
#-------Terraform main config file for provider, backend and eks---
4+
####################################################################
5+
6+
provider "aws" {
7+
region = var.aws_region
8+
shared_credentials_file = "~/.aws/credentials"
9+
/*assume_role {
10+
role_arn = "arn:aws:iam::var.role-account:role/var.rolename"
11+
}*/
12+
profile = var.env
13+
version = "~> 2.0"
14+
}
15+
16+
17+
terraform {
18+
backend "s3" {
19+
shared_credentials_file = "~/.aws/credentials"
20+
profile = "dev"
21+
//role_arn = "arn:aws:iam::var.role-account:role/var.rolename"
22+
bucket = "tl-eks-dev-terraformstate"
23+
key = "dev/backup-state/terraform.tfstate"
24+
region = "us-west-2"
25+
dynamodb_table = "tl-eks-dev-terraformstate"
26+
encrypt = true
27+
}
28+
}
29+
30+
31+
32+
module "storage" {
33+
source = "../../modules/storage"
34+
}
35+
36+
37+
module "networking" {
38+
source = "../../modules/networking"
39+
cidrblock = var.cidrblock
40+
cidr_public = var.cidr_public
41+
cidr_private = var.cidr_private
42+
cluster-name = var.eks-clustername
43+
}
44+
45+
46+
module "securitygroups" {
47+
source = "../../modules/securitygroups"
48+
vpc_id = module.networking.vpc-id
49+
}
50+
51+
52+
module "eks" {
53+
source = "../../modules/eks"
54+
cluster-name = var.eks-clustername
55+
pub-subnet-ids = module.networking.public_subnet
56+
private-subnet-ids = module.networking.private_subnet
57+
tf-tl-eks-cluster-sg = module.securitygroups.tf-tl-eks-clustersecuritygroup-id
58+
kubernetes-version = var.eks-kubernetes-version
59+
nodegroupname = var.node-group-name
60+
instance-type = var.instancetype
61+
ami-type = var.amitype
62+
}

environments/dev/outputs.tf

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
###############################################################
2+
#------------------Author: Ramu Reddy -----------------
3+
#------------------output terraform resources-----------------
4+
################################################################
5+
6+
output "tl-dev-terraformbucket_name" {
7+
value = module.storage.tl-dev-terraformbucket
8+
description = "The tl eks terraform state bucket"
9+
}
10+
11+
output "tl-dev-terraform-dynamodb_name" {
12+
value = module.storage.tl-dev-terraform-dynamodb
13+
description = "The tl eks terraform dynamodb table for locking state"
14+
}
15+
16+
output "tl-dev-terraform-vpc" {
17+
value = module.networking.vpc-id
18+
description = "The tl eks terraform vpc ID"
19+
}
20+
21+
output "tl-dev-terraform-publicsubnets" {
22+
value = module.networking.public_subnet
23+
description = "tl dev eks public subnets"
24+
}
25+
26+
output "tl-dev-terraform-privatesubnets" {
27+
value = module.networking.private_subnet
28+
description = "tl dev eks private subnets"
29+
}
30+
31+
output "tl-dev-terraform-IGW" {
32+
value = module.networking.igw-id
33+
description = "tl eks terraform Internet gateway"
34+
}
35+
36+
output "tl-dev-terraform-natGW" {
37+
value = module.networking.nat-gw
38+
description = "tl eks terraform nat gateway"
39+
}
40+
41+
output "tl-dev-terraform-eks-clusterSecuritygroup" {
42+
value = module.securitygroups.tf-tl-eks-clustersecuritygroup
43+
}
44+
45+
output "tl-dev-terraform-eks-workernodesecuritygroup" {
46+
value = module.securitygroups.tf-tl-eks-workernodesecuritygroup
47+
}
48+
49+
output "tl-dev-terraform-eks-endpoint" {
50+
value = module.eks.tf-tl-eks-endpoint
51+
}
52+
output "tl-dev-terraform-eks-kubeconfig-cert-authority-data" {
53+
value = module.eks.tf-tl-eks-kubeconfig-certificate-authority-data
54+
}

0 commit comments

Comments
 (0)