Skip to content

Commit

Permalink
Update SSM policy to least privilege permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
kieranbrown committed Feb 27, 2024
1 parent 85d533b commit 6883f63
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ module "fck-nat" {
| [aws_autoscaling_group.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/autoscaling_group) | resource |
| [aws_iam_instance_profile.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_instance_profile) | resource |
| [aws_iam_role.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
| [aws_iam_role_policy_attachment.ssm](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
| [aws_instance.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance) | resource |
| [aws_launch_template.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/launch_template) | resource |
| [aws_network_interface.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/network_interface) | resource |
Expand All @@ -73,6 +72,7 @@ module "fck-nat" {
|------|-------------|------|---------|:--------:|
| <a name="input_additional_security_group_ids"></a> [additional\_security\_group\_ids](#input\_additional\_security\_group\_ids) | A list of identifiers of security groups to be added for the NAT instance | `list(string)` | `[]` | no |
| <a name="input_ami_id"></a> [ami\_id](#input\_ami\_id) | AMI to use for the NAT instance. Uses fck-nat latest AMI in the region if none provided | `string` | `null` | no |
| <a name="input_attach_ssm_policy"></a> [attach\_ssm\_policy](#input\_attach\_ssm\_policy) | Whether to attach the minimum required IAM permissions to connect to the instance via SSM. | `bool` | `true` | no |
| <a name="input_cloudwatch_agent_configuration"></a> [cloudwatch\_agent\_configuration](#input\_cloudwatch\_agent\_configuration) | CloudWatch configuration for the NAT instance | <pre>object({<br> namespace = optional(string, "fck-nat"),<br> collection_interval = optional(number, 60),<br> endpoint_override = optional(string, "")<br> })</pre> | <pre>{<br> "collection_interval": 60,<br> "endpoint_override": "",<br> "namespace": "fck-nat"<br>}</pre> | no |
| <a name="input_cloudwatch_agent_configuration_param_arn"></a> [cloudwatch\_agent\_configuration\_param\_arn](#input\_cloudwatch\_agent\_configuration\_param\_arn) | ARN of the SSM parameter containing the CloudWatch agent configuration. If none provided, creates one | `string` | `null` | no |
| <a name="input_ebs_root_volume_size"></a> [ebs\_root\_volume\_size](#input\_ebs\_root\_volume\_size) | Size of the EBS root volume in GB | `number` | `2` | no |
Expand Down Expand Up @@ -114,4 +114,4 @@ module "fck-nat" {
| <a name="output_security_group_id"></a> [security\_group\_id](#output\_security\_group\_id) | Deprecated. The ID of the security group used by fck-nat ENIs |
| <a name="output_security_group_ids"></a> [security\_group\_ids](#output\_security\_group\_ids) | List of security group IDs used by fck-nat ENIs |
| <a name="output_subnet_id"></a> [subnet\_id](#output\_subnet\_id) | Subnet ID to which the fck-nat instance is deployed into |
| <a name="output_vpc_id"></a> [vpc\_id](#output\_vpc\_id) | VPC ID to which the fck-nat instance is deployed into |
| <a name="output_vpc_id"></a> [vpc\_id](#output\_vpc\_id) | VPC ID to which the fck-nat instance is deployed into |
24 changes: 19 additions & 5 deletions iam.tf
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,25 @@ data "aws_iam_policy_document" "main" {
}
}
}

dynamic "statement" {
for_each = var.attach_ssm_policy ? ["x"] : []

content {
sid = "SessionManager"
effect = "Allow"
actions = [
"ssmmessages:CreateDataChannel",
"ssmmessages:OpenDataChannel",
"ssmmessages:CreateControlChannel",
"ssmmessages:OpenControlChannel",
"ssm:UpdateInstanceInformation",
]
resources = [
"*"
]
}
}
}

resource "aws_iam_role" "main" {
Expand All @@ -119,9 +138,4 @@ resource "aws_iam_role" "main" {
}

tags = var.tags
}

resource "aws_iam_role_policy_attachment" "ssm" {
role = aws_iam_role.main.name
policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
}
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ variable "eip_allocation_ids" {
default = []
}

variable "attach_ssm_policy" {
description = "Whether to attach the minimum required IAM permissions to connect to the instance via SSM."
type = bool
default = true
}

variable "use_spot_instances" {
description = "Whether or not to use spot instances for running the NAT instance"
type = bool
Expand Down

0 comments on commit 6883f63

Please sign in to comment.