Skip to content

Commit cadf1a1

Browse files
rsrchboyjoe-nilandactions-bot
authored
Allow instance profiles to be provided (#60)
Co-authored-by: Joe Niland <joe@originalmind.com.au> Co-authored-by: actions-bot <58130806+actions-bot@users.noreply.github.com>
1 parent 6b0ab4e commit cadf1a1

File tree

5 files changed

+38
-14
lines changed

5 files changed

+38
-14
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ Available targets:
189189
| environment | Environment, e.g. 'prod', 'staging', 'dev', 'pre-prod', 'UAT' | `string` | `""` | no |
190190
| evaluation\_periods | The number of periods over which data is compared to the specified threshold. | `number` | `5` | no |
191191
| instance\_enabled | Flag to control the instance creation. Set to false if it is necessary to skip instance creation | `bool` | `true` | no |
192+
| instance\_profile | A pre-defined profile to attach to the instance (default is to build our own) | `string` | `""` | no |
192193
| instance\_type | The type of the instance | `string` | `"t2.micro"` | no |
193194
| ipv6\_address\_count | Number of IPv6 addresses to associate with the primary network interface. Amazon EC2 chooses the IPv6 addresses from the range of your subnet (-1 to use subnet default) | `number` | `0` | no |
194195
| ipv6\_addresses | List of IPv6 addresses from the range of the subnet to associate with the primary network interface | `list(string)` | `[]` | no |
@@ -223,6 +224,7 @@ Available targets:
223224
| alarm | CloudWatch Alarm ID |
224225
| ebs\_ids | IDs of EBSs |
225226
| id | Disambiguated ID of the instance |
227+
| instance\_profile | Name of the instance's profile (either built or supplied) |
226228
| name | Instance name |
227229
| primary\_network\_interface\_id | ID of the instance's primary network interface |
228230
| private\_dns | Private DNS of instance |

docs/terraform.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
| environment | Environment, e.g. 'prod', 'staging', 'dev', 'pre-prod', 'UAT' | `string` | `""` | no |
4242
| evaluation\_periods | The number of periods over which data is compared to the specified threshold. | `number` | `5` | no |
4343
| instance\_enabled | Flag to control the instance creation. Set to false if it is necessary to skip instance creation | `bool` | `true` | no |
44+
| instance\_profile | A pre-defined profile to attach to the instance (default is to build our own) | `string` | `""` | no |
4445
| instance\_type | The type of the instance | `string` | `"t2.micro"` | no |
4546
| ipv6\_address\_count | Number of IPv6 addresses to associate with the primary network interface. Amazon EC2 chooses the IPv6 addresses from the range of your subnet (-1 to use subnet default) | `number` | `0` | no |
4647
| ipv6\_addresses | List of IPv6 addresses from the range of the subnet to associate with the primary network interface | `list(string)` | `[]` | no |
@@ -75,6 +76,7 @@
7576
| alarm | CloudWatch Alarm ID |
7677
| ebs\_ids | IDs of EBSs |
7778
| id | Disambiguated ID of the instance |
79+
| instance\_profile | Name of the instance's profile (either built or supplied) |
7880
| name | Instance name |
7981
| primary\_network\_interface\_id | ID of the instance's primary network interface |
8082
| private\_dns | Private DNS of instance |

main.tf

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
locals {
2-
instance_count = var.instance_enabled ? 1 : 0
3-
security_group_count = var.create_default_security_group ? 1 : 0
4-
region = var.region != "" ? var.region : data.aws_region.default.name
5-
root_iops = var.root_volume_type == "io1" ? var.root_iops : "0"
6-
ebs_iops = var.ebs_volume_type == "io1" ? var.ebs_iops : "0"
7-
availability_zone = var.availability_zone != "" ? var.availability_zone : data.aws_subnet.default.availability_zone
8-
ami = var.ami != "" ? var.ami : join("", data.aws_ami.default.*.image_id)
9-
ami_owner = var.ami != "" ? var.ami_owner : join("", data.aws_ami.default.*.owner_id)
10-
root_volume_type = var.root_volume_type != "" ? var.root_volume_type : data.aws_ami.info.root_device_type
11-
public_dns = var.associate_public_ip_address && var.assign_eip_address && var.instance_enabled ? data.null_data_source.eip.outputs["public_dns"] : join("", aws_instance.default.*.public_dns)
2+
instance_count = var.instance_enabled ? 1 : 0
3+
# create an instance profile if the instance is enabled and we aren't given
4+
# one to use
5+
instance_profile_count = ! var.instance_enabled ? 0 : length(var.instance_profile) > 0 ? 0 : 1
6+
instance_profile = local.instance_profile_count == 0 ? var.instance_profile : join("", aws_iam_instance_profile.default.*.name)
7+
security_group_count = var.create_default_security_group ? 1 : 0
8+
region = var.region != "" ? var.region : data.aws_region.default.name
9+
root_iops = var.root_volume_type == "io1" ? var.root_iops : "0"
10+
ebs_iops = var.ebs_volume_type == "io1" ? var.ebs_iops : "0"
11+
availability_zone = var.availability_zone != "" ? var.availability_zone : data.aws_subnet.default.availability_zone
12+
ami = var.ami != "" ? var.ami : join("", data.aws_ami.default.*.image_id)
13+
ami_owner = var.ami != "" ? var.ami_owner : join("", data.aws_ami.default.*.owner_id)
14+
root_volume_type = var.root_volume_type != "" ? var.root_volume_type : data.aws_ami.info.root_device_type
15+
public_dns = var.associate_public_ip_address && var.assign_eip_address && var.instance_enabled ? data.null_data_source.eip.outputs["public_dns"] : join("", aws_instance.default.*.public_dns)
1216
}
1317

1418
data "aws_caller_identity" "default" {
@@ -67,6 +71,11 @@ data "aws_ami" "info" {
6771
owners = [local.ami_owner]
6872
}
6973

74+
data "aws_iam_instance_profile" "given" {
75+
count = var.instance_enabled && length(var.instance_profile) > 0 ? 1 : 0
76+
name = var.instance_profile
77+
}
78+
7079
module "label" {
7180
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.16.0"
7281
namespace = var.namespace
@@ -80,13 +89,13 @@ module "label" {
8089
}
8190

8291
resource "aws_iam_instance_profile" "default" {
83-
count = local.instance_count
92+
count = local.instance_profile_count
8493
name = module.label.id
8594
role = join("", aws_iam_role.default.*.name)
8695
}
8796

8897
resource "aws_iam_role" "default" {
89-
count = local.instance_count
98+
count = local.instance_profile_count
9099
name = module.label.id
91100
path = "/"
92101
assume_role_policy = data.aws_iam_policy_document.default.json
@@ -101,7 +110,7 @@ resource "aws_instance" "default" {
101110
ebs_optimized = var.ebs_optimized
102111
disable_api_termination = var.disable_api_termination
103112
user_data = var.user_data
104-
iam_instance_profile = join("", aws_iam_instance_profile.default.*.name)
113+
iam_instance_profile = local.instance_profile
105114
associate_public_ip_address = var.associate_public_ip_address
106115
key_name = var.ssh_key_pair
107116
subnet_id = var.subnet

outputs.tf

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ output "security_group_ids" {
4545

4646
output "role" {
4747
description = "Name of AWS IAM Role associated with the instance"
48-
value = join("", aws_iam_role.default.*.name)
48+
value = local.instance_profile_count > 0 ? join("", aws_iam_role.default.*.name) : join("", data.aws_iam_instance_profile.given.*.role_name)
4949
}
5050

5151
output "alarm" {
@@ -70,3 +70,8 @@ output "primary_network_interface_id" {
7070
description = "ID of the instance's primary network interface"
7171
value = join("", aws_instance.default.*.primary_network_interface_id)
7272
}
73+
74+
output "instance_profile" {
75+
description = "Name of the instance's profile (either built or supplied)"
76+
value = local.instance_profile
77+
}

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,9 @@ variable "permissions_boundary_arn" {
287287
description = "Policy ARN to attach to instance role as a permissions boundary"
288288
default = ""
289289
}
290+
291+
variable "instance_profile" {
292+
type = string
293+
description = "A pre-defined profile to attach to the instance (default is to build our own)"
294+
default = ""
295+
}

0 commit comments

Comments
 (0)