Skip to content

Commit e852049

Browse files
committed
Refactor to use API instead of Cloudformation
1 parent ca86e70 commit e852049

File tree

6 files changed

+53
-68
lines changed

6 files changed

+53
-68
lines changed

.pre-commit-config.yaml

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
repos:
22
- repo: https://github.com/antonbabenko/pre-commit-terraform
3-
rev: v1.31.0
3+
rev: v1.77.0
44
hooks:
55
- id: terraform_docs
66
always_run: true
7-
args:
8-
- --args=--sort-by-required
97
- id: terraform_fmt
108
- id: terraform_tflint
119
alias: terraform_tflint_deep

README.md

+32-20
Original file line numberDiff line numberDiff line change
@@ -96,41 +96,53 @@ This module bridges the gap allowing Terraform to create EC2 Image Builder compo
9696

9797
| Name | Version |
9898
|------|---------|
99-
| terraform | >= 0.12.28 |
100-
| aws | >= 2.44, < 4.0.0 |
99+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13 |
100+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 4.22.0 |
101101

102102
## Providers
103103

104104
| Name | Version |
105105
|------|---------|
106-
| aws | >= 2.44, < 4.0.0 |
106+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 4.22.0 |
107+
108+
## Modules
109+
110+
No modules.
111+
112+
## Resources
113+
114+
| Name | Type |
115+
|------|------|
116+
| [aws_imagebuilder_component.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/imagebuilder_component) | resource |
117+
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
118+
| [aws_region.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |
119+
| [aws_secretsmanager_secret.ssh_key](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/secretsmanager_secret) | data source |
107120

108121
## Inputs
109122

110123
| Name | Description | Type | Default | Required |
111124
|------|-------------|------|---------|:--------:|
112-
| component\_version | Version of the component | `string` | n/a | yes |
113-
| name | name to use for component | `string` | n/a | yes |
114-
| playbook\_repo | git url for repo where ansible code lives with provisioning playbook and requirements file<br>can append with `-b BRANCH_NAME` to clone a specific branch | `string` | n/a | yes |
115-
| change\_description | description of changes since last version | `string` | `null` | no |
116-
| cloudformation\_timeout | How long to wait (in minutes) for CFN to apply before giving up | `number` | `10` | no |
117-
| data\_uri | Use this to override the component document with one at a particualar URL endpoint | `string` | `null` | no |
118-
| description | description of component | `string` | `null` | no |
119-
| kms\_key\_id | KMS key to use for encryption | `string` | `null` | no |
120-
| platform | platform of component (Linux or Windows) | `string` | `"Linux"` | no |
121-
| playbook\_dir | directory where playbook and requirements are found (if not root of repo) | `string` | `null` | no |
122-
| playbook\_file | path to playbook file, relative to `playbook_dir` | `string` | `"provision.yml"` | no |
123-
| ssh\_key\_secret\_arn | ARN of a secretsmanager secret containing an SSH key (use arn OR name, not both) | `string` | `null` | no |
124-
| ssh\_key\_secret\_name | Name of a secretsmanager secret containing an SSH key (use arn OR name, not both) | `string` | `null` | no |
125-
| tags | map of tags to use for CFN stack and component | `map(string)` | `{}` | no |
125+
| <a name="input_change_description"></a> [change\_description](#input\_change\_description) | description of changes since last version | `string` | `null` | no |
126+
| <a name="input_component_version"></a> [component\_version](#input\_component\_version) | Version of the component | `string` | n/a | yes |
127+
| <a name="input_data_uri"></a> [data\_uri](#input\_data\_uri) | Use this to override the component document with one at a particualar URL endpoint | `string` | `null` | no |
128+
| <a name="input_description"></a> [description](#input\_description) | description of component | `string` | `null` | no |
129+
| <a name="input_kms_key_id"></a> [kms\_key\_id](#input\_kms\_key\_id) | KMS key to use for encryption | `string` | `null` | no |
130+
| <a name="input_name"></a> [name](#input\_name) | name to use for component | `string` | n/a | yes |
131+
| <a name="input_platform"></a> [platform](#input\_platform) | platform of component (Linux or Windows) | `string` | `"Linux"` | no |
132+
| <a name="input_playbook_dir"></a> [playbook\_dir](#input\_playbook\_dir) | directory where playbook and requirements are found (if not root of repo) | `string` | `null` | no |
133+
| <a name="input_playbook_file"></a> [playbook\_file](#input\_playbook\_file) | path to playbook file, relative to `playbook_dir` | `string` | `"provision.yml"` | no |
134+
| <a name="input_playbook_repo"></a> [playbook\_repo](#input\_playbook\_repo) | git url for repo where ansible code lives with provisioning playbook and requirements file<br>can append with `-b BRANCH_NAME` to clone a specific branch | `string` | n/a | yes |
135+
| <a name="input_ssh_key_secret_arn"></a> [ssh\_key\_secret\_arn](#input\_ssh\_key\_secret\_arn) | ARN of a secretsmanager secret containing an SSH key (use arn OR name, not both) | `string` | `null` | no |
136+
| <a name="input_ssh_key_secret_name"></a> [ssh\_key\_secret\_name](#input\_ssh\_key\_secret\_name) | Name of a secretsmanager secret containing an SSH key (use arn OR name, not both) | `string` | `null` | no |
137+
| <a name="input_supported_os_versions"></a> [supported\_os\_versions](#input\_supported\_os\_versions) | A set of operating system versions supported by the component. If the OS information is available, a prefix match is performed against the base image OS version during image recipe creation. | `set(string)` | `null` | no |
138+
| <a name="input_tags"></a> [tags](#input\_tags) | map of tags to use for CFN stack and component | `map(string)` | `{}` | no |
126139

127140
## Outputs
128141

129142
| Name | Description |
130143
|------|-------------|
131-
| component\_arn | ARN of the EC2 Image Builder Component |
132-
| latest\_minor\_version\_arn | ARN of the EC2 Image Builder Component |
133-
144+
| <a name="output_component_arn"></a> [component\_arn](#output\_component\_arn) | ARN of the EC2 Image Builder Component |
145+
| <a name="output_latest_minor_version_arn"></a> [latest\_minor\_version\_arn](#output\_latest\_minor\_version\_arn) | ARN of the EC2 Image Builder Component |
134146
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
135147

136148
## The Giants underneath this module

main.tf

+11-28
Original file line numberDiff line numberDiff line change
@@ -29,37 +29,20 @@ data "aws_secretsmanager_secret" "ssh_key" {
2929
name = var.ssh_key_secret_name
3030
}
3131

32-
resource "aws_cloudformation_stack" "this" {
33-
name = "${var.name}-${uuid()}"
34-
on_failure = "ROLLBACK"
35-
timeout_in_minutes = var.cloudformation_timeout
32+
resource "aws_imagebuilder_component" "this" {
33+
name = var.name
34+
version = var.component_version
35+
36+
change_description = var.change_description
37+
data = var.data_uri == null ? local.data : null
38+
description = var.description
39+
kms_key_id = var.kms_key_id
40+
platform = var.platform
41+
supported_os_versions = var.supported_os_versions
42+
uri = var.data_uri
3643

3744
tags = merge(
3845
var.tags,
3946
{ Name : "${var.name}-stack" }
4047
)
41-
42-
template_body = templatefile("${path.module}/cloudformation.yml.tpl", {
43-
change_description = var.change_description
44-
data = local.data
45-
description = var.description
46-
kms_key_id = var.kms_key_id
47-
name = var.name
48-
platform = var.platform
49-
uri = var.data_uri
50-
version = var.component_version
51-
52-
tags = merge(
53-
var.tags,
54-
{ Name : var.name }
55-
)
56-
})
57-
58-
lifecycle {
59-
create_before_destroy = true
60-
61-
ignore_changes = [
62-
name
63-
]
64-
}
6548
}

outputs.tf

+1-9
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,10 @@ locals {
88

99
output "component_arn" {
1010
description = "ARN of the EC2 Image Builder Component"
11-
value = "arn:aws:imagebuilder:${local.region}:${local.account_id}:component/${lower(var.name)}/${var.component_version}/1"
12-
13-
depends_on = [
14-
aws_cloudformation_stack.this
15-
]
11+
value = aws_imagebuilder_component.this.arn
1612
}
1713

1814
output "latest_minor_version_arn" {
1915
description = "ARN of the EC2 Image Builder Component"
2016
value = "arn:aws:imagebuilder:${local.region}:${local.account_id}:component/${lower(var.name)}/${local.latest_component_minor_version}"
21-
22-
depends_on = [
23-
aws_cloudformation_stack.this
24-
]
2517
}

variables.tf

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ variable "change_description" {
44
type = string
55
}
66

7-
variable "cloudformation_timeout" {
8-
default = 10
9-
description = "How long to wait (in minutes) for CFN to apply before giving up"
10-
type = number
11-
}
12-
137
variable "component_version" {
148
description = "Version of the component"
159
type = string
@@ -77,6 +71,12 @@ variable "ssh_key_secret_name" {
7771
type = string
7872
}
7973

74+
variable "supported_os_versions" {
75+
default = null
76+
description = "A set of operating system versions supported by the component. If the OS information is available, a prefix match is performed against the base image OS version during image recipe creation."
77+
type = set(string)
78+
}
79+
8080
variable "tags" {
8181
default = {}
8282
description = "map of tags to use for CFN stack and component"

versions.tf

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
terraform {
2-
required_version = ">= 0.12.28"
2+
required_version = ">= 0.14"
33

44
required_providers {
55
aws = {
66
source = "hashicorp/aws",
7-
version = ">= 2.44, < 4.0.0"
7+
version = ">= 4.22.0"
88
}
99
}
1010
}

0 commit comments

Comments
 (0)