Skip to content

Commit 978b508

Browse files
committed
Initial Terraform Files
1 parent 2e58bd4 commit 978b508

7 files changed

+310
-0
lines changed

terraform/.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Terraform Local State
2+
**/.terraform
3+
**/terraform.tfstate
4+
**/terraform.tfstate.backup

terraform/backend/.terraform.lock.hcl

+25
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

terraform/backend/bucket_codebuild.tf

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Bucket for backend state.
3+
*/
4+
resource "aws_s3_bucket" "terraform_state_codebuild" {
5+
bucket = "web-dub-aws-infrastructure-state-codebuild"
6+
7+
/*
8+
lifecycle {
9+
prevent_destroy = true
10+
}
11+
*/
12+
}
13+
14+
resource "aws_s3_bucket_versioning" "terraform_state_codebuild" {
15+
bucket = aws_s3_bucket.terraform_state_codebuild.id
16+
17+
versioning_configuration {
18+
status = "Enabled"
19+
}
20+
}
21+
22+
resource "aws_kms_key" "terraform_state_codebuild" {
23+
description = "Key for bucket web-dub-aws-infrastructure-state-codebuild"
24+
deletion_window_in_days = 10
25+
}
26+
27+
resource "aws_s3_bucket_server_side_encryption_configuration" "terraform_state_codebuild" {
28+
bucket = aws_s3_bucket.terraform_state_codebuild.id
29+
30+
rule {
31+
apply_server_side_encryption_by_default {
32+
kms_master_key_id = aws_kms_key.terraform_state_codebuild.arn
33+
sse_algorithm = "aws:kms"
34+
}
35+
}
36+
}

terraform/backend/terraform.tf

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Pin specific versions.
3+
*/
4+
terraform {
5+
required_providers {
6+
aws = {
7+
source = "hashicorp/aws"
8+
version = "5.38.0"
9+
}
10+
}
11+
12+
required_version = "1.7.4"
13+
}
14+
15+
/*
16+
* Configure AWS profile.
17+
*/
18+
provider "aws" {
19+
profile = "probe"
20+
}
21+
22+
/*
23+
* Table for backend locking.
24+
*/
25+
resource "aws_dynamodb_table" "terraform_state_lock" {
26+
name = "web-dub-aws-infrastructure-state-lock"
27+
read_capacity = 1
28+
write_capacity = 1
29+
hash_key = "LockID"
30+
31+
attribute {
32+
name = "LockID"
33+
type = "S"
34+
}
35+
}

terraform/codebuild/.terraform.lock.hcl

+25
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

terraform/codebuild/terraform.tf

