This repository has been archived by the owner on Jul 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 97
Ec2 connect tunnel #325
Draft
qrilka
wants to merge
9
commits into
master
Choose a base branch
from
ec2-connect-tunnel
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Ec2 connect tunnel #325
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3e3ad0d
Support multiple data volumes in single-node-asg module.
Magicloud 8e9e53e
New function: single-node-asg module supports binding EIP by itself.
Magicloud a024cbb
Merge remote-tracking branch 'origin/many_ebs' into ec2-connect-tunnel
qrilka 98a15b1
Tunnel using EC2 instance connect
qrilka 31bcc52
Add ec-connect-role module
qrilka 3c17169
Changelog entries for EC2 Instance Connect modules
qrilka 9faa472
Add init_prefix and init_suffix to ec2-instance-connect
qrilka 06471e5
remove sg rule and exporting values
JoseD92 dd4700d
Fix ec2-connect-tunnel outputs
qrilka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
provider "aws" { | ||
region = "ap-northeast-1" | ||
} | ||
|
||
data "aws_availability_zones" "azs" {} | ||
|
||
module "vpc" { | ||
source = "fpco/foundation/aws//modules/vpc-scenario-2" | ||
cidr = "192.168.0.0/16" | ||
public_subnet_cidrs = ["192.168.0.0/24", "192.168.1.0/24"] | ||
private_subnet_cidrs = ["192.168.100.0/24", "192.168.101.0/24"] | ||
azs = data.aws_availability_zones.azs.names | ||
name_prefix = "ebs-test" | ||
region = "ap-northeast-1" | ||
} | ||
|
||
module "ubuntu" { | ||
source = "fpco/foundation/aws//modules/ami-ubuntu" | ||
} | ||
|
||
resource "aws_security_group" "ssh" { | ||
vpc_id = module.vpc.vpc_id | ||
ingress { | ||
from_port = 22 | ||
to_port = 22 | ||
protocol = "tcp" | ||
cidr_blocks = ["0.0.0.0/0"] | ||
} | ||
egress { | ||
from_port = 0 | ||
to_port = 0 | ||
protocol = "-1" | ||
cidr_blocks = ["0.0.0.0/0"] | ||
} | ||
} | ||
|
||
module "tester" { | ||
source = "../../modules/single-node-asg" | ||
name_prefix = "ebs" | ||
name_suffix = "test" | ||
key_name = "tokyo" | ||
ami = module.ubuntu.id | ||
instance_type = "t2.micro" | ||
subnet_id = module.vpc.public_subnet_ids[0] | ||
security_group_ids = [aws_security_group.ssh.id] | ||
region = "ap-northeast-1" | ||
compatible_with_single_volume = false | ||
data_volumes = [{ name = "a", device = "/dev/xvdm", size = 50 }, { name = "b", device = "/dev/xvdn" }] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
## EC2 Instance Connect Role | ||
|
||
Creates an IAM role that can be used to connect to EC2 instances using | ||
EC2 Instance Connect e.g. created using the `ec2-connect-tunnel` module. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
data "aws_caller_identity" "current" { | ||
} | ||
|
||
data "aws_iam_policy_document" "ec2-instance-connect" { | ||
statement { | ||
actions = [ | ||
"ec2:DescribeInstances", | ||
] | ||
|
||
resources = ["*"] | ||
} | ||
|
||
statement { | ||
actions = [ | ||
"ec2-instance-connect:SendSSHPublicKey", | ||
] | ||
|
||
resources = [for i in var.instance_ids : "arn:aws:ec2:${var.region}:${var.account_id}:instance/${i}"] | ||
|
||
condition { | ||
test = "StringEquals" | ||
variable = "ec2:osuser" | ||
|
||
values = [ | ||
"ubuntu", | ||
] | ||
} | ||
} | ||
} | ||
|
||
resource "aws_iam_policy" "ec2-instance-connect" { | ||
name = "ec2-instance-connect" | ||
description = "grants permissions to connect to an instance using EC2 Instance Connect" | ||
policy = data.aws_iam_policy_document.ec2-instance-connect.json | ||
} | ||
|
||
module "role" { | ||
source = "../cross-account-role" | ||
name = var.name | ||
trust_account_ids = concat([data.aws_caller_identity.current.account_id], | ||
var.trust_account_ids) | ||
} | ||
|
||
resource "aws_iam_role_policy_attachment" "role_ec2-instance-connect" { | ||
role = module.role.name | ||
policy_arn = aws_iam_policy.ec2-instance-connect.arn | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
output "arn" { | ||
value = module.role.arn | ||
} | ||
|
||
output "name" { | ||
value = module.role.name | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
variable "name" { | ||
description = "Name to give the role" | ||
type = string | ||
} | ||
|
||
variable "trust_account_ids" { | ||
description = "List of other accounts to trust to assume the role" | ||
default = [] | ||
type = list(string) | ||
} | ||
|
||
variable "region" { | ||
description = "The AWS region to deploy to" | ||
type = string | ||
} | ||
|
||
variable "account_id" { | ||
description = "ID of the account which instances to connect to" | ||
type = string | ||
} | ||
|
||
variable "instance_ids" { | ||
description = "IDs of instances to connect to" | ||
type = list(string) | ||
default = ["*"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# EC2 Instance Connect tunnel | ||
|
||
Creates a s single node ASG (using the `singe-node-asg` module) allowing SSH | ||
connections using [EC2 Instance Connect](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Connect-using-EC2-Instance-Connect.html). | ||
Assumes Ubuntu AMI to be used (`ec2-instance-connect` gets installed using | ||
`apt`). Use `ec2-connect-role` to setup an IAM role for SSH access. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# allows connecting with SSM manager | ||
resource "aws_iam_role_policy_attachment" "ssm_instance" { | ||
role = module.asg.asg_iam_role_name | ||
policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module "asg" { | ||
source = "../single-node-asg" | ||
|
||
region = var.region | ||
ami = var.ami | ||
key_name = "" | ||
instance_type = var.instance_type | ||
name_prefix = var.name_prefix | ||
name_suffix = var.name_suffix | ||
|
||
security_group_ids = [module.tunnel-sg.id] | ||
subnet_id = var.subnet_id | ||
data_volumes = [] | ||
assign_eip = true | ||
|
||
init_prefix = var.init_prefix | ||
init_suffix = <<END_INIT_SUFFIX | ||
echo "Installing ec2-instance-connect" | ||
apt install ec2-instance-connect | ||
${var.init_suffix} | ||
END_INIT_SUFFIX | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
output "public_ip" { | ||
value = module.asg.eip_address | ||
description = "Public IP of the tunnel" | ||
} | ||
|
||
output "sg_id" { | ||
value = module.tunnel-sg.id | ||
description = "Security group id of the tunnel" | ||
} | ||
|
||
output "asg_name" { | ||
value = module.asg.name | ||
description = "`name` exported from the Server `aws_autoscaling_group`" | ||
} | ||
|
||
output "asg_iam_role_name" { | ||
value = module.asg.asg_iam_role_name | ||
description = "`name` exported from the Service Data `aws_iam_role`" | ||
} | ||
|
||
output "data_volume_name_tag" { | ||
value = module.asg.data_volume_name_tag | ||
description = "Name tag value for attached data volume." | ||
} | ||
|
||
output "eip_address" { | ||
value = module.asg.eip_address | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module "tunnel-sg" { | ||
source = "../security-group-base" | ||
name = "${var.name_prefix}-sg" | ||
description = "SG for the tunnel ASG" | ||
vpc_id = var.vpc_id | ||
extra_tags = var.extra_tags | ||
} | ||
|
||
# security group rule to open egress (outbound from nodes) | ||
module "allow-open-egress" { | ||
source = "../open-egress-sg" | ||
security_group_id = module.tunnel-sg.id | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
variable "name_prefix" { | ||
description = "Prefix for naming resources, usually project-related" | ||
type = string | ||
} | ||
|
||
variable "name_suffix" { | ||
description = "suffix to include when naming the various resources" | ||
type = string | ||
default = "" | ||
} | ||
|
||
variable "region" { | ||
description = "The AWS region to deploy to" | ||
type = string | ||
} | ||
|
||
variable "ami" { | ||
description = "The base AMI for each AWS instance created" | ||
type = string | ||
} | ||
|
||
variable "instance_type" { | ||
description = "The type of AWS instance (size)" | ||
type = string | ||
} | ||
|
||
variable "vpc_id" { | ||
description = "ID of VPC to associate SG with" | ||
type = string | ||
} | ||
|
||
variable "subnet_id" { | ||
description = "The ID of the subnet to use, depends on the availability zone" | ||
type = string | ||
} | ||
|
||
variable "init_prefix" { | ||
default = "" | ||
description = "init shell to run before executing the main part of instance init" | ||
type = string | ||
} | ||
|
||
variable "init_suffix" { | ||
default = "" | ||
description = "init shell to run after the main part of instance init" | ||
type = string | ||
} | ||
|
||
variable "extra_tags" { | ||
description = "map of name,value pairs to tag the security group (append to Name tag)" | ||
default = {} | ||
type = map(string) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
if which wget; then | ||
INSTANCE_ID="$(wget -O- http://169.254.169.254/latest/meta-data/instance-id)" | ||
elif which curl; then | ||
INSTANCE_ID="$(curl http://169.254.169.254/latest/meta-data/instance-id)" | ||
fi | ||
|
||
if [ "x$${INSTANCE_ID}" == "x" ]; then | ||
echo 'There is no wget or curl tool installed. Hence bootstrap cannot get instance ID.' | ||
exit 1 | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JoseD92 this output already exists as
public_ip