Skip to content

Commit 4724f9a

Browse files
authored
Merge pull request #14 from DTherHtun/chapter11-other-codes
Chapter11 other codes
2 parents 4ce1c30 + 63cade1 commit 4724f9a

File tree

17 files changed

+575
-13
lines changed

17 files changed

+575
-13
lines changed

chapter11/complete/modules/codepipeline/main.tf

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ resource "aws_codepipeline" "codepipeline" {
112112
version = "1"
113113

114114
configuration = {
115-
CustomData = "Please review output of plan and approve"
116-
NotificationArn = aws_sns_topic.codepipeline.arn
115+
CustomData = "Please review output of plan and approve"
116+
NotificationArn = aws_sns_topic.codepipeline.arn
117117
}
118118
}
119119
}

chapter11/complete/modules/codepipeline/variables.tf

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
variable "name" {
2-
type = string
3-
default = "terraform"
2+
type = string
3+
default = "terraform"
44
description = "A project name to use for resource mapping"
55
}
66

77
variable "auto_apply" {
8-
type = bool
9-
default = false
8+
type = bool
9+
default = false
1010
description = "Whether to automatically apply changes when a Terraform plan is successful. Defaults to false."
1111
}
1212

1313
variable "terraform_version" {
14-
type = string
15-
default = "latest"
14+
type = string
15+
default = "latest"
1616
description = "The version of Terraform to use for this workspace. Defaults to the latest available version."
1717
}
1818

1919
variable "working_directory" {
20-
type = string
21-
default = "."
20+
type = string
21+
default = "."
2222
description = "A relative path that Terraform will execute within. Defaults to the root of your repository."
2323
}
2424

2525
variable "vcs_repo" {
26-
type = object({ identifier = string, branch = string, oauth_token = string })
26+
type = object({ identifier = string, branch = string, oauth_token = string })
2727
description = "Settings for the workspace's VCS repository."
2828
}
2929

3030
variable "environment" {
31-
type = map(string)
32-
default = {}
31+
type = map(string)
32+
default = {}
3333
description = "A map of environment varaibles to use for this workspace"
3434
}
3535

chapter11/listing11.1/main.tf

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
variable "aws" {
2+
type = object({ access_key = string, secret_key = string, region = string })
3+
}
4+
5+
variable "vcs_repo" {
6+
type = object({ identifier = string, branch = string, oauth_token = string })
7+
}
8+
9+
provider "aws" {
10+
access_key = var.aws.access_key
11+
secret_key = var.aws.secret_key
12+
region = var.aws.region
13+
}
14+
15+
module "s3backend" {
16+
source = "scottwinkler/s3backend/aws"
17+
}
18+
19+
module "codepipeline" {
20+
source = "./modules/codepipeline"
21+
name = "terraform-in-action"
22+
vcs_repo = var.vcs_repo
23+
environment = {
24+
AWS_ACCESS_KEY_ID = var.aws.access_key
25+
AWS_SECRET_ACCESS_KEY = var.aws.secret_key
26+
}
27+
s3_backend_config = module.s3backend.config
28+
}

chapter11/listing11.10/main.tf

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
resource "aws_s3_bucket" "codepipeline_bucket" {
2+
bucket = "${local.namespace}-codepipeline-bucket"
3+
acl = "private"
4+
force_destroy = true
5+
}
6+
7+
resource "aws_sns_topic" "codepipeline" {
8+
name = "${local.namespace}-pipeline-topic"
9+
}

