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

Commit

Permalink
New: module iam-instance-profile
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Magicloud committed Jul 24, 2019
1 parent ae28905 commit d9d11fa
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 88 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

### Modules

* `iam-instance-profile`: Abstract the usage pattern of IAM instance profile.


### Examples

Expand All @@ -25,7 +27,6 @@

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


# v0.9.0

### Summary
Expand Down
5 changes: 3 additions & 2 deletions examples/nexus-asg/nexus.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@

variable "region" {
description = "The region to put resources in"
default = "us-east-1"
default = "us-east-2"
}

variable "az" {
description = "The availability zone to put resources in"
default = "us-east-1a"
default = "us-east-2b"
}

variable "key_name" {
description = "The keypair used to ssh into the asg intances"
default = "shida-east-2"
}

module "vpc" {
Expand Down
21 changes: 21 additions & 0 deletions modules/iam-instance-profile/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# IAM Instance Profile

This module abstracts the useage pattern of IAM instance profile. The caller provides role/policy, and gets profile id to assign to instance.

Sample usgae:

```
module "iam_instance_profile" {
source = "../iam-instance-profile"
assume_role_policy = "${data.aws_iam_policy_document.attach_ebs.json}"
policy = "${data.aws_iam_policy_document.attach_ebs_policy.json}"
name_prefix = "persistent-ebs"
}
module "server" {
source = "../asg"
iam_profile = "${module.iam_instance_profile.iam_profile_id}"
# other things here is ignored
}
```
38 changes: 38 additions & 0 deletions modules/iam-instance-profile/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
variable "name_prefix" {
description = "Creates a unique name beginning with the specified prefix."
}

resource "aws_iam_instance_profile" "profile" {
name_prefix = var.name_prefix
role = aws_iam_role.role.name
}

resource "aws_iam_role" "role" {
name = var.name_prefix
path = "/"
assume_role_policy = <<-EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF

}

output "iam_role_name" {
value = aws_iam_role.role.name
}

output "iam_profile_id" {
value = aws_iam_instance_profile.profile.id
}

4 changes: 4 additions & 0 deletions modules/iam-instance-profile/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

terraform {
required_version = ">= 0.12"
}
21 changes: 6 additions & 15 deletions modules/persistent-ebs/data.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,7 @@ data "aws_caller_identity" "current" {
data "aws_partition" "current" {
}

data "aws_iam_policy_document" "attach_ebs" {
statement {
sid = ""
effect = "Allow"

principals {
type = "Service"
identifiers = ["ec2.amazonaws.com"]
}

actions = ["sts:AssumeRole"]
}
}

data "aws_iam_policy_document" "attach_ebs_policy" {
data "aws_iam_policy_document" "attach_ebs_policy_doc" {
statement {
sid = ""
effect = "Allow"
Expand All @@ -35,3 +21,8 @@ data "aws_iam_policy_document" "attach_ebs_policy" {
}
}

resource "aws_iam_policy" "attach_ebs_policy" {
name = "attach_ebs"

policy = data.aws_iam_policy_document.attach_ebs_policy_doc.json
}
19 changes: 0 additions & 19 deletions modules/persistent-ebs/iam.tf

This file was deleted.

26 changes: 3 additions & 23 deletions modules/persistent-ebs/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,9 @@ resource "aws_ebs_volume" "main" {
)
}

output "iam_profile_id" {
value = aws_iam_instance_profile.attach_ebs.id
description = "`id` exported from the `aws_iam_instance_profile`"
}

output "iam_profile_arn" {
value = aws_iam_instance_profile.attach_ebs.arn
description = "`arn` exported from the `aws_iam_instance_profile`"
}

output "iam_profile_policy_document" {
value = aws_iam_role_policy.attach_ebs.policy
description = "`policy` exported from the `aws_iam_role_policy`"
}

output "iam_role_arn" {
value = aws_iam_role.attach_ebs.arn
description = "`arn` exported from the `aws_iam_role`"
}

output "iam_role_name" {
value = aws_iam_role.attach_ebs.name
description = "`name` exported from the `aws_iam_role`"
resource "aws_iam_role_policy_attachment" "attach_ebs" {
role = var.iam_instance_profile_role_name
policy_arn = aws_iam_policy.attach_ebs_policy.arn
}

output "volume_id" {
Expand Down
4 changes: 4 additions & 0 deletions modules/persistent-ebs/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,7 @@ variable "extra_tags" {
type = map(string)
}

variable "iam_instance_profile_role_name" {
description = "The role to attach policy needed by this module."
type = string
}
29 changes: 17 additions & 12 deletions modules/single-node-asg/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,22 @@
*/

module "service-data" {
source = "../persistent-ebs"
name_prefix = "${var.name_prefix}-${var.name_suffix}-data"
region = var.region
az = data.aws_subnet.server-subnet.availability_zone
size = var.data_volume_size
iops = var.data_volume_iops
volume_type = var.data_volume_type
encrypted = var.data_volume_encrypted
kms_key_id = var.data_volume_kms_key_id
snapshot_id = var.data_volume_snapshot_id
source = "../persistent-ebs"
name_prefix = "${var.name_prefix}-${var.name_suffix}-data"
region = var.region
az = data.aws_subnet.server-subnet.availability_zone
size = var.data_volume_size
iops = var.data_volume_iops
volume_type = var.data_volume_type
encrypted = var.data_volume_encrypted
kms_key_id = var.data_volume_kms_key_id
snapshot_id = var.data_volume_snapshot_id
iam_instance_profile_role_name = module.instance_profile.iam_role_name
}

module "instance_profile" {
source = "../iam-instance-profile"
name_prefix = "${var.name_prefix}-${var.name_suffix}"
}

module "server" {
Expand All @@ -44,8 +50,7 @@ module "server" {
root_volume_type = var.root_volume_type
root_volume_size = var.root_volume_size

#
iam_profile = module.service-data.iam_profile_id
iam_profile = module.instance_profile.iam_profile_id

user_data = <<END_INIT
#!/bin/bash
Expand Down
16 changes: 0 additions & 16 deletions modules/single-node-asg/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,3 @@ output "asg_name" {
value = module.server.name
description = "`name` exported from the Server `aws_autoscaling_group`"
}

output "asg_iam_profile_arn" {
value = module.service-data.iam_profile_arn
description = "`arn` exported from the Service Data `aws_iam_profile`"
}

output "asg_iam_role_arn" {
value = module.service-data.iam_role_arn
description = "`arn` exported from the Service Data `aws_iam_role`"
}

output "asg_iam_role_name" {
value = module.service-data.iam_role_name
description = "`name` exported from the Service Data `aws_iam_role`"
}

0 comments on commit d9d11fa

Please sign in to comment.