Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PFMENG-2731: Use aws_iam_role's manageed_policy_arns instead of opensource terraform-aws-modules #28

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/ec2/sg.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
resource "aws_security_group" "ecs_sg" {
#checkov:skip=CKV2_AWS_5:Security group is attached to another resource
#checkov:skip=CKV_AWS_382: "Ensure no security groups allow egress from 0.0.0.0:0 to port -1"
name = "ecs-sg-${var.name}"
description = "Allow inbound traffic"
vpc_id = data.aws_vpc.default.id
Expand Down Expand Up @@ -38,6 +39,7 @@ resource "aws_security_group" "lb_public_sg" {
}

resource "aws_security_group_rule" "lb_sg_allow_all" {
#checkov:skip=CKV_AWS_382: "Ensure no security groups allow egress from 0.0.0.0:0 to port -1"
description = "Allow all outbound traffic"
type = "egress"
from_port = 0
Expand Down
2 changes: 2 additions & 0 deletions examples/fargate/sg.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
resource "aws_security_group" "ecs_sg" {
#checkov:skip=CKV2_AWS_5:Security group is attached to another resource
#checkov:skip=CKV_AWS_382: "Ensure no security groups allow egress from 0.0.0.0:0 to port -1"
name = "ecs-sg-${var.name}"
description = "Allow inbound traffic"
vpc_id = data.aws_vpc.default.id
Expand Down Expand Up @@ -38,6 +39,7 @@ resource "aws_security_group" "lb_public_sg" {
}

resource "aws_security_group_rule" "lb_sg_allow_all" {
#checkov:skip=CKV_AWS_382: "Ensure no security groups allow egress from 0.0.0.0:0 to port -1"
description = "Allow all outbound traffic"
type = "egress"
from_port = 0
Expand Down
6 changes: 4 additions & 2 deletions modules/iam/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.38.0 |
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.83.1 |

## Modules

| Name | Source | Version |
|------|--------|---------|
| <a name="module_iam_assumable_role"></a> [iam\_assumable\_role](#module\_iam\_assumable\_role) | terraform-aws-modules/iam/aws//modules/iam-assumable-role | ~> 4.13.0 |
| <a name="module_iam_policy"></a> [iam\_policy](#module\_iam\_policy) | terraform-aws-modules/iam/aws//modules/iam-policy | ~> 4.13.0 |

## Resources

| Name | Type |
|------|------|
| [aws_iam_instance_profile.iam_instance_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_instance_profile) | resource |
| [aws_iam_role.iam_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
| [aws_iam_role_policy_attachment.attach](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
| [aws_iam_policy_document.assume_role_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |

## Inputs

Expand Down
15 changes: 15 additions & 0 deletions modules/iam/data.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
data "aws_iam_policy_document" "assume_role_policy" {
statement {
effect = "Allow"
principals {
type = "AWS"
identifiers = var.trusted_role_arns
}

principals {
type = "Service"
identifiers = var.trusted_role_services
}
actions = ["sts:AssumeRole"]
}
}
27 changes: 15 additions & 12 deletions modules/iam/main.tf
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
module "iam_assumable_role" {
source = "terraform-aws-modules/iam/aws//modules/iam-assumable-role"
version = "~> 4.13.0"
resource "aws_iam_role" "iam_role" {
name = var.role_name

trusted_role_arns = var.trusted_role_arns
trusted_role_services = var.trusted_role_services
custom_role_policy_arns = var.custom_role_policy_arns
force_detach_policies = true
assume_role_policy = data.aws_iam_policy_document.assume_role_policy.json
managed_policy_arns = var.custom_role_policy_arns

create_role = true
create_instance_profile = var.create_instance_profile
role_requires_mfa = false
tags = merge(var.tags, { "Name" = var.role_name })
}

role_name = var.role_name
resource "aws_iam_instance_profile" "iam_instance_role" {
count = var.create_instance_profile ? 1 : 0

tags = merge(var.tags, { "Name" = var.role_name })
name = var.role_name
path = "/"
role = aws_iam_role.iam_role.name

tags = var.tags
}

module "iam_policy" {
Expand All @@ -30,6 +33,6 @@ module "iam_policy" {
resource "aws_iam_role_policy_attachment" "attach" {
count = length(var.policy) > 0 ? 1 : 0

role = module.iam_assumable_role.iam_role_name
role = aws_iam_role.iam_role.name
policy_arn = module.iam_policy.arn
}
9 changes: 9 additions & 0 deletions modules/iam/moved.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
moved {
from = module.iam_assumable_role.aws_iam_role.this
to = aws_iam_role.iam_role
}

moved {
from = module.iam_assumable_role.aws_iam_instance_profile.this
to = aws_iam_instance_profile.iam_instance_role
}
8 changes: 4 additions & 4 deletions modules/iam/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
output "iam_role_arn" {
description = "ARN of IAM role"
value = try(module.iam_assumable_role.iam_role_arn, "")
value = try(aws_iam_role.iam_role.arn, "")
}

output "iam_role_name" {
description = "Name of IAM role"
value = try(module.iam_assumable_role.iam_role_name, "")
value = try(aws_iam_role.iam_role.name, "")
}

output "iam_instance_profile_arn" {
description = "ARN of IAM instance profile"
value = try(module.iam_assumable_role.iam_instance_profile_arn, "")
value = try(aws_iam_instance_profile.iam_instance_role[0].arn, "")
}

output "iam_instance_profile_name" {
description = "Name of IAM instance profile"
value = try(module.iam_assumable_role.iam_instance_profile_name, "")
value = try(aws_iam_instance_profile.iam_instance_role[0].name, "")
}

output "iam_policy_id" {
Expand Down
2 changes: 1 addition & 1 deletion modules/service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.38.0 |
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.83.1 |

## Modules

Expand Down
1 change: 1 addition & 0 deletions modules/service/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ data "aws_ecs_task_definition" "this" {

resource "aws_ecs_service" "this" {
#checkov:skip=CKV_AWS_332: Already defaulting to latest FARGATE platform version
#checkov:skip=CKV_AWS_333: "Ensure ECS services do not have public IP addresses assigned to them automatically"

name = var.name
cluster = var.cluster_id
Expand Down
Loading