chapter11/listing11.11/main.tf

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
resource "aws_codepipeline" "codepipeline" {
2+
name = "${local.namespace}-pipeline"
3+
role_arn = aws_iam_role.codepipeline_role.arn
4+
5+
artifact_store {
6+
location = aws_s3_bucket.codepipeline_bucket.bucket
7+
type = "S3"
8+
}
9+
10+
stage {
11+
name = "Source"
12+
13+
action {
14+
name = "Source"
15+
category = "Source"
16+
owner = "ThirdParty"
17+
provider = "GitHub"
18+
version = "1"
19+
output_artifacts = ["source_output"]
20+
21+
configuration = {
22+
Owner = split("/", var.vcs_repo.identifier)[0]
23+
Repo = split("/", var.vcs_repo.identifier)[1]
24+
Branch = var.vcs_repo.branch
25+
OAuthToken = var.vcs_repo.oauth_token
26+
}
27+
}
28+
}
29+
30+
stage {
31+
name = "Plan"
32+
33+
action {
34+
name = "Plan"
35+
category = "Build"
36+
owner = "AWS"
37+
provider = "CodeBuild"
38+
input_artifacts = ["source_output"]
39+
version = "1"
40+
41+
configuration = {
42+
ProjectName = aws_codebuild_project.project[0].name
43+
EnvironmentVariables = local.environment
44+
}
45+
}
46+
}
47+
48+
dynamic "stage" {
49+
for_each = ! var.auto_apply ? [1] : []
50+
content {
51+
name = "Approval"
52+
53+
action {
54+
name = "Approval"
55+
category = "Approval"
56+
owner = "AWS"
57+
provider = "Manual"
58+
version = "1"
59+
60+
configuration = {
61+
CustomData = "Please review output of plan and approve"
62+
NotificationArn = aws_sns_topic.codepipeline.arn
63+
}
64+
}
65+
}
66+
}
67+
68+
stage {
69+
name = "Apply"
70+
71+
action {
72+
name = "Apply"
73+
category = "Build"
74+
owner = "AWS"
75+
provider = "CodeBuild"
76+
input_artifacts = ["source_output"]
77+
version = "1"
78+
79+
configuration = {
80+
ProjectName = aws_codebuild_project.project[1].name
81+
EnvironmentVariables = local.environment
82+
}
83+
}
84+
}
85+
}

