Skip to content

Commit 94ca7f9

Browse files
committed
ENG-1058: init
1 parent baa8ef6 commit 94ca7f9

9 files changed

Lines changed: 283 additions & 86 deletions

File tree

README.md

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,71 @@
1-
# terraform-terraform-template
1+
# terraform-aws-component-ansible-setup
22
Template repository for terraform modules. Good for any cloud and any provider.
33

4-
[![tflint](https://github.com/rhythmictech/terraform-terraform-template/workflows/tflint/badge.svg?branch=master&event=push)](https://github.com/rhythmictech/terraform-terraform-template/actions?query=workflow%3Atflint+event%3Apush+branch%3Amaster)
5-
[![tfsec](https://github.com/rhythmictech/terraform-terraform-template/workflows/tfsec/badge.svg?branch=master&event=push)](https://github.com/rhythmictech/terraform-terraform-template/actions?query=workflow%3Atfsec+event%3Apush+branch%3Amaster)
6-
[![yamllint](https://github.com/rhythmictech/terraform-terraform-template/workflows/yamllint/badge.svg?branch=master&event=push)](https://github.com/rhythmictech/terraform-terraform-template/actions?query=workflow%3Ayamllint+event%3Apush+branch%3Amaster)
7-
[![misspell](https://github.com/rhythmictech/terraform-terraform-template/workflows/misspell/badge.svg?branch=master&event=push)](https://github.com/rhythmictech/terraform-terraform-template/actions?query=workflow%3Amisspell+event%3Apush+branch%3Amaster)
8-
[![pre-commit-check](https://github.com/rhythmictech/terraform-terraform-template/workflows/pre-commit-check/badge.svg?branch=master&event=push)](https://github.com/rhythmictech/terraform-terraform-template/actions?query=workflow%3Apre-commit-check+event%3Apush+branch%3Amaster)
4+
[![tflint](https://github.com/rhythmictech/terraform-aws-component-ansible-setup/workflows/tflint/badge.svg?branch=master&event=push)](https://github.com/rhythmictech/terraform-aws-component-ansible-setup/actions?query=workflow%3Atflint+event%3Apush+branch%3Amaster)
5+
[![tfsec](https://github.com/rhythmictech/terraform-aws-component-ansible-setup/workflows/tfsec/badge.svg?branch=master&event=push)](https://github.com/rhythmictech/terraform-aws-component-ansible-setup/actions?query=workflow%3Atfsec+event%3Apush+branch%3Amaster)
6+
[![yamllint](https://github.com/rhythmictech/terraform-aws-component-ansible-setup/workflows/yamllint/badge.svg?branch=master&event=push)](https://github.com/rhythmictech/terraform-aws-component-ansible-setup/actions?query=workflow%3Ayamllint+event%3Apush+branch%3Amaster)
7+
[![misspell](https://github.com/rhythmictech/terraform-aws-component-ansible-setup/workflows/misspell/badge.svg?branch=master&event=push)](https://github.com/rhythmictech/terraform-aws-component-ansible-setup/actions?query=workflow%3Amisspell+event%3Apush+branch%3Amaster)
8+
[![pre-commit-check](https://github.com/rhythmictech/terraform-aws-component-ansible-setup/workflows/pre-commit-check/badge.svg?branch=master&event=push)](https://github.com/rhythmictech/terraform-aws-component-ansible-setup/actions?query=workflow%3Apre-commit-check+event%3Apush+branch%3Amaster)
99
<a href="https://twitter.com/intent/follow?screen_name=RhythmicTech"><img src="https://img.shields.io/twitter/follow/RhythmicTech?style=social&logo=twitter" alt="follow on Twitter"></a>
1010

11+
Terraform module that creates an EC2 Image Builder component with CloudFormation which installs Ansible on Amazon Linux 2
12+
1113
## Example
12-
Here's what using the module will look like
1314
```hcl
14-
module "example" {
15-
source = "rhythmictech/terraform-mycloud-mymodule
15+
module "test_component" {
16+
source = "rhythmictech/imagebuilder-component-ansible-setup/aws"
17+
version = "~> 1.0.0-rc1"
18+
19+
component_version = "1.0.0"
20+
description = "Testing component"
21+
name = "testing-component"
22+
tags = local.tags
1623
}
1724
```
1825

1926
## About
20-
A bit about this module
27+
This module bridges the gap allowing Terraform to create an EC2 Image Builder component which installs Ansible on Amazon Linux 2 until native support for Image Builder is added to Terraform
2128

2229
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
2330
## Requirements
2431

2532
| Name | Version |
2633
|------|---------|
27-
| terraform | >= 0.12.14 |
34+
| terraform | >= 0.12.28 |
35+
| aws | >= 2.44, < 4.0.0 |
2836

2937
## Providers
3038

31-
No provider.
39+
| Name | Version |
40+
|------|---------|
41+
| aws | >= 2.44, < 4.0.0 |
3242

3343
## Inputs
3444

3545
| Name | Description | Type | Default | Required |
3646
|------|-------------|------|---------|:--------:|
37-
| name | Moniker to apply to all resources in the module | `string` | n/a | yes |
38-
| tags | User-Defined tags | `map(string)` | `{}` | no |
47+
| component\_version | Version of the component | `string` | n/a | yes |
48+
| name | name to use for component | `string` | n/a | yes |
49+
| change\_description | description of changes since last version | `string` | `null` | no |
50+
| cloudformation\_timeout | How long to wait (in minutes) for CFN to apply before giving up | `number` | `10` | no |
51+
| data\_uri | Use this to override the component document with one at a particualar URL endpoint | `string` | `null` | no |
52+
| description | description of component | `string` | `null` | no |
53+
| kms\_key\_id | KMS key to use for encryption | `string` | `null` | no |
54+
| platform | platform of component (Linux or Windows) | `string` | `"Linux"` | no |
55+
| ssh\_key\_secret\_arn | ARN of a secretsmanager secret containing an SSH key (use arn OR name, not both) | `string` | `null` | no |
56+
| ssh\_key\_secret\_name | Name of a secretsmanager secret containing an SSH key (use arn OR name, not both) | `string` | `null` | no |
57+
| tags | map of tags to use for CFN stack and component | `map(string)` | `{}` | no |
3958

4059
## Outputs
4160

4261
| Name | Description |
4362
|------|-------------|
44-
| tags\_module | Tags Module in it's entirety |
63+
| component\_arn | ARN of the EC2 Image Builder Component |
4564

4665
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
4766

48-
## The Giants Underneath this Module
49-
- [pre-commit.com](pre-commit.com)
50-
- [terraform.io](terraform.io)
51-
- [github.com/tfutils/tfenv](github.com/tfutils/tfenv)
52-
- [github.com/segmentio/terraform-docs](github.com/segmentio/terraform-docs)
67+
## The Giants underneath this module
68+
- pre-commit.com/
69+
- terraform.io/
70+
- github.com/tfutils/tfenv
71+
- github.com/segmentio/terraform-docs

cloudformation.yml.tpl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
Resources:
2+
ImageBuildComponent:
3+
Type: AWS::ImageBuilder::Component
4+
# Retaining each component when updated because the old component can't be removed until the recipe is updated
5+
UpdateReplacePolicy: Retain
6+
Properties:
7+
Name: ${name}
8+
Version: ${version}
9+
%{~ if change_description != null ~}
10+
ChangeDescription: ${change_description}
11+
%{~ endif ~}
12+
%{~ if description != null ~}
13+
Description: ${description}
14+
%{~ endif ~}
15+
%{~ if kms_key_id != null ~}
16+
KmsKeyId: ${kms_key_id}
17+
%{~ endif ~}
18+
Platform: ${platform}
19+
Tags:
20+
${ indent(8, chomp(yamlencode(tags))) }
21+
%{~ if uri != null ~}
22+
Uri: ${uri}
23+
%{~ endif ~}
24+
%{~ if data != null ~}
25+
Data: |
26+
${indent(8, data)}
27+
%{~ endif ~}
28+
Outputs:
29+
ComponentArn:
30+
Description: ARN of the created component
31+
Value: !Ref "ImageBuildComponent"

component.yml.tpl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: ${name}-document
2+
%{ if description != null ~}
3+
description: ${description}
4+
%{ endif ~}
5+
schemaVersion: 1.0
6+
phases:
7+
- name: build
8+
steps:
9+
- name: ansible-install
10+
action: ExecuteBash
11+
inputs:
12+
commands:
13+
# Install Ansible dependencies
14+
- sudo yum install -y python python3 python-pip python3-pip git
15+
# Enable Ansible repository
16+
- sudo amazon-linux-extras enable ansible2
17+
# Install Ansible
18+
# TODO: #1 Enable version selection
19+
- sudo yum install -y ansible

examples/basic/README.md

Lines changed: 14 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,19 @@
22
A basic example for this repository
33

44
## Code
5-
Look to [main.tf](./main.tf), or be helpful and copy/paste that code here.
5+
```hcl
6+
module "test_component" {
7+
source = "rhythmictech/imagebuilder-component-ansible/aws"
8+
version = "~> 0.2.0"
9+
10+
component_version = "1.0.0"
11+
description = "Testing component"
12+
name = "testing-component"
13+
playbook_dir = "packer-generic-images/base"
14+
playbook_repo = "https://github.com/rhythmictech/packer-generic-images.git"
15+
tags = local.tags
16+
}
17+
```
618

719
## Applying
820
```
@@ -12,47 +24,5 @@ Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
1224
1325
Outputs:
1426
15-
example = {
16-
"tags_module" = {
17-
"name" = "TEST"
18-
"name32" = "TEST"
19-
"name6" = "TEST"
20-
"namenosymbols" = "TEST"
21-
"tags" = {
22-
"Name" = "TEST"
23-
"terraform_managed" = true
24-
"terraform_module" = "terraform-terraform-tags-1.0.0"
25-
"terraform_root_module" = "."
26-
"terraform_workspace" = "default"
27-
}
28-
"tags_as_list_of_maps" = [
29-
{
30-
"key" = "Name"
31-
"value" = "TEST"
32-
},
33-
{
34-
"key" = "terraform_managed"
35-
"value" = true
36-
},
37-
{
38-
"key" = "terraform_module"
39-
"value" = "terraform-terraform-tags-1.0.0"
40-
},
41-
{
42-
"key" = "terraform_root_module"
43-
"value" = "."
44-
},
45-
{
46-
"key" = "terraform_workspace"
47-
"value" = "default"
48-
},
49-
]
50-
"tags_no_name" = {
51-
"terraform_managed" = true
52-
"terraform_module" = "terraform-terraform-tags-1.0.0"
53-
"terraform_root_module" = "."
54-
"terraform_workspace" = "default"
55-
}
56-
}
57-
}
27+
component_arn = arn:aws:imagebuilder:us-east-1:000000000000:component/testing-component/1.0.0/1
5828
```

examples/basic/main.tf

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,62 @@
1+
data "aws_caller_identity" "current" {
2+
}
3+
4+
locals {
5+
account_id = data.aws_caller_identity.current.account_id
6+
tags = module.tags.tags_no_name
7+
}
8+
9+
module "tags" {
10+
source = "git::https://github.com/rhythmictech/terraform-terraform-tags.git?ref=v1.0.0"
11+
12+
names = [
13+
"smiller",
14+
"imagebuilder-test"
15+
]
16+
17+
tags = merge({
18+
"Env" = "test"
19+
"Namespace" = "smiller"
20+
"notes" = "Testing only - Can be safely deleted"
21+
"Owner" = var.owner
22+
}, var.additional_tags)
23+
}
24+
25+
module "test_component" {
26+
source = "rhythmictech/imagebuilder-component-ansible-setup/aws"
27+
version = "~> 1.0.0-rc1"
28+
29+
component_version = "1.0.0"
30+
description = "Testing component"
31+
name = "testing-component"
32+
tags = local.tags
33+
}
34+
35+
module "test_recipe" {
36+
source = "rhythmictech/imagebuilder-recipe/aws"
37+
version = "~> 0.2.0"
138

2-
module "example" {
3-
source = "../.."
39+
description = "Testing recipe"
40+
name = "test-recipe"
41+
parent_image = "arn:aws:imagebuilder:us-east-1:aws:image/amazon-linux-2-x86/x.x.x"
42+
recipe_version = "1.0.0"
43+
tags = local.tags
44+
update = true
445

5-
name = "test"
46+
component_arns = [
47+
module.test_component.component_arn,
48+
"arn:aws:imagebuilder:us-east-1:aws:component/simple-boot-test-linux/1.0.0/1",
49+
"arn:aws:imagebuilder:us-east-1:aws:component/reboot-test-linux/1.0.0/1"
50+
]
651
}
752

8-
output "example" {
9-
value = module.example
53+
module "test_pipeline" {
54+
source = "rhythmictech/imagebuilder-pipeline/aws"
55+
version = "~> 0.3.0"
56+
57+
description = "Testing pipeline"
58+
name = "test-pipeline"
59+
tags = local.tags
60+
recipe_arn = module.test_recipe.recipe_arn
61+
public = false
1062
}

main.tf

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,51 @@
1+
locals {
2+
has_ssh_key = var.ssh_key_secret_arn != null || var.ssh_key_secret_name != null
3+
4+
data = templatefile("${path.module}/component.yml.tpl", {
5+
description = var.description
6+
name = var.name
7+
ssh_key_name = try(data.aws_secretsmanager_secret.ssh_key[0].name, null)
8+
})
9+
}
110

2-
module "tags" {
3-
source = "rhythmictech/tags/terraform"
4-
version = "1.0.0"
11+
data "aws_secretsmanager_secret" "ssh_key" {
12+
count = local.has_ssh_key ? 1 : 0
513

6-
enforce_case = "UPPER"
7-
names = [var.name]
8-
tags = var.tags
14+
arn = var.ssh_key_secret_arn
15+
name = var.ssh_key_secret_name
916
}
1017

11-
locals {
12-
# tflint-ignore: terraform_unused_declarations
13-
name = module.tags.name
14-
# tflint-ignore: terraform_unused_declarations
15-
tags = module.tags.tags_no_name
18+
resource "aws_cloudformation_stack" "this" {
19+
name = "${var.name}-${uuid()}"
20+
on_failure = "ROLLBACK"
21+
timeout_in_minutes = var.cloudformation_timeout
22+
23+
tags = merge(
24+
var.tags,
25+
{ Name : "${var.name}-stack" }
26+
)
27+
28+
template_body = templatefile("${path.module}/cloudformation.yml.tpl", {
29+
change_description = var.change_description
30+
data = local.data
31+
description = var.description
32+
kms_key_id = var.kms_key_id
33+
name = var.name
34+
platform = var.platform
35+
uri = var.data_uri
36+
version = var.component_version
37+
38+
tags = merge(
39+
var.tags,
40+
{ Name : var.name }
41+
)
42+
})
43+
44+
lifecycle {
45+
create_before_destroy = true
46+
47+
ignore_changes = [
48+
name
49+
]
50+
}
1651
}

outputs.tf

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
1+
data "aws_caller_identity" "current" {}
2+
data "aws_region" "current" {}
13

2-
output "tags_module" {
3-
description = "Tags Module in it's entirety"
4-
value = module.tags
4+
locals {
5+
account_id = data.aws_caller_identity.current.account_id
6+
region = data.aws_region.current.name
7+
}
8+
9+
output "component_arn" {
10+
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+
]
516
}

0 commit comments

Comments
 (0)