Skip to content

Commit 569f5d2

Browse files
simoferr98Simone Ferraro
and
Simone Ferraro
authored
feat: initial draft #1 (#2)
* feat: initial draft #1 * fix: changes in the code following the tests done to the module. The code in the present resource block was changed without adding any new resources. * fix: change the description of the output component_name and component_arn * feat(examples): add of the complete example * fix (terraform version) change terraform required version * fix (examples): in the variables in the main and in the README.md - Added new variables in README.md and revised descriptions - variables.tf changed the description of the variables and added the variable launch_template_id_2 - main.tf changed the code of the module - versions.tf changed the terraform required version and required providers * fix (module): change file variables.tf, main.tf,version.tf and outputs.tf - variables.tf changed the description and type - outputs.tf changed the descriptyion - main.tf changed the code of the module - versions.tf changed the terraform required version and required providers * docs: add README.md Co-authored-by: Simone Ferraro <[email protected]>
1 parent 564e2c3 commit 569f5d2

13 files changed

+1054
-0
lines changed

.gitignore

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Local .terraform directories
2+
**/.terraform/*
3+
4+
# .tfstate files
5+
*.tfstate
6+
*.tfstate.*
7+
8+
# Crash log files
9+
crash.log
10+
11+
# Ignore any .tfvars files that are generated automatically for each Terraform run. Most
12+
# .tfvars files are managed as part of configuration and so should be included in
13+
# version control.
14+
#
15+
# example.tfvars
16+
17+
# Ignore override files as they are usually used to override resources locally and so
18+
# are not checked in
19+
override.tf
20+
override.tf.json
21+
*_override.tf
22+
*_override.tf.json
23+
24+
# Include override files you do wish to add to version control using negated pattern
25+
#
26+
# !example_override.tf
27+
28+
# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
29+
# example: *tfplan*
30+
31+
# Terraform Provider lock file
32+
.terraform.lock.hcl

README.md

Lines changed: 192 additions & 0 deletions
Large diffs are not rendered by default.

examples/complete/README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# EC2-Image-Builder
2+
3+
This example will manage the creation of EC2 Image builder with the creation of an iam role and Bucket S3 for logs.
4+
5+
## Usage
6+
7+
To run this example, you need to execute the following commands:
8+
9+
```shell
10+
$ terraform init
11+
$ terraform plan
12+
$ terraform apply
13+
```
14+
15+
:memo: **Note:** You will need a Terraform Cloud/Enterprise API token for authentication.
16+
You'll be prompted to insert it to provide a value for "tfc_token" variable.
17+
See [here](https://www.terraform.io/cloud-docs/users-teams-organizations/api-tokens)
18+
for further information.
19+
20+
:memo: **Note:** This root modules defines other several sensitive variables.
21+
You'll be prompted to insert the required values by default on every "plan" and "apply" action
22+
Choose the appropriate method to automatically specify these values, like described [here](https://www.terraform.io/language/values/variables#assigning-values-to-root-module-variables)
23+
24+
:warning: **Warning:** This example may create resources that cost money. Execute the command
25+
`terraform destroy` when the resources are no longer needed.
26+
27+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
28+
## Requirements
29+
30+
| Name | Version |
31+
|---------------------------------------------------------------------------|----------|
32+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.1.0 |
33+
| <a name="requirement_tfe"></a> [aws](#requirement\aws) | >= 4.20 |
34+
35+
## Providers
36+
37+
| Name | Version |
38+
|---------------------------------------------------|---------|
39+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 4.20 |
40+
41+
## Modules
42+
43+
| Name | Source | Version |
44+
|---------------------------------------------------------------------------------------|--------|---------|
45+
| <a name="module_advanced_workspace"></a> [imagebuilder](#module\_advanced\_workspace) | ../../ | n/a |
46+
47+
## Resources
48+
49+
| Name | Type |
50+
|------------------------------------------------------------------------------------------------------------------------------------------------------------|----------|
51+
| [aws_iam_role.iam_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
52+
| [aws_iam_instance_profile.istance_profile](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_account_alias) | resource |
53+
| [aws_iam_role_policy_attachment.policy_attachment](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_instance_profile) | resource |
54+
| [aws_s3_bucket.bucket_logs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket) | resource |
55+
| [aws_s3_bucket_public_access_block.bucket_logs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_public_access_block) | resource |
56+
| [aws_s3_bucket_acl.bucket_logs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_acl) | resource |
57+
58+
## Inputs
59+
60+
| Name | Description | Type | Default | Required |
61+
|------------------------------------------------------------------------------------------|----------------------------------------------------------|----------|---------|:--------:|
62+
| <a name="input_region"></a> [region](#input\_region) | Region where resources are created | `string` | `n/a` | yes |
63+
| <a name="input_name"></a> [name](#input\_name) | Generic resource name | `string` | `n/a` | yes |
64+
| <a name="input_account_id"></a> [account\_id](#account\_id) | The account ID used in the launch template configuration | `string` | `n/a` | yes |
65+
| <a name="launch_template_id_1"></a> [launch\_template\_id\_1](#ilaunch\_template\_id\_1) | The ID of the Amazon EC2 launch template one | `string` | `n/a` | yes |
66+
| <a name="launch_template_id_2"></a> [launch\_template\_id\_2](#ilaunch\_template\_id\_2) | The ID of the Amazon EC2 launch template two | `string` | `n/a` | yes |
67+
| <a name="input_tfc_token"></a> [user\_ids](#iuser\_ids) | Set of AWS Account user in the launch_permission | `string` | `n/a` | yes |
68+
69+
## Outputs
70+
71+
| Name | Description |
72+
|------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------|
73+
| <a name="output_component"></a> [component](#output\_component) | Map of Component |
74+
| <a name="output_component_name"></a> [component\_name](#output\_component\_name) | List of Name of the component |
75+
| <a name="output_component_arn"></a> [component\_arn](#output\_component\_arn) | List of Arn of the component |
76+
| <a name="output_image_recipe_arn"></a> [image\_recipe\_arn](#output\_image\_recipe\_arn) | Amazon Resource Name (ARN) of the image recipe |
77+
| <a name="output_infrastructure_configuration_arn"></a> [infrastructure\_configuration\_arn](#output\_infrastructure\_configuration\_arn) | arn of the infrastructure configuration |
78+
| <a name="output_distribution_configuration_name"></a> [distribution\_configuration\_name](#output\_distribution\_configuration\_name) | Name of the distribution configuration |
79+
| <a name="output_image_pipeline_arn"></a> [image\_pipeline\_arn](#output\_image\_pipeline\_arn) | Name of the distribution configuration |
80+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

examples/complete/data.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
data "aws_partition" "current" {}

examples/complete/locals.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
locals {
2+
common_tags = {
3+
Terraform = true
4+
Project = var.name
5+
}
6+
}

examples/complete/main.tf

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
################################################################################
2+
# Role IAM for EC2-Image-Builder Module
3+
################################################################################
4+
resource "aws_iam_role" "iam_role" {
5+
name = var.name
6+
path = "/${var.name}/"
7+
8+
assume_role_policy = <<EOF
9+
{
10+
"Version": "2008-10-17",
11+
"Statement": [
12+
{
13+
"Action": "sts:AssumeRole",
14+
"Principal": {
15+
"Service": ["ec2.amazonaws.com"]
16+
},
17+
"Effect": "Allow"
18+
}
19+
]
20+
}
21+
EOF
22+
}
23+
24+
resource "aws_iam_instance_profile" "istance_profile" {
25+
name = var.name
26+
role = aws_iam_role.iam_role.name
27+
}
28+
29+
resource "aws_iam_role_policy_attachment" "policy_attachment" {
30+
role = aws_iam_role.iam_role.id
31+
policy_arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/EC2InstanceProfileForImageBuilder"
32+
}
33+
34+
################################################################################
35+
# S3 Bucket for logs
36+
################################################################################
37+
resource "aws_s3_bucket" "bucket_logs" {
38+
bucket = "${var.account_id}-${var.name}-s3"
39+
40+
}
41+
42+
resource "aws_s3_bucket_public_access_block" "bucket_logs" {
43+
bucket = aws_s3_bucket.bucket_logs.id
44+
45+
block_public_acls = true
46+
block_public_policy = true
47+
ignore_public_acls = true
48+
restrict_public_buckets = true
49+
50+
depends_on = [
51+
aws_s3_bucket.bucket_logs
52+
]
53+
}
54+
55+
resource "aws_s3_bucket_acl" "bucket_logs" {
56+
bucket = aws_s3_bucket.bucket_logs.id
57+
acl = "private"
58+
}
59+
60+
################################################################################
61+
# EC2-Image-Builder Module
62+
################################################################################
63+
module "imagebuilder" {
64+
source = "../../"
65+
66+
name = var.name
67+
68+
components = [
69+
{
70+
"data" = yamlencode({
71+
phases = [
72+
{
73+
name = "build"
74+
steps = [
75+
{
76+
action = "ExecuteBash"
77+
inputs = {
78+
commands = ["echo 'hello world'"]
79+
}
80+
name = "helloworld"
81+
onFailure = "Continue"
82+
}
83+
]
84+
}
85+
]
86+
schemaVersion = 1.0
87+
}),
88+
"name" = "hello world",
89+
"description" = "component hello world",
90+
"version" = "1.0.0"
91+
}
92+
]
93+
94+
component_external_arns = ["arn:aws:imagebuilder:${var.region}:aws:component/amazon-cloudwatch-agent-linux/1.0.1/1"]
95+
96+
image_recipe_parent_image = "arn:${data.aws_partition.current.partition}:imagebuilder:${var.region}:aws:image/ubuntu-server-20-lts-x86/2022.3.8"
97+
image_recipe_version = "1.0.0"
98+
99+
block_device_mapping = [
100+
{
101+
device_name = "/dev/sda"
102+
delete_on_termination = true
103+
volume_size = 10
104+
volume_type = "gp3"
105+
}
106+
]
107+
108+
infrastructure_configuration_instance_profile_name = aws_iam_instance_profile.istance_profile.name
109+
infrastructure_configuration_instance_types = ["t3.nano"]
110+
111+
infrastructure_configuration_s3_logs_enabled = false
112+
infrastructure_configuration_s3_bucket_name = aws_s3_bucket.bucket_logs.bucket
113+
114+
enable_resource_tags = true
115+
resource_tags = local.common_tags
116+
117+
118+
distribution_configuration_region = var.region
119+
120+
ami_distribution_configuration = {
121+
name = var.name
122+
123+
ami_tags = {
124+
CostCenter = "IT"
125+
}
126+
127+
}
128+
129+
launch_permission = {
130+
user_ids = var.user_ids
131+
}
132+
133+
launch_template_configuration = [
134+
{
135+
launch_template_id = var.launch_template_id_1
136+
account_id = var.account_id
137+
},
138+
{
139+
launch_template_id = var.launch_template_id_2
140+
account_id = var.account_id
141+
}
142+
]
143+
144+
image_tests_configuration_schedule_enabled = true
145+
image_tests_configuration_schedule_expression = "cron(0 0 * * ? *)"
146+
}

examples/complete/outputs.tf

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
output "component" {
2+
description = "Map of Component"
3+
value = module.imagebuilder.component
4+
}
5+
6+
output "component_name" {
7+
description = "List of Name of the component"
8+
value = module.imagebuilder.component_name
9+
}
10+
11+
output "component_arn" {
12+
description = "List of Arn of the component"
13+
value = module.imagebuilder.component_arn
14+
}
15+
16+
output "image_recipe_arn" {
17+
description = "Amazon Resource Name (ARN) of the image recipe"
18+
value = module.imagebuilder.image_recipe_arn
19+
}
20+
21+
output "infrastructure_configuration_arn" {
22+
description = "arn of the infrastructure configuration"
23+
value = module.imagebuilder.infrastructure_configuration_arn
24+
}
25+
26+
output "distribution_configuration_name" {
27+
description = "Name of the distribution configuration"
28+
value = module.imagebuilder.distribution_configuration_name
29+
}
30+
31+
output "image_pipeline_arn" {
32+
description = "Name of the distribution configuration"
33+
value = module.imagebuilder.image_pipeline_arn
34+
}

examples/complete/variables.tf

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
variable "region" {
2+
description = "Region where resources are created"
3+
type = string
4+
}
5+
6+
variable "name" {
7+
description = "Generic resource name"
8+
type = string
9+
}
10+
11+
variable "account_id" {
12+
description = "The account ID used in the launch template configuration"
13+
type = string
14+
sensitive = true
15+
}
16+
17+
variable "launch_template_id_1" {
18+
description = "The ID of the Amazon EC2 launch template one"
19+
type = string
20+
sensitive = true
21+
}
22+
23+
variable "launch_template_id_2" {
24+
description = "The ID of the Amazon EC2 launch template two"
25+
type = string
26+
sensitive = true
27+
}
28+
29+
variable "user_ids" {
30+
description = "Set of AWS Account user in the launch_permission"
31+
type = list(string)
32+
sensitive = true
33+
}

examples/complete/versions.tf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
terraform {
2+
required_version = ">= 1.1.0"
3+
4+
required_providers {
5+
aws = {
6+
source = "hashicorp/aws"
7+
version = ">= 4.24"
8+
}
9+
}
10+
}
11+
12+
provider "aws" {
13+
region = var.region
14+
15+
default_tags {
16+
tags = local.common_tags
17+
}
18+
}

0 commit comments

Comments
 (0)