This repository has been archived by the owner on Jul 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add user_data_write_files and user_data_runcmd (#19)
- Loading branch information
Showing
8 changed files
with
234 additions
and
72 deletions.
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
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 @@ | ||
[Unit] | ||
Description = DNAT via ENI eth1 | ||
|
||
[Service] | ||
ExecStart = /opt/nat/dnat.sh | ||
Type = simple | ||
Restart = always | ||
|
||
[Install] | ||
WantedBy = multi-user.target |
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,25 @@ | ||
#!/bin/bash -x | ||
|
||
region="$(/opt/aws/bin/ec2-metadata -z | sed 's/placement: \(.*\).$/\1/')" | ||
eth1_addr="$(ip -f inet -o addr show dev eth1 | cut -d' ' -f 7 | cut -d/ -f 1)" | ||
|
||
function get_instance_private_ip_by_name() { | ||
local name="$1" | ||
aws ec2 describe-instances \ | ||
--region "$region" \ | ||
--filters "Name=tag:Name,Values=$name" "Name=instance-state-name,Values=running" | | ||
jq -r .Reservations[0].Instances[0].PrivateIpAddress | ||
} | ||
|
||
function run_iptables() { | ||
local action="$1" | ||
iptables -t nat "$action" PREROUTING 1 -m tcp -p tcp \ | ||
--dst "$eth1_addr" --dport 80 \ | ||
-j DNAT --to-destination "$(get_instance_private_ip_by_name ${ec2_name}):80" | ||
} | ||
|
||
run_iptables -I | ||
while true; do | ||
sleep 30 | ||
run_iptables -R | ||
done |
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,93 @@ | ||
# an example instance in the private subnet | ||
resource "aws_instance" "private_instance" { | ||
ami = data.aws_ami.amazon_linux_2.id | ||
instance_type = "t3.micro" | ||
iam_instance_profile = aws_iam_instance_profile.private_instance.name | ||
subnet_id = module.vpc.private_subnets[0] | ||
vpc_security_group_ids = [aws_security_group.private_instance.id] | ||
|
||
tags = { | ||
Name = "example-terraform-aws-nat-instance" | ||
} | ||
|
||
user_data = <<EOF | ||
#!/bin/bash | ||
yum install -y httpd | ||
systemctl start httpd | ||
EOF | ||
} | ||
|
||
resource "aws_security_group" "private_instance" { | ||
name = "example-terraform-aws-nat-instance" | ||
description = "expose http service" | ||
vpc_id = module.vpc.vpc_id | ||
ingress { | ||
protocol = "tcp" | ||
from_port = 80 | ||
to_port = 80 | ||
security_groups = [module.nat.sg_id] | ||
} | ||
egress { | ||
protocol = "-1" | ||
from_port = 0 | ||
to_port = 0 | ||
cidr_blocks = ["0.0.0.0/0"] | ||
} | ||
} | ||
|
||
# AMI of the latest Amazon Linux 2 | ||
data "aws_ami" "amazon_linux_2" { | ||
most_recent = true | ||
owners = ["amazon"] | ||
filter { | ||
name = "architecture" | ||
values = ["x86_64"] | ||
} | ||
filter { | ||
name = "root-device-type" | ||
values = ["ebs"] | ||
} | ||
filter { | ||
name = "name" | ||
values = ["amzn2-ami-hvm-*"] | ||
} | ||
filter { | ||
name = "virtualization-type" | ||
values = ["hvm"] | ||
} | ||
filter { | ||
name = "block-device-mapping.volume-type" | ||
values = ["gp2"] | ||
} | ||
} | ||
|
||
# enable SSM access | ||
resource "aws_iam_instance_profile" "private_instance" { | ||
role = aws_iam_role.private_instance.name | ||
} | ||
|
||
resource "aws_iam_role" "private_instance" { | ||
assume_role_policy = <<EOF | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Effect": "Allow", | ||
"Principal": { | ||
"Service": "ec2.amazonaws.com" | ||
}, | ||
"Action": "sts:AssumeRole" | ||
} | ||
] | ||
} | ||
EOF | ||
} | ||
|
||
resource "aws_iam_role_policy_attachment" "ssm" { | ||
policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore" | ||
role = aws_iam_role.private_instance.name | ||
} | ||
|
||
output "private_instance_id" { | ||
value = aws_instance.private_instance.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
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