Skip to content

Commit b3ad7c6

Browse files
Feature/update 1 (#2)
* updated module to provision codepipeline resource alone * updated codebase and README * updated test.ftvars file * removed reference to aws account ID in local.tf * removed aws_caller_identity data block in main.tf of root module
1 parent 28bb533 commit b3ad7c6

File tree

12 files changed

+42
-256
lines changed

12 files changed

+42
-256
lines changed

README.md

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -146,25 +146,13 @@ No modules.
146146

147147
| Name | Type |
148148
|------|------|
149-
| [aws_cloudwatch_event_rule.pipeline_event](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_event_rule) | resource |
150-
| [aws_cloudwatch_event_target.code_pipeline](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_event_target) | resource |
151149
| [aws_codepipeline.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/codepipeline) | resource |
152-
| [aws_iam_policy.pipeline_event_role_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
153150
| [aws_iam_role.codepipeline_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
154-
| [aws_iam_role.pipeline_event_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
155151
| [aws_iam_role_policy.codepipeline_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
156-
| [aws_iam_role_policy_attachment.pipeline_event_role_attach_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
157-
| [aws_s3_bucket.codepipeline_bucket](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket) | resource |
158-
| [aws_s3_bucket.source](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket) | resource |
159-
| [aws_s3_bucket_logging.codepipeline_bucket_logging](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_logging) | resource |
160-
| [aws_s3_bucket_logging.source_bucket_logging](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_logging) | resource |
161-
| [aws_s3_bucket_server_side_encryption_configuration.sse](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_server_side_encryption_configuration) | resource |
162-
| [aws_s3_bucket_versioning.versioning](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_versioning) | resource |
163152
| [random_string.random](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/string) | resource |
164153
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
165154
| [aws_iam_policy_document.assume_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
166155
| [aws_iam_policy_document.codepipeline_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
167-
| [aws_iam_policy_document.event_bridge_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
168156
| [aws_iam_policy_document.pipeline_event_role_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
169157

170158
## Inputs
@@ -174,11 +162,7 @@ No modules.
174162
| <a name="input_name"></a> [name](#input\_name) | The name of the pipeline | `string` | n/a | yes |
175163
| <a name="input_artifact_stores"></a> [artifact\_stores](#input\_artifact\_stores) | One or more artifact\_store blocks. | `list(map(string))` | <pre>[<br> {<br> "use_kms": false<br> }<br>]</pre> | no |
176164
| <a name="input_stages"></a> [stages](#input\_stages) | One or more stage blocks. | `any` | n/a | yes |
177-
| <a name="input_create_s3_source"></a> [create\_s3\_source](#input\_create\_s3\_source) | Create S3 source bucket for CodePipeline. | `string` | `false` | no |
178-
| <a name="input_source_s3_bucket"></a> [source\_s3\_bucket](#input\_source\_s3\_bucket) | Name of the S3 source bucket. | `string` | `null` | no |
179-
| <a name="input_s3_trigger_file"></a> [s3\_trigger\_file](#input\_s3\_trigger\_file) | S3 file that the event rule is looking for to trigger the pipeline. | `string` | `"trigger_pipeline.zip"` | no |
180-
| <a name="input_log_target_bucket"></a> [log\_target\_bucket](#input\_log\_target\_bucket) | Target bucket for S3 Access Logs. If left blank, access logging is disabled. | `string` | `""` | no |
181-
| <a name="input_log_target_prefix"></a> [log\_target\_prefix](#input\_log\_target\_prefix) | Prefix for S3 Access logs. Defaults to AWSLogs/<AWS Account ID>/s3audit if not otherwise provided. | `string` | `""` | no |
165+
| <a name="input_source_s3_bucket"></a> [artifact\_bucket\_name](#input\_source\_s3\_bucket) | Name of the S3 source bucket. | `string` | `null` | no |
182166
| <a name="input_codepipeline_iam"></a> [codepipeline\_iam](#input\_codepipeline\_iam) | Additional IAM actions to add to CodePipeline IAM role. | `map(list(string))` | `null` | no |
183167
| <a name="input_pipeline_type"></a> [pipeline\_type](#input\_pipeline\_type) | The CodePipeline pipeline\_type. Valid options are V1, V2 | `string` | `"V1"` | no |
184168
| <a name="input_execution_mode"></a> [execution\_mode](#input\_execution\_mode) | The CodePipeline execution\_mode. Valid options are `PARALLEL`, `QUEUED`, `SUPERSEDED` (default) | `string` | `"SUPERSEDED"` | no |
@@ -190,5 +174,4 @@ No modules.
190174
|------|-------------|
191175
| <a name="output_id"></a> [id](#output\_id) | The codepipeline ID |
192176
| <a name="output_arn"></a> [arn](#output\_arn) | The codepipeline ARN |
193-
| <a name="output_source_s3_bucket_arn"></a> [source\_s3\_bucket\_arn](#output\_source\_s3\_bucket\_arn) | The codepipeline source s3 bucket ARN |
194177
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

examples/complete/README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ provider "random" {}
4545
| Name | Description | Type | Default | Required |
4646
|------|-------------|------|---------|:--------:|
4747
| <a name="input_name"></a> [name](#input\_name) | The name of the pipeline | `string` | n/a | yes |
48-
| <a name="input_create_s3_source"></a> [create\_s3\_source](#input\_create\_s3\_source) | Create S3 source bucket for CodePipeline. | `string` | `false` | no |
49-
| <a name="input_source_s3_bucket_prefix"></a> [source\_s3\_bucket\_prefix](#input\_source\_s3\_bucket\_prefix) | Prefix of the S3 source bucket. Random characters will be added to the end to ensure uniqueness. | `string` | `null` | no |
5048
| <a name="input_stages"></a> [stages](#input\_stages) | One or more stage blocks. | `any` | n/a | yes |
5149
| <a name="input_pipeline_type"></a> [pipeline\_type](#input\_pipeline\_type) | The CodePipeline pipeline\_type. Valid options are V1, V2 | `string` | `"V2"` | no |
5250
| <a name="input_execution_mode"></a> [execution\_mode](#input\_execution\_mode) | The CodePipeline execution\_mode. Valid options are `PARALLEL`, `QUEUED`, `SUPERSEDED` (default) | `string` | `"SUPERSEDED"` | no |
@@ -58,5 +56,6 @@ provider "random" {}
5856
|------|-------------|
5957
| <a name="output_id"></a> [id](#output\_id) | The codepipeline ID |
6058
| <a name="output_arn"></a> [arn](#output\_arn) | The codepipeline ARN |
61-
| <a name="output_source_s3_bucket_arn"></a> [source\_s3\_bucket\_arn](#output\_source\_s3\_bucket\_arn) | The codepipeline source s3 bucket ARN |
59+
6260
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
61+
c

examples/complete/locals.tf

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,3 @@
1010
// See the License for the specific language governing permissions and
1111
// limitations under the License.
1212

13-
locals {
14-
source_bucket_name = "${var.source_s3_bucket_prefix}-${random_string.random.result}"
15-
}

examples/complete/main.tf

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@ module "codepipeline" {
2020
source = "../.."
2121

2222
name = var.name
23-
24-
create_s3_source = var.create_s3_source
25-
source_s3_bucket = local.source_bucket_name
26-
stages = var.stages
27-
pipeline_type = var.pipeline_type
28-
execution_mode = var.execution_mode
23+
24+
artifact_bucket_name = var.artifact_bucket_name
25+
stages = var.stages
26+
pipeline_type = var.pipeline_type
27+
execution_mode = var.execution_mode
2928

3029
tags = var.tags
3130
}

examples/complete/outputs.tf

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,3 @@ output "arn" {
2020
value = module.codepipeline.arn
2121
}
2222

23-
output "source_s3_bucket_arn" {
24-
description = "The codepipeline source s3 bucket ARN"
25-
value = module.codepipeline.source_s3_bucket_arn
26-
}

examples/complete/test.tfvars

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
name = "tf-aws-module_primitive-codepipeline-test-pipeline"
2-
create_s3_source = true
3-
source_s3_bucket_prefix = "tf-aws-module_primitive-codepipeline-test-bucket"
4-
pipeline_type = "V2"
5-
execution_mode = "PARALLEL"
1+
name = "tf-aws-module_primitive-codepipeline-test-pipeline"
2+
pipeline_type = "V2"
3+
execution_mode = "PARALLEL"
4+
artifact_bucket_name = "osahon-test-020127659860"
65

76

87
stages = [
@@ -14,7 +13,7 @@ stages = [
1413
provider = "S3"
1514
version = "1"
1615
configuration = {
17-
S3Bucket = "aaron-test-bucket-778189110199"
16+
S3Bucket = "osahon-test-020127659860"
1817
S3ObjectKey = "trigger_pipeline.zip"
1918
PollForSourceChanges = "false"
2019
}

examples/complete/variables.tf

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,10 @@ variable "name" {
1515
type = string
1616
}
1717

18-
variable "create_s3_source" {
19-
description = "Create S3 source bucket for CodePipeline."
20-
type = string
21-
default = false
22-
}
23-
24-
variable "source_s3_bucket_prefix" {
25-
description = "Prefix of the S3 source bucket. Random characters will be added to the end to ensure uniqueness."
26-
type = string
27-
default = null
28-
}
29-
3018
variable "stages" {
3119
description = "One or more stage blocks."
3220
type = any
3321
}
34-
3522
variable "pipeline_type" {
3623
description = "The CodePipeline pipeline_type. Valid options are V1, V2"
3724
type = string
@@ -49,6 +36,11 @@ variable "execution_mode" {
4936
}
5037
}
5138

39+
variable "artifact_bucket_name" {
40+
description = "the name of the S3 bucket used for storing the artifacts in the Codepipeline"
41+
type = string
42+
}
43+
5244
variable "tags" {
5345
description = "An arbitrary map of tags that can be added to all resources."
5446
type = map(string)

locals.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ locals {
1414
default_tags = {
1515
provisioner = "Terraform"
1616
}
17-
account_id = data.aws_caller_identity.current.account_id
18-
bucket_prefix = length(var.log_target_prefix) == 0 ? "AWSLogs/${local.account_id}/s3audit" : var.log_target_prefix
19-
17+
2018
tags = merge(local.default_tags, var.tags)
2119
}
20+
21+

main.tf

Lines changed: 15 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
// See the License for the specific language governing permissions and
1111
// limitations under the License.
1212

13-
data "aws_caller_identity" "current" {}
13+
data "aws_s3_bucket" "artifact_bucket" {
14+
bucket = var.artifact_bucket_name
15+
}
1416

15-
### Resources to create CodePipeline
17+
### Resouces to create codepipeline
1618
resource "aws_codepipeline" "this" {
1719
name = var.name
1820
role_arn = aws_iam_role.codepipeline_role.arn
@@ -27,7 +29,7 @@ resource "aws_codepipeline" "this" {
2729
}]
2830

2931
content {
30-
location = aws_s3_bucket.codepipeline_bucket.bucket
32+
location = data.aws_s3_bucket.artifact_bucket.bucket
3133
type = "S3"
3234

3335
dynamic "encryption_key" {
@@ -41,7 +43,7 @@ resource "aws_codepipeline" "this" {
4143
}
4244
}
4345

44-
# Add each stage to the pipeline from the codebuild_stages var.
46+
# Add each stage to the pipeline from the codebuild_stage var.
4547
dynamic "stage" {
4648
for_each = [for stage_val in var.stages : {
4749
stage_name = try(stage_val.stage_name, "My-Stage")
@@ -79,28 +81,7 @@ resource "aws_codepipeline" "this" {
7981
tags = local.tags
8082
}
8183

82-
# CodePipeline bucket used to store Output Artifacts
83-
resource "aws_s3_bucket" "codepipeline_bucket" {
84-
bucket = join("-", ["codepipeline", random_string.random.result])
85-
force_destroy = true
86-
}
87-
88-
resource "aws_s3_bucket_logging" "codepipeline_bucket_logging" {
89-
count = length(var.log_target_bucket) > 0 ? 1 : 0
90-
91-
bucket = aws_s3_bucket.codepipeline_bucket.id
92-
93-
target_bucket = var.log_target_bucket
94-
target_prefix = local.bucket_prefix
95-
}
96-
97-
resource "random_string" "random" {
98-
length = 16
99-
special = false
100-
upper = false
101-
}
102-
103-
# CodePipeline Role
84+
#CodePipeline Role
10485
data "aws_iam_policy_document" "assume_role" {
10586
statement {
10687
effect = "Allow"
@@ -121,8 +102,7 @@ resource "aws_iam_role" "codepipeline_role" {
121102

122103
data "aws_iam_policy_document" "codepipeline_policy" {
123104

124-
# Eventbridge trigger
125-
statement {
105+
statement {
126106
effect = "Allow"
127107
actions = [
128108
"cloudwatch:*",
@@ -150,142 +130,19 @@ data "aws_iam_policy_document" "codepipeline_policy" {
150130
"s3:*"
151131
]
152132
resources = [
153-
aws_s3_bucket.codepipeline_bucket.arn,
154-
"${aws_s3_bucket.codepipeline_bucket.arn}/*",
133+
data.aws_s3_bucket.artifact_bucket.arn,
134+
"${data.aws_s3_bucket.artifact_bucket.arn}/*",
155135
]
156136
}
157-
158-
# Allow the ability to access source bucket
159-
dynamic "statement" {
160-
for_each = var.create_s3_source ? [1] : []
161-
content {
162-
effect = "Allow"
163-
actions = [
164-
"s3:GetObject",
165-
"s3:GetObjectVersion",
166-
"s3:GetBucketVersioning",
167-
"s3:PutObjectAcl",
168-
"s3:PutObject",
169-
]
170-
resources = [
171-
aws_s3_bucket.source[0].arn,
172-
"${aws_s3_bucket.source[0].arn}/*",
173-
]
174-
}
175-
}
176-
177-
# Add additional policy statements if any
178-
dynamic "statement" {
179-
for_each = var.codepipeline_iam != null ? [1] : []
180-
content {
181-
effect = "Allow"
182-
actions = var.codepipeline_iam.actions
183-
resources = var.codepipeline_iam.resources
184-
}
185-
}
186137
}
187-
188138
resource "aws_iam_role_policy" "codepipeline_policy" {
189139
name = "codepipeline_policy"
190140
role = aws_iam_role.codepipeline_role.id
191141
policy = data.aws_iam_policy_document.codepipeline_policy.json
192142
}
193143

194-
### Resources to create an S3 Event Bridge trigger
195-
resource "aws_s3_bucket" "source" {
196-
count = var.create_s3_source ? 1 : 0
197-
198-
bucket = replace(var.source_s3_bucket, "_", "-")
199-
force_destroy = true
200-
}
201-
202-
resource "aws_s3_bucket_logging" "source_bucket_logging" {
203-
count = length(var.log_target_bucket) > 0 && var.create_s3_source ? 1 : 0
204-
205-
bucket = aws_s3_bucket.source[0].id
206-
207-
target_bucket = var.log_target_bucket
208-
target_prefix = local.bucket_prefix
209-
}
210-
211-
resource "aws_s3_bucket_versioning" "versioning" {
212-
bucket = aws_s3_bucket.source[0].id
213-
versioning_configuration {
214-
status = "Enabled"
215-
}
216-
}
217-
218-
resource "aws_s3_bucket_server_side_encryption_configuration" "sse" {
219-
bucket = aws_s3_bucket.source[0].bucket
220-
221-
rule {
222-
bucket_key_enabled = true
223-
apply_server_side_encryption_by_default {
224-
sse_algorithm = "AES256"
225-
}
226-
}
227-
}
228-
229-
resource "aws_cloudwatch_event_rule" "pipeline_event" {
230-
count = aws_s3_bucket.source != null ? 1 : 0
231-
name = substr("${var.name}-event", 0, 63)
232-
description = "Cloud watch event when zip is uploaded to s3"
233-
234-
event_pattern = <<EOF
235-
{
236-
"source": ["aws.s3"],
237-
"detail-type": ["AWS API Call via CloudTrail"],
238-
"detail": {
239-
"eventSource": ["s3.amazonaws.com"],
240-
"eventName": ["PutObject", "CompleteMultipartUpload", "CopyObject"],
241-
"requestParameters": {
242-
"bucketName": ["${aws_s3_bucket.source[0].id}"],
243-
"key": ["${var.s3_trigger_file}"]
244-
}
245-
}
246-
}
247-
EOF
248-
}
249-
250-
resource "aws_cloudwatch_event_target" "code_pipeline" {
251-
count = aws_s3_bucket.source != null ? 1 : 0
252-
rule = aws_cloudwatch_event_rule.pipeline_event[0].name
253-
target_id = "SendToCodePipeline"
254-
arn = aws_codepipeline.this.arn
255-
role_arn = aws_iam_role.pipeline_event_role.arn
256-
}
257-
258-
data "aws_iam_policy_document" "event_bridge_role" {
259-
count = aws_s3_bucket.source != null ? 1 : 0
260-
statement {
261-
actions = ["sts:AssumeRole"]
262-
effect = "Allow"
263-
principals {
264-
type = "Service"
265-
identifiers = ["events.amazonaws.com"]
266-
}
267-
}
268-
}
269-
270-
resource "aws_iam_role" "pipeline_event_role" {
271-
name = substr("${var.name}-event-bridge-role", 0, 63)
272-
assume_role_policy = data.aws_iam_policy_document.event_bridge_role[0].json
273-
}
274-
275-
data "aws_iam_policy_document" "pipeline_event_role_policy" {
276-
statement {
277-
actions = ["codepipeline:StartPipelineExecution"]
278-
resources = [aws_codepipeline.this.arn]
279-
effect = "Allow"
280-
}
281-
}
282-
283-
resource "aws_iam_policy" "pipeline_event_role_policy" {
284-
name = "${var.name}-event-role-policy"
285-
policy = data.aws_iam_policy_document.pipeline_event_role_policy.json
286-
}
287-
288-
resource "aws_iam_role_policy_attachment" "pipeline_event_role_attach_policy" {
289-
role = aws_iam_role.pipeline_event_role.name
290-
policy_arn = aws_iam_policy.pipeline_event_role_policy.arn
291-
}
144+
resource "random_string" "random" {
145+
length = 10
146+
special = false
147+
upper = false
148+
}

outputs.tf

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,3 @@ output "arn" {
2020
value = aws_codepipeline.this.arn
2121
}
2222

23-
output "source_s3_bucket_arn" {
24-
description = "The codepipeline source s3 bucket ARN"
25-
value = aws_s3_bucket.source[0].arn
26-
}

0 commit comments

Comments
 (0)