+172
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
/*
2+
* Pin specific versions.
3+
*/
4+
terraform {
5+
required_providers {
6+
aws = {
7+
source = "hashicorp/aws"
8+
version = "5.38.0"
9+
}
10+
}
11+
12+
backend "s3" {
13+
profile = "probe"
14+
region = "us-east-1"
15+
16+
bucket = "web-dub-aws-infrastructure-state-codebuild"
17+
key = "state/terraform.tfstate"
18+
dynamodb_table = "web-dub-aws-infrastructure-state-lock"
19+
}
20+
21+
required_version = "1.7.4"
22+
}
23+
24+
/*
25+
* Configure AWS profile.
26+
*/
27+
provider "aws" {
28+
profile = "probe"
29+
}
30+
31+
/*
32+
* S3 bucket in which to place archive of source.
33+
*/
34+
resource "aws_s3_bucket" "codebuild_source_bucket" {
35+
}
36+
37+
/*
38+
* S3 upload of source.
39+
*/
40+
resource "aws_s3_object" "codebuild_source_object" {
41+
bucket = aws_s3_bucket.codebuild_source_bucket.id
42+
key = "${var.name}.zip"
43+
source = var.source_archive
44+
45+
# etag triggers upload when file changes
46+
etag = filemd5(var.source_archive)
47+
}
48+
49+
/*
50+
* Policy document for assuming the defined role.
51+
*/
52+
data "aws_iam_policy_document" "policy_document_assume" {
53+
statement {
54+
actions = [
55+
"sts:AssumeRole",
56+
]
57+
58+
principals {
59+
type = "Service"
60+
identifiers = [
61+
"codebuild.amazonaws.com",
62+
]
63+
}
64+
}
65+
}
66+
67+
/*
68+
* Policy document for the CodeBuild role.
69+
*/
70+
data "aws_iam_policy_document" "policy_document_codebuild" {
71+
# CodeBuild policy for permissive access to logging
72+
statement {
73+
actions = [
74+
"logs:CreateLogGroup",
75+
"logs:CreateLogStream",
76+
"logs:PutLogEvents",
77+
]
78+
79+
resources = [
80+
"*"
81+
]
82+
}
83+
84+
# CodeBuild policy for permissive access to S3
85+
statement {
86+
actions = [
87+
"s3:GetBucketAcl",
88+
"s3:GetBucketLocation",
89+
"s3:GetObject",
90+
"s3:GetObjectVersion",
91+
"s3:PutObject",
92+
]
93+
94+
resources = [
95+
"*"
96+
]
97+
}
98+
99+
# CodeBuild policy for permissive access to ECR
100+
statement {
101+
actions = [
102+
"ecr:BatchCheckLayerAvailability",
103+
"ecr:CompleteLayerUpload",
104+
"ecr:GetAuthorizationToken",
105+
"ecr:InitiateLayerUpload",
106+
"ecr:PutImage",
107+
"ecr:UploadLayerPart",
108+
]
109+
110+
resources = [
111+
"*"
112+
]
113+
}
114+
}
115+
116+
/*
117+
* Policy for the CodeBuild role.
118+
*/
119+
resource "aws_iam_policy" "policy_codebuild" {
120+
policy = data.aws_iam_policy_document.policy_document_codebuild.json
121+
}
122+
123+
/*
124+
* Role that defines access policies for project.
125+
*/
126+
resource "aws_iam_role" "codebuild_project_role" {
127+
name = "codebuild_role_${var.name}"
128+
129+
assume_role_policy = data.aws_iam_policy_document.policy_document_assume.json
130+
managed_policy_arns = [
131+
aws_iam_policy.policy_codebuild.arn,
132+
]
133+
}
134+
135+
/*
136+
* Group for logs.
137+
*/
138+
resource "aws_cloudwatch_log_group" "logs" {
139+
name = "/aws/codebuild/${var.name}"
140+
}
141+
142+
/*
143+
* Codebuild project.
144+
*/
145+
resource "aws_codebuild_project" "codebuild_project" {
146+
name = var.name
147+
148+
service_role = aws_iam_role.codebuild_project_role.arn
149+
150+
artifacts {
151+
type = "NO_ARTIFACTS"
152+
}
153+
154+
environment {
155+
compute_type = "BUILD_GENERAL1_SMALL"
156+
type = "LINUX_CONTAINER"
157+
image = "aws/codebuild/standard:5.0"
158+
159+
privileged_mode = true
160+
}
161+
162+
source {
163+
type = "S3"
164+
location = "${aws_s3_object.codebuild_source_object.bucket}/${aws_s3_object.codebuild_source_object.key}"
165+
}
166+
167+
logs_config {
168+
cloudwatch_logs {
169+
group_name = aws_cloudwatch_log_group.logs.name
170+
}
171+
}
172+
}

terraform/codebuild/variables.tf

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Name of CodeBuild project.
3+
*/
4+
variable "name" {
5+
type = string
6+
}
7+
8+
/*
9+
* Path to source archive to upload for CodeBuild.
10+
*/
11+
variable "source_archive" {
12+
type = string
13+
}

0 commit comments

Comments
 (0)