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

Commit 1ac417e

Browse files
committed
Add ec-connect-role module
1 parent 98a15b1 commit 1ac417e

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

modules/ec2-connect-role/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## EC2 Instance Connect Role
2+
3+
Creates an IAM role that can be used to connect to EC2 instances using
4+
EC2 Instance Connect e.g. created using the `ec2-connect-tunnel` module.

modules/ec2-connect-role/main.tf

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
data "aws_iam_policy_document" "ec2-instance-connect" {
2+
statement {
3+
actions = [
4+
"ec2:DescribeInstances",
5+
]
6+
7+
resources = ["*"]
8+
}
9+
10+
statement {
11+
actions = [
12+
"ec2-instance-connect:SendSSHPublicKey",
13+
]
14+
15+
resources = [for i in var.instance_ids : "arn:aws:ec2:${var.region}:${var.account_id}:instance/${i}"]
16+
17+
condition {
18+
test = "StringEquals"
19+
variable = "ec2:osuser"
20+
21+
values = [
22+
"ubuntu",
23+
]
24+
}
25+
}
26+
}
27+
28+
resource "aws_iam_policy" "ec2-instance-connect" {
29+
name = "ec2-instance-connect"
30+
description = "grants permissions to connect to an instance using EC2 Instance Connect"
31+
policy = data.aws_iam_policy_document.ec2-instance-connect.json
32+
}
33+
34+
module "role" {
35+
source = "../cross-account-role"
36+
name = var.name
37+
trust_account_ids = var.trust_account_ids
38+
}
39+
40+
resource "aws_iam_role_policy_attachment" "role_ec2-instance-connect" {
41+
role = module.role.name
42+
policy_arn = aws_iam_policy.ec2-instance-connect.arn
43+
}

modules/ec2-connect-role/outputs.tf

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
output "arn" {
2+
value = module.role.arn
3+
}
4+
5+
output "name" {
6+
value = module.role.name
7+
}

modules/ec2-connect-role/variables.tf

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
variable "name" {
2+
description = "Name to give the role"
3+
type = string
4+
}
5+
6+
variable "trust_account_ids" {
7+
description = "List of other accounts to trust to assume the role"
8+
default = []
9+
type = list(string)
10+
}
11+
12+
variable "region" {
13+
description = "The AWS region to deploy to"
14+
type = string
15+
}
16+
17+
variable "account_id" {
18+
description = "ID of the account which instances to connect to"
19+
type = string
20+
}
21+
22+
variable "instance_ids" {
23+
description = "IDs of instances to connect to"
24+
type = list(string)
25+
default = ["*"]
26+
}

0 commit comments

Comments
 (0)