Skip to content

Commit 4dee368

Browse files
committed
First Commit
1 parent 92f0c68 commit 4dee368

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1095
-0
lines changed

.gitignore

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Local .terraform directories
2+
**/.terraform/*
3+
4+
# .tfstate files
5+
*.tfstate
6+
*.tfstate.*
7+
8+
# Crash log files
9+
crash.log
10+
11+
# Ignore any .tfvars files that are generated automatically for each Terraform run. Most
12+
# .tfvars files are managed as part of configuration and so should be included in
13+
# version control.
14+
#
15+
# example.tfvars
16+
17+
# Ignore override files as they are usually used to override resources locally and so
18+
# are not checked in
19+
override.tf
20+
override.tf.json
21+
*_override.tf
22+
*_override.tf.json
23+
24+
# Include override files you do wish to add to version control using negated pattern
25+
#
26+
# !example_override.tf
27+
28+
# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
29+
# example: *tfplan*

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# terraform-iacdevops-with-aws-codepipeline
2+
terraform-iacdevops-with-aws-codepipeline

buildspec-dev.yml

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
version: 0.2
2+
3+
env:
4+
variables:
5+
TERRAFORM_VERSION: "0.15.3"
6+
TF_COMMAND: "apply"
7+
#TF_COMMAND: "destroy"
8+
parameter-store:
9+
AWS_ACCESS_KEY_ID: "/CodeBuild/MY_AWS_ACCESS_KEY_ID"
10+
AWS_SECRET_ACCESS_KEY: "/CodeBuild/MY_AWS_SECRET_ACCESS_KEY"
11+
12+
phases:
13+
install:
14+
runtime-versions:
15+
python: 3.7
16+
on-failure: ABORT
17+
commands:
18+
- tf_version=$TERRAFORM_VERSION
19+
- wget https://releases.hashicorp.com/terraform/"$TERRAFORM_VERSION"/terraform_"$TERRAFORM_VERSION"_linux_amd64.zip
20+
- unzip terraform_"$TERRAFORM_VERSION"_linux_amd64.zip
21+
- mv terraform /usr/local/bin/
22+
pre_build:
23+
on-failure: ABORT
24+
commands:
25+
- echo terraform execution started on `date`
26+
build:
27+
on-failure: ABORT
28+
commands:
29+
# Project-1: AWS VPC, ASG, ALB, Route53, ACM, Security Groups and SNS
30+
- cd "$CODEBUILD_SRC_DIR/terraform-manifests"
31+
- ls -lrt "$CODEBUILD_SRC_DIR/terraform-manifests"
32+
- terraform --version
33+
- terraform init -input=false --backend-config=dev.conf
34+
- terraform validate
35+
- terraform plan -lock=false -input=false -var-file=dev.tfvars
36+
- terraform $TF_COMMAND -input=false -var-file=dev.tfvars -auto-approve
37+
post_build:
38+
on-failure: CONTINUE
39+
commands:
40+
- echo terraform execution completed on `date`
41+
42+
43+

buildspec-stag.yml

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
version: 0.2
2+
3+
env:
4+
variables:
5+
TERRAFORM_VERSION: "0.15.3"
6+
TF_COMMAND: "apply"
7+
#TF_COMMAND: "destroy"
8+
parameter-store:
9+
AWS_ACCESS_KEY_ID: "/CodeBuild/MY_AWS_ACCESS_KEY_ID"
10+
AWS_SECRET_ACCESS_KEY: "/CodeBuild/MY_AWS_SECRET_ACCESS_KEY"
11+
12+
phases:
13+
install:
14+
runtime-versions:
15+
python: 3.7
16+
on-failure: ABORT
17+
commands:
18+
- tf_version=$TERRAFORM_VERSION
19+
- wget https://releases.hashicorp.com/terraform/"$TERRAFORM_VERSION"/terraform_"$TERRAFORM_VERSION"_linux_amd64.zip
20+
- unzip terraform_"$TERRAFORM_VERSION"_linux_amd64.zip
21+
- mv terraform /usr/local/bin/
22+
pre_build:
23+
on-failure: ABORT
24+
commands:
25+
- echo terraform execution started on `date`
26+
build:
27+
on-failure: ABORT
28+
commands:
29+
# Project-1: AWS VPC, ASG, ALB, Route53, ACM, Security Groups and SNS
30+
- cd "$CODEBUILD_SRC_DIR/terraform-manifests"
31+
- ls -lrt "$CODEBUILD_SRC_DIR/terraform-manifests"
32+
- terraform --version
33+
- terraform init -input=false --backend-config=stag.conf
34+
- terraform validate
35+
- terraform plan -lock=false -input=false -var-file=stag.tfvars
36+
- terraform $TF_COMMAND -input=false -var-file=stag.tfvars -auto-approve
37+
post_build:
38+
on-failure: CONTINUE
39+
commands:
40+
- echo terraform execution completed on `date`
41+
42+
43+

terraform-manifests/app1-install.sh

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#! /bin/bash
2+
# Instance Identity Metadata Reference - https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-identity-documents.html
3+
sudo yum update -y
4+
sudo yum install -y httpd
5+
sudo systemctl enable httpd
6+
sudo service httpd start
7+
sudo echo '<h1>Welcome to StackSimplify - APP-1</h1>' | sudo tee /var/www/html/index.html
8+
sudo mkdir /var/www/html/app1
9+
sudo echo '<!DOCTYPE html> <html> <body style="background-color:rgb(250, 210, 210);"> <h1>Welcome to Stack Simplify - APP-1</h1> <p>Terraform Demo</p> <p>Application Version: V1</p> </body></html>' | sudo tee /var/www/html/app1/index.html
10+
sudo curl http://169.254.169.254/latest/dynamic/instance-identity/document -o /var/www/html/app1/metadata.html
11+
12+

terraform-manifests/c1-versions.tf

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Terraform Block
2+
terraform {
3+
required_version = "~> 0.14" # which means any version equal & above 0.14 like 0.15, 0.16 etc and < 1.xx
4+
required_providers {
5+
aws = {
6+
source = "hashicorp/aws"
7+
version = "~> 3.0"
8+
}
9+
null = {
10+
source = "hashicorp/null"
11+
version = "~> 3.0"
12+
}
13+
random = {
14+
source = "hashicorp/random"
15+
version = "~> 3.0"
16+
}
17+
}
18+
# Adding Backend as S3 for Remote State Storage
19+
backend "s3" {}
20+
}
21+
22+
# Provider Block
23+
provider "aws" {
24+
region = var.aws_region
25+
profile = "default"
26+
}
27+
/*
28+
Note-1: AWS Credentials Profile (profile = "default") configured on your local desktop terminal
29+
$HOME/.aws/credentials
30+
*/
31+
32+
# Create Random Pet Resource
33+
resource "random_pet" "this" {
34+
length = 2
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Terraform AWS Application Load Balancer Variables
2+
# Place holder file for AWS ALB Variables
3+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# Terraform AWS Application Load Balancer (ALB)
2+
module "alb" {
3+
source = "terraform-aws-modules/alb/aws"
4+
#version = "5.16.0"
5+
version = "6.0.0"
6+
7+
name = "${local.name}-alb"
8+
load_balancer_type = "application"
9+
vpc_id = module.vpc.vpc_id
10+
/*Option-1: Give as list with specific subnets or in next line, pass all public subnets
11+
subnets = [
12+
module.vpc.public_subnets[0],
13+
module.vpc.public_subnets[1]
14+
]*/
15+
subnets = module.vpc.public_subnets
16+
#security_groups = [module.loadbalancer_sg.this_security_group_id]
17+
security_groups = [module.loadbalancer_sg.security_group_id]
18+
# Listeners
19+
# HTTP Listener - HTTP to HTTPS Redirect
20+
http_tcp_listeners = [
21+
{
22+
port = 80
23+
protocol = "HTTP"
24+
action_type = "redirect"
25+
redirect = {
26+
port = "443"
27+
protocol = "HTTPS"
28+
status_code = "HTTP_301"
29+
}
30+
}
31+
]
32+
# Target Groups
33+
target_groups = [
34+
# App1 Target Group - TG Index = 0
35+
{
36+
name_prefix = "app1-"
37+
backend_protocol = "HTTP"
38+
backend_port = 80
39+
target_type = "instance"
40+
deregistration_delay = 10
41+
health_check = {
42+
enabled = true
43+
interval = 30
44+
path = "/app1/index.html"
45+
port = "traffic-port"
46+
healthy_threshold = 3
47+
unhealthy_threshold = 3
48+
timeout = 6
49+
protocol = "HTTP"
50+
matcher = "200-399"
51+
}
52+
protocol_version = "HTTP1"
53+
/* # App1 Target Group - Targets
54+
targets = {
55+
my_app1_vm1 = {
56+
target_id = module.ec2_private_app1.id[0]
57+
port = 80
58+
},
59+
my_app1_vm2 = {
60+
target_id = module.ec2_private_app1.id[1]
61+
port = 80
62+
}
63+
}
64+
tags =local.common_tags # Target Group Tags*/
65+
},
66+
]
67+
68+
# HTTPS Listener
69+
https_listeners = [
70+
# HTTPS Listener Index = 0 for HTTPS 443
71+
{
72+
port = 443
73+
protocol = "HTTPS"
74+
#certificate_arn = module.acm.this_acm_certificate_arn
75+
certificate_arn = module.acm.acm_certificate_arn
76+
action_type = "fixed-response"
77+
fixed_response = {
78+
content_type = "text/plain"
79+
message_body = "Fixed Static message - for Root Context"
80+
status_code = "200"
81+
}
82+
},
83+
]
84+
85+
# HTTPS Listener Rules
86+
https_listener_rules = [
87+
# Rule-1: /app1* should go to App1 EC2 Instances
88+
{
89+
https_listener_index = 0
90+
priority = 1
91+
actions = [
92+
{
93+
type = "forward"
94+
target_group_index = 0
95+
}
96+
]
97+
conditions = [{
98+
path_patterns = ["/*"]
99+
}]
100+
},
101+
]
102+
tags = local.common_tags # ALB Tags
103+
}
104+
105+
106+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Terraform AWS Application Load Balancer (ALB) Outputs
2+
output "lb_id" {
3+
description = "The ID and ARN of the load balancer we created."
4+
value = module.alb.lb_id
5+
}
6+
7+
output "lb_arn" {
8+
description = "The ID and ARN of the load balancer we created."
9+
value = module.alb.lb_arn
10+
}
11+
12+
output "lb_dns_name" {
13+
description = "The DNS name of the load balancer."
14+
value = module.alb.lb_dns_name
15+
}
16+
17+
output "lb_arn_suffix" {
18+
description = "ARN suffix of our load balancer - can be used with CloudWatch."
19+
value = module.alb.lb_arn_suffix
20+
}
21+
22+
output "lb_zone_id" {
23+
description = "The zone_id of the load balancer to assist with creating DNS records."
24+
value = module.alb.lb_zone_id
25+
}
26+
27+
output "http_tcp_listener_arns" {
28+
description = "The ARN of the TCP and HTTP load balancer listeners created."
29+
value = module.alb.http_tcp_listener_arns
30+
}
31+
32+
output "http_tcp_listener_ids" {
33+
description = "The IDs of the TCP and HTTP load balancer listeners created."
34+
value = module.alb.http_tcp_listener_ids
35+
}
36+
37+
output "https_listener_arns" {
38+
description = "The ARNs of the HTTPS load balancer listeners created."
39+
value = module.alb.https_listener_arns
40+
}
41+
42+
output "https_listener_ids" {
43+
description = "The IDs of the load balancer listeners created."
44+
value = module.alb.https_listener_ids
45+
}
46+
47+
output "target_group_arns" {
48+
description = "ARNs of the target groups. Useful for passing to your Auto Scaling group."
49+
value = module.alb.target_group_arns
50+
}
51+
52+
output "target_group_arn_suffixes" {
53+
description = "ARN suffixes of our target groups - can be used with CloudWatch."
54+
value = module.alb.target_group_arn_suffixes
55+
}
56+
57+
output "target_group_names" {
58+
description = "Name of the target group. Useful for passing to your CodeDeploy Deployment Group."
59+
value = module.alb.target_group_names
60+
}
61+
62+
output "target_group_attachments" {
63+
description = "ARNs of the target group attachment IDs."
64+
value = module.alb.target_group_attachments
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# ACM Module - To create and Verify SSL Certificates
2+
module "acm" {
3+
source = "terraform-aws-modules/acm/aws"
4+
#version = "2.14.0"
5+
version = "3.0.0"
6+
7+
domain_name = trimsuffix(data.aws_route53_zone.mydomain.name, ".")
8+
zone_id = data.aws_route53_zone.mydomain.zone_id
9+
10+
subject_alternative_names = [
11+
#"*.devopsincloud.com"
12+
var.dns_name
13+
]
14+
tags = local.common_tags
15+
}
16+
17+
# Output ACM Certificate ARN
18+
output "this_acm_certificate_arn" {
19+
description = "The ARN of the certificate"
20+
#value = module.acm.this_acm_certificate_arn
21+
value = module.acm.acm_certificate_arn
22+
}
23+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# DNS Name Input Variable
2+
variable "dns_name" {
3+
description = "DNS Name to support multiple environments"
4+
type = string
5+
}
6+
# DNS Registration
7+
resource "aws_route53_record" "apps_dns" {
8+
zone_id = data.aws_route53_zone.mydomain.zone_id
9+
name = var.dns_name
10+
type = "A"
11+
alias {
12+
name = module.alb.lb_dns_name
13+
zone_id = module.alb.lb_zone_id
14+
evaluate_target_health = true
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Autoscaling Input Variables
2+
## Placeholder file

0 commit comments

Comments
 (0)