chapter11/listing11.12/main.tf

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
resource "random_string" "rand" {
2+
length = 24
3+
special = false
4+
upper = false
5+
}
6+
7+
locals {
8+
namespace = substr(join("-", [var.name, random_string.rand.result]), 0, 24)
9+
projects = ["plan", "apply"]
10+
}
11+
12+
resource "aws_codebuild_project" "project" {
13+
count = length(local.projects)
14+
name = "${local.namespace}-${local.projects[count.index]}"
15+
service_role = aws_iam_role.codebuild_role.id
16+
17+
artifacts {
18+
type = "NO_ARTIFACTS"
19+
}
20+
21+
environment {
22+
compute_type = "BUILD_GENERAL1_SMALL"
23+
image = "hashicorp/terraform:${var.terraform_version}"
24+
type = "LINUX_CONTAINER"
25+
}
26+
27+
source {
28+
type = "NO_SOURCE"
29+
buildspec = file("${path.module}/templates/buildspec_${local.projects[count.index]}.yml")
30+
}
31+
}
32+
33+
locals {
34+
backend = templatefile("${path.module}/templates/backend.json", { config : var.s3_backend_config, name : local.namespace })
35+
default_environment = {
36+
TF_IN_AUTOMATION = "1"
37+
TF_INPUT = "1"
38+
WORKING_DIRECTORY = var.working_directory
39+
BACKEND = local.backend,
40+
}
41+
environment = jsonencode([for k, v in merge(local.default_environment, var.environment) : { name : k, value : v, type : "PLAINTEXT" }])
42+
}
43+
44+
resource "aws_s3_bucket" "codepipeline_bucket" {
45+
bucket = "${local.namespace}-codepipeline-bucket"
46+
acl = "private"
47+
force_destroy = true
48+
}
49+
50+
resource "aws_sns_topic" "codepipeline" {
51+
name = "${local.namespace}-pipeline-topic"
52+
}
53+
54+
resource "aws_codepipeline" "codepipeline" {
55+
name = "${local.namespace}-pipeline"
56+
role_arn = aws_iam_role.codepipeline_role.arn
57+
58+
artifact_store {
59+
location = aws_s3_bucket.codepipeline_bucket.bucket
60+
type = "S3"
61+
}
62+
63+
stage {
64+
name = "Source"
65+
66+
action {
67+
name = "Source"
68+
category = "Source"
69+
owner = "ThirdParty"
70+
provider = "GitHub"
71+
version = "1"
72+
output_artifacts = ["source_output"]
73+
74+
configuration = {
75+
Owner = split("/", var.vcs_repo.identifier)[0]
76+
Repo = split("/", var.vcs_repo.identifier)[1]
77+
Branch = var.vcs_repo.branch
78+
OAuthToken = var.vcs_repo.oauth_token
79+
}
80+
}
81+
}
82+
83+
stage {
84+
name = "Plan"
85+
86+
action {
87+
name = "Plan"
88+
category = "Build"
89+
owner = "AWS"
90+
provider = "CodeBuild"
91+
input_artifacts = ["source_output"]
92+
version = "1"
93+
94+
configuration = {
95+
ProjectName = aws_codebuild_project.project[0].name
96+
EnvironmentVariables = local.environment
97+
}
98+
}
99+
}
100+
101+
dynamic "stage" {
102+
for_each = ! var.auto_apply ? [1] : []
103+
content {
104+
name = "Approval"
105+
106+
action {
107+
name = "Approval"
108+
category = "Approval"
109+
owner = "AWS"
110+
provider = "Manual"
111+
version = "1"
112+
113+
configuration = {
114+
CustomData = "Please review output of plan and approve"
115+
NotificationArn = aws_sns_topic.codepipeline.arn
116+
}
117+
}
118+
}
119+
}
120+
121+
stage {
122+
name = "Apply"
123+
124+
action {
125+
name = "Apply"
126+
category = "Build"
127+
owner = "AWS"
128+
provider = "CodeBuild"
129+
input_artifacts = ["source_output"]
130+
version = "1"
131+
132+
configuration = {
133+
ProjectName = aws_codebuild_project.project[1].name
134+
EnvironmentVariables = local.environment
135+
}
136+
}
137+
}
138+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
aws = {
2+
access_key = "AKIATESI2XGPI5F"
3+
secret_key = "x2TZm1kBKfH4Z6P"
4+
region = "us-west-2"
5+
}
6+
7+
vcs_repo = {
8+
branch = "master"
9+
identifier = "swinkler/test_deploy"
10+
oauth_token = "b4c5e88c29a192cde69d150"
11+
}

chapter11/listing11.14/main.tf

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
provider "aws" {
2+
version = "2.12.0"
3+
region = "us-west-2"
4+
}
5+
6+
data "aws_ami" "ubuntu" {
7+
most_recent = true
8+
9+
filter {
10+
name = "name"
11+
values = ["ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-*"]
12+
}
13+
14+
owners = ["099720109477"]
15+
}
16+
17+
resource "aws_instance" "helloworld" {
18+
ami = data.aws_ami.ubuntu.id
19+
instance_type = "t2.micro"
20+
}

chapter11/listing11.15/main.tf

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
variable "aws" {
2+
type = object({ access_key = string, secret_key = string, region = string })
3+
}
4+
5+
variable "vcs_repo" {
6+
type = object({ identifier = string, branch = string, oauth_token = string })
7+
}
8+
9+
provider "aws" {
10+
access_key = var.aws.access_key
11+
secret_key = var.aws.secret_key
12+
region = var.aws.region
13+
}
14+
15+
module "s3backend" {
16+
source = "scottwinkler/s3backend/aws"
17+
}
18+
19+
module "codepipeline" {
20+
source = "./modules/codepipeline"
21+
name = "terraform-in-action"
22+
vcs_repo = var.vcs_repo
23+
auto_apply = true
24+
environment = {
25+
AWS_ACCESS_KEY_ID = var.aws.access_key
26+
AWS_SECRET_ACCESS_KEY = var.aws.secret_key
27+
CONFIRM_DESTROY = 1
28+
}
29+
s3_backend_config = module.s3backend.config
30+
}

0 commit comments

Comments
 (0)