Skip to content

Commit 8c7f266

Browse files
Codestar source output artifact type var (#97)
* Expose output artifact type for Source stage of codestar pipeline * Assign codestar policy to codebuild role * Attach codestar policy to codebuild service role if 'full clone' option is selected * Auto Format * Implement @aknysh suggested changes * Auto Format Co-authored-by: cloudpossebot <[email protected]>
1 parent 7d1f5b2 commit 8c7f266

File tree

5 files changed

+26
-7
lines changed

5 files changed

+26
-7
lines changed

.github/auto-release.yml

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ version-resolver:
1717
- 'bugfix'
1818
- 'bug'
1919
- 'hotfix'
20-
- 'no-release'
2120
default: 'minor'
2221

2322
categories:

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ Available targets:
244244
| [aws_iam_policy.s3](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
245245
| [aws_iam_role.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
246246
| [aws_iam_role_policy_attachment.codebuild](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
247+
| [aws_iam_role_policy_attachment.codebuild_codestar](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
247248
| [aws_iam_role_policy_attachment.codebuild_s3](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
248249
| [aws_iam_role_policy_attachment.codestar](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
249250
| [aws_iam_role_policy_attachment.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
@@ -275,6 +276,7 @@ Available targets:
275276
| <a name="input_cache_type"></a> [cache\_type](#input\_cache\_type) | The type of storage that will be used for the AWS CodeBuild project cache. Valid values: NO\_CACHE, LOCAL, and S3. Defaults to S3. If cache\_type is S3, it will create an S3 bucket for storing codebuild cache inside | `string` | `"S3"` | no |
276277
| <a name="input_codebuild_vpc_config"></a> [codebuild\_vpc\_config](#input\_codebuild\_vpc\_config) | Configuration for the builds to run inside a VPC. | `any` | `{}` | no |
277278
| <a name="input_codestar_connection_arn"></a> [codestar\_connection\_arn](#input\_codestar\_connection\_arn) | CodeStar connection ARN required for Bitbucket integration with CodePipeline | `string` | `""` | no |
279+
| <a name="input_codestar_output_artifact_format"></a> [codestar\_output\_artifact\_format](#input\_codestar\_output\_artifact\_format) | Output artifact type for Source stage in pipeline. Valid values are "CODE\_ZIP" (default) and "CODEBUILD\_CLONE\_REF". See https://docs.aws.amazon.com/codepipeline/latest/userguide/action-reference-CodestarConnectionSource.html | `string` | `"CODE_ZIP"` | no |
278280
| <a name="input_context"></a> [context](#input\_context) | Single object for setting entire context at once.<br>See description of individual variables for details.<br>Leave string and numeric variables as `null` to use default value.<br>Individual variable settings (non-null) override settings in context object,<br>except for attributes, tags, and additional\_tag\_map, which are merged. | `any` | <pre>{<br> "additional_tag_map": {},<br> "attributes": [],<br> "delimiter": null,<br> "descriptor_formats": {},<br> "enabled": true,<br> "environment": null,<br> "id_length_limit": null,<br> "label_key_case": null,<br> "label_order": [],<br> "label_value_case": null,<br> "labels_as_tags": [<br> "unset"<br> ],<br> "name": null,<br> "namespace": null,<br> "regex_replace_chars": null,<br> "stage": null,<br> "tags": {},<br> "tenant": null<br>}</pre> | no |
279281
| <a name="input_delimiter"></a> [delimiter](#input\_delimiter) | Delimiter to be used between ID elements.<br>Defaults to `-` (hyphen). Set to `""` to use no delimiter at all. | `string` | `null` | no |
280282
| <a name="input_descriptor_formats"></a> [descriptor\_formats](#input\_descriptor\_formats) | Describe additional descriptors to be output in the `descriptors` output map.<br>Map of maps. Keys are names of descriptors. Values are maps of the form<br>`{<br> format = string<br> labels = list(string)<br>}`<br>(Type is `any` so the map values can later be enhanced to provide additional options.)<br>`format` is a Terraform format string to be passed to the `format()` function.<br>`labels` is a list of labels, in order, to pass to `format()` function.<br>Label values will be normalized before being passed to `format()` so they will be<br>identical to how they appear in `id`.<br>Default is `{}` (`descriptors` output will be empty). | `any` | `{}` | no |

docs/terraform.md

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
| [aws_iam_policy.s3](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
4141
| [aws_iam_role.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
4242
| [aws_iam_role_policy_attachment.codebuild](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
43+
| [aws_iam_role_policy_attachment.codebuild_codestar](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
4344
| [aws_iam_role_policy_attachment.codebuild_s3](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
4445
| [aws_iam_role_policy_attachment.codestar](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
4546
| [aws_iam_role_policy_attachment.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
@@ -71,6 +72,7 @@
7172
| <a name="input_cache_type"></a> [cache\_type](#input\_cache\_type) | The type of storage that will be used for the AWS CodeBuild project cache. Valid values: NO\_CACHE, LOCAL, and S3. Defaults to S3. If cache\_type is S3, it will create an S3 bucket for storing codebuild cache inside | `string` | `"S3"` | no |
7273
| <a name="input_codebuild_vpc_config"></a> [codebuild\_vpc\_config](#input\_codebuild\_vpc\_config) | Configuration for the builds to run inside a VPC. | `any` | `{}` | no |
7374
| <a name="input_codestar_connection_arn"></a> [codestar\_connection\_arn](#input\_codestar\_connection\_arn) | CodeStar connection ARN required for Bitbucket integration with CodePipeline | `string` | `""` | no |
75+
| <a name="input_codestar_output_artifact_format"></a> [codestar\_output\_artifact\_format](#input\_codestar\_output\_artifact\_format) | Output artifact type for Source stage in pipeline. Valid values are "CODE\_ZIP" (default) and "CODEBUILD\_CLONE\_REF". See https://docs.aws.amazon.com/codepipeline/latest/userguide/action-reference-CodestarConnectionSource.html | `string` | `"CODE_ZIP"` | no |
7476
| <a name="input_context"></a> [context](#input\_context) | Single object for setting entire context at once.<br>See description of individual variables for details.<br>Leave string and numeric variables as `null` to use default value.<br>Individual variable settings (non-null) override settings in context object,<br>except for attributes, tags, and additional\_tag\_map, which are merged. | `any` | <pre>{<br> "additional_tag_map": {},<br> "attributes": [],<br> "delimiter": null,<br> "descriptor_formats": {},<br> "enabled": true,<br> "environment": null,<br> "id_length_limit": null,<br> "label_key_case": null,<br> "label_order": [],<br> "label_value_case": null,<br> "labels_as_tags": [<br> "unset"<br> ],<br> "name": null,<br> "namespace": null,<br> "regex_replace_chars": null,<br> "stage": null,<br> "tags": {},<br> "tenant": null<br>}</pre> | no |
7577
| <a name="input_delimiter"></a> [delimiter](#input\_delimiter) | Delimiter to be used between ID elements.<br>Defaults to `-` (hyphen). Set to `""` to use no delimiter at all. | `string` | `null` | no |
7678
| <a name="input_descriptor_formats"></a> [descriptor\_formats](#input\_descriptor\_formats) | Describe additional descriptors to be output in the `descriptors` output map.<br>Map of maps. Keys are names of descriptors. Values are maps of the form<br>`{<br> format = string<br> labels = list(string)<br>}`<br>(Type is `any` so the map values can later be enhanced to provide additional options.)<br>`format` is a Terraform format string to be passed to the `format()` function.<br>`labels` is a list of labels, in order, to pass to `format()` function.<br>Label values will be normalized before being passed to `format()` so they will be<br>identical to how they appear in `id`.<br>Default is `{}` (`descriptors` output will be empty). | `any` | `{}` | no |

main.tf

+16-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
locals {
2+
codestar_enabled = module.this.enabled && var.codestar_connection_arn != "" && var.codestar_connection_arn != null
3+
}
4+
15
module "codepipeline_label" {
26
source = "cloudposse/label/null"
37
version = "0.25.0"
@@ -157,28 +161,28 @@ data "aws_iam_policy_document" "codebuild" {
157161

158162
# https://docs.aws.amazon.com/codepipeline/latest/userguide/connections-permissions.html
159163
resource "aws_iam_role_policy_attachment" "codestar" {
160-
count = module.this.enabled && var.codestar_connection_arn != "" ? 1 : 0
164+
count = local.codestar_enabled ? 1 : 0
161165
role = join("", aws_iam_role.default.*.id)
162166
policy_arn = join("", aws_iam_policy.codestar.*.arn)
163167
}
164168

165169
module "codestar_label" {
166170
source = "cloudposse/label/null"
167171
version = "0.25.0"
168-
enabled = module.this.enabled && var.codestar_connection_arn != ""
172+
enabled = local.codestar_enabled
169173
attributes = ["codestar"]
170174

171175
context = module.this.context
172176
}
173177

174178
resource "aws_iam_policy" "codestar" {
175-
count = module.this.enabled && var.codestar_connection_arn != "" ? 1 : 0
179+
count = local.codestar_enabled ? 1 : 0
176180
name = module.codestar_label.id
177181
policy = join("", data.aws_iam_policy_document.codestar.*.json)
178182
}
179183

180184
data "aws_iam_policy_document" "codestar" {
181-
count = module.this.enabled && var.codestar_connection_arn != "" ? 1 : 0
185+
count = local.codestar_enabled ? 1 : 0
182186
statement {
183187
sid = ""
184188

@@ -240,6 +244,12 @@ resource "aws_iam_role_policy_attachment" "codebuild_s3" {
240244
policy_arn = join("", aws_iam_policy.s3.*.arn)
241245
}
242246

247+
resource "aws_iam_role_policy_attachment" "codebuild_codestar" {
248+
count = local.codestar_enabled && var.codestar_output_artifact_format == "CODEBUILD_CLONE_REF" ? 1 : 0
249+
role = module.codebuild.role_id
250+
policy_arn = join("", aws_iam_policy.codestar.*.arn)
251+
}
252+
243253
resource "aws_codepipeline" "default" {
244254
count = module.this.enabled && var.github_oauth_token != "" ? 1 : 0
245255
name = module.codepipeline_label.id
@@ -323,7 +333,7 @@ resource "aws_codepipeline" "default" {
323333

324334
# https://docs.aws.amazon.com/codepipeline/latest/userguide/action-reference-CodestarConnectionSource.html#action-reference-CodestarConnectionSource-example
325335
resource "aws_codepipeline" "bitbucket" {
326-
count = module.this.enabled && var.codestar_connection_arn != "" ? 1 : 0
336+
count = local.codestar_enabled ? 1 : 0
327337
name = module.codepipeline_label.id
328338
role_arn = join("", aws_iam_role.default.*.arn)
329339

@@ -355,7 +365,7 @@ resource "aws_codepipeline" "bitbucket" {
355365
ConnectionArn = var.codestar_connection_arn
356366
FullRepositoryId = format("%s/%s", var.repo_owner, var.repo_name)
357367
BranchName = var.branch
358-
OutputArtifactFormat = "CODE_ZIP"
368+
OutputArtifactFormat = var.codestar_output_artifact_format
359369
}
360370
}
361371
}

variables.tf

+6
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,12 @@ variable "codestar_connection_arn" {
180180
default = ""
181181
}
182182

183+
variable "codestar_output_artifact_format" {
184+
type = string
185+
description = "Output artifact type for Source stage in pipeline. Valid values are \"CODE_ZIP\" (default) and \"CODEBUILD_CLONE_REF\". See https://docs.aws.amazon.com/codepipeline/latest/userguide/action-reference-CodestarConnectionSource.html"
186+
default = "CODE_ZIP"
187+
}
188+
183189
variable "cache_type" {
184190
type = string
185191
default = "S3"

0 commit comments

Comments
 (0)