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

Commit 31bcc52

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

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed

modules/ec2-connect-role/README.md

Lines changed: 4 additions & 0 deletions
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

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