Skip to content
This repository was archived by the owner on Jul 11, 2023. It is now read-only.

Commit d9d11fa

Browse files
committed
New: module iam-instance-profile
Abstract a usage pattern for IAM instance profile. The instance level should setup this module and pass the role name to modules that attach the policy. Refer to single-node-asg and persistent-ebs for usage. Simply export profile id for attaching to instance, and role name for ataching policies.
1 parent ae28905 commit d9d11fa

File tree

11 files changed

+98
-88
lines changed

11 files changed

+98
-88
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
### Modules
77

8+
* `iam-instance-profile`: Abstract the usage pattern of IAM instance profile.
9+
810

911
### Examples
1012

@@ -25,7 +27,6 @@
2527

2628
* `load-asg`: updated to use new `autoscaling-policy-metric-alarm-pair` module
2729

28-
2930
# v0.9.0
3031

3132
### Summary

examples/nexus-asg/nexus.tf

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,17 @@
1616

1717
variable "region" {
1818
description = "The region to put resources in"
19-
default = "us-east-1"
19+
default = "us-east-2"
2020
}
2121

2222
variable "az" {
2323
description = "The availability zone to put resources in"
24-
default = "us-east-1a"
24+
default = "us-east-2b"
2525
}
2626

2727
variable "key_name" {
2828
description = "The keypair used to ssh into the asg intances"
29+
default = "shida-east-2"
2930
}
3031

3132
module "vpc" {
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# IAM Instance Profile
2+
3+
This module abstracts the useage pattern of IAM instance profile. The caller provides role/policy, and gets profile id to assign to instance.
4+
5+
Sample usgae:
6+
7+
```
8+
module "iam_instance_profile" {
9+
source = "../iam-instance-profile"
10+
assume_role_policy = "${data.aws_iam_policy_document.attach_ebs.json}"
11+
policy = "${data.aws_iam_policy_document.attach_ebs_policy.json}"
12+
name_prefix = "persistent-ebs"
13+
}
14+
15+
module "server" {
16+
source = "../asg"
17+
iam_profile = "${module.iam_instance_profile.iam_profile_id}"
18+
19+
# other things here is ignored
20+
}
21+
```

modules/iam-instance-profile/main.tf

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
variable "name_prefix" {
2+
description = "Creates a unique name beginning with the specified prefix."
3+
}
4+
5+
resource "aws_iam_instance_profile" "profile" {
6+
name_prefix = var.name_prefix
7+
role = aws_iam_role.role.name
8+
}
9+
10+
resource "aws_iam_role" "role" {
11+
name = var.name_prefix
12+
path = "/"
13+
assume_role_policy = <<-EOF
14+
{
15+
"Version": "2012-10-17",
16+
"Statement": [
17+
{
18+
"Action": "sts:AssumeRole",
19+
"Principal": {
20+
"Service": "ec2.amazonaws.com"
21+
},
22+
"Effect": "Allow",
23+
"Sid": ""
24+
}
25+
]
26+
}
27+
EOF
28+
29+
}
30+
31+
output "iam_role_name" {
32+
value = aws_iam_role.role.name
33+
}
34+
35+
output "iam_profile_id" {
36+
value = aws_iam_instance_profile.profile.id
37+
}
38+
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
terraform {
3+
required_version = ">= 0.12"
4+
}

modules/persistent-ebs/data.tf

+6-15
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,7 @@ data "aws_caller_identity" "current" {
44
data "aws_partition" "current" {
55
}
66

7-
data "aws_iam_policy_document" "attach_ebs" {
8-
statement {
9-
sid = ""
10-
effect = "Allow"
11-
12-
principals {
13-
type = "Service"
14-
identifiers = ["ec2.amazonaws.com"]
15-
}
16-
17-
actions = ["sts:AssumeRole"]
18-
}
19-
}
20-
21-
data "aws_iam_policy_document" "attach_ebs_policy" {
7+
data "aws_iam_policy_document" "attach_ebs_policy_doc" {
228
statement {
239
sid = ""
2410
effect = "Allow"
@@ -35,3 +21,8 @@ data "aws_iam_policy_document" "attach_ebs_policy" {
3521
}
3622
}
3723

24+
resource "aws_iam_policy" "attach_ebs_policy" {
25+
name = "attach_ebs"
26+
27+
policy = data.aws_iam_policy_document.attach_ebs_policy_doc.json
28+
}

modules/persistent-ebs/iam.tf

-19
This file was deleted.

modules/persistent-ebs/main.tf

+3-23
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,9 @@ resource "aws_ebs_volume" "main" {
2525
)
2626
}
2727

28-
output "iam_profile_id" {
29-
value = aws_iam_instance_profile.attach_ebs.id
30-
description = "`id` exported from the `aws_iam_instance_profile`"
31-
}
32-
33-
output "iam_profile_arn" {
34-
value = aws_iam_instance_profile.attach_ebs.arn
35-
description = "`arn` exported from the `aws_iam_instance_profile`"
36-
}
37-
38-
output "iam_profile_policy_document" {
39-
value = aws_iam_role_policy.attach_ebs.policy
40-
description = "`policy` exported from the `aws_iam_role_policy`"
41-
}
42-
43-
output "iam_role_arn" {
44-
value = aws_iam_role.attach_ebs.arn
45-
description = "`arn` exported from the `aws_iam_role`"
46-
}
47-
48-
output "iam_role_name" {
49-
value = aws_iam_role.attach_ebs.name
50-
description = "`name` exported from the `aws_iam_role`"
28+
resource "aws_iam_role_policy_attachment" "attach_ebs" {
29+
role = var.iam_instance_profile_role_name
30+
policy_arn = aws_iam_policy.attach_ebs_policy.arn
5131
}
5232

5333
output "volume_id" {

modules/persistent-ebs/variables.tf

+4
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,7 @@ variable "extra_tags" {
5656
type = map(string)
5757
}
5858

59+
variable "iam_instance_profile_role_name" {
60+
description = "The role to attach policy needed by this module."
61+
type = string
62+
}

modules/single-node-asg/main.tf

+17-12
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,22 @@
1212
*/
1313

1414
module "service-data" {
15-
source = "../persistent-ebs"
16-
name_prefix = "${var.name_prefix}-${var.name_suffix}-data"
17-
region = var.region
18-
az = data.aws_subnet.server-subnet.availability_zone
19-
size = var.data_volume_size
20-
iops = var.data_volume_iops
21-
volume_type = var.data_volume_type
22-
encrypted = var.data_volume_encrypted
23-
kms_key_id = var.data_volume_kms_key_id
24-
snapshot_id = var.data_volume_snapshot_id
15+
source = "../persistent-ebs"
16+
name_prefix = "${var.name_prefix}-${var.name_suffix}-data"
17+
region = var.region
18+
az = data.aws_subnet.server-subnet.availability_zone
19+
size = var.data_volume_size
20+
iops = var.data_volume_iops
21+
volume_type = var.data_volume_type
22+
encrypted = var.data_volume_encrypted
23+
kms_key_id = var.data_volume_kms_key_id
24+
snapshot_id = var.data_volume_snapshot_id
25+
iam_instance_profile_role_name = module.instance_profile.iam_role_name
26+
}
27+
28+
module "instance_profile" {
29+
source = "../iam-instance-profile"
30+
name_prefix = "${var.name_prefix}-${var.name_suffix}"
2531
}
2632

2733
module "server" {
@@ -44,8 +50,7 @@ module "server" {
4450
root_volume_type = var.root_volume_type
4551
root_volume_size = var.root_volume_size
4652

47-
#
48-
iam_profile = module.service-data.iam_profile_id
53+
iam_profile = module.instance_profile.iam_profile_id
4954

5055
user_data = <<END_INIT
5156
#!/bin/bash

modules/single-node-asg/outputs.tf

-16
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,3 @@ output "asg_name" {
22
value = module.server.name
33
description = "`name` exported from the Server `aws_autoscaling_group`"
44
}
5-
6-
output "asg_iam_profile_arn" {
7-
value = module.service-data.iam_profile_arn
8-
description = "`arn` exported from the Service Data `aws_iam_profile`"
9-
}
10-
11-
output "asg_iam_role_arn" {
12-
value = module.service-data.iam_role_arn
13-
description = "`arn` exported from the Service Data `aws_iam_role`"
14-
}
15-
16-
output "asg_iam_role_name" {
17-
value = module.service-data.iam_role_name
18-
description = "`name` exported from the Service Data `aws_iam_role`"
19-
}
20-

0 commit comments

Comments
 (0)