-
Notifications
You must be signed in to change notification settings - Fork 20
possibility of using nat instance, which can be multi-az or just one nat instance per region. #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jrpradojr
wants to merge
35
commits into
master
Choose a base branch
from
nat_gw
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.
Open
Changes from 34 commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
64e38dd
Option to not create nat gw
jrpradojr 5e97329
Added option to choose NAT Gateway or Instance
jrpradojr dd0180d
Update init.sh
jrpradojr 378e1c2
Fix data
jrpradojr 3e71f50
Fix data
jrpradojr 2b5e6a5
Fix subnet private
jrpradojr 75d06d1
TODO correct nat cloudformation_stack
jrpradojr 6ce8200
Fix nat_instance
jrpradojr d612d10
Fix nat_instance
jrpradojr 2965208
Fixed version
jrpradojr 6d7c51b
Fixed nat policy
jrpradojr 431fe30
Final version
jrpradojr 3a90d51
Disable source dest check
jrpradojr acf755e
Ajust init.sh
jrpradojr 26e5c47
Update AMI and init.sh
jrpradojr 4430073
Fixed sg when nat_instance=true and multi_az=false
jrpradojr f715978
Fixed role and profile names
jrpradojr 90ba0d9
Update _data.tf
jrpradojr 4e12915
changed instance type to graviton (arm)
jrpradojr 04484f4
Update _variables.tf
jrpradojr ba7f3ee
Update init.sh
jrpradojr f5e3fa6
Update _variables.tf
jrpradojr 767e500
Update _variables.tf
jrpradojr fbe6bfd
Update _data.tf
jrpradojr b9c8e1b
Update _variables.tf
jrpradojr e0d67ff
Added option to enable or disable vpc flow logs
2c4a20e
Updated the Readme
723befe
Fixed vpc-flow-logs
cdd137b
Fixed interpolation
537f932
Updated the Readme
jrpradojr 061a26e
Fixed vpc-flow-logs
jrpradojr 000eb8f
Merge branch 'master' into nat_gw
jrpradojr 2c22d51
Merged from master
jrpradojr 5580200
Removed expressions deprecated from files
jrpradojr 1246ccf
Changed test connection url to http://www.google.com
jrpradojr 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,8 +1,41 @@ | ||
| data "aws_availability_zones" "available" {} | ||
| data "aws_availability_zones" "available" { | ||
| state = "available" | ||
| } | ||
|
|
||
| output "aws_availability_zones" { | ||
| value = data.aws_availability_zones.available.names | ||
| description = "aws_availability_zones" | ||
| } | ||
|
|
||
| data "aws_availability_zone" "az" { | ||
| count = length(data.aws_availability_zones.available.names) | ||
| name = data.aws_availability_zones.available.names[count.index] | ||
| } | ||
|
|
||
| data "aws_region" "current" {} | ||
|
|
||
| # AMI of the latest Amazon Linux 2 | ||
| data "aws_ami" "amazon_linux" { | ||
| most_recent = true | ||
| owners = ["amazon"] | ||
| filter { | ||
| name = "architecture" | ||
| values = var.nat_architecture | ||
| } | ||
| 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"] | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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,95 @@ | ||
| #! /bin/bash | ||
| set -x | ||
|
|
||
| echo "### INSTALL PACKAGES" | ||
| yum update -y | ||
| yum install -y amazon-efs-utils aws-cli | ||
|
|
||
| echo "### INSTALL SSM AGENT" | ||
| cd /tmp | ||
| yum install -y https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/linux_amd64/amazon-ssm-agent.rpm | ||
|
|
||
| echo "### CONFIG NETWORK INTERFACE" | ||
| echo "### Determine the region" | ||
| export AWS_DEFAULT_REGION="$(/opt/aws/bin/ec2-metadata -z | sed 's/placement: \(.*\).$/\1/')" | ||
|
|
||
| echo "### Determine the instance id" | ||
| instance_id="$(/opt/aws/bin/ec2-metadata -i | cut -d' ' -f2)" | ||
|
|
||
| echo "### Disable source dest check" | ||
| aws ec2 modify-instance-attribute --instance-id "$instance_id" --source-dest-check "{\"Value\": false}" | ||
|
|
||
| echo "### Determine the count of EIP id" | ||
| eip_id="$(aws ec2 describe-addresses --query Addresses[*].AllocationId --filters "Name=tag:Function,Values=NAT-instance" --output text)" | ||
|
|
||
| if [ $(echo "$eip_id" |wc -w) -eq 1 ]; then | ||
| echo "### Attach the EIP" | ||
| aws ec2 associate-address --instance-id "$instance_id" --allocation-id "$eip_id" | ||
|
|
||
| echo "### Change the private route tables" | ||
| route_tables="$(aws ec2 describe-route-tables --query RouteTables[*].RouteTableId --filters "Name=tag:Scheme,Values=private" --output text)" | ||
| for route_id in $(echo "$route_tables") | ||
| do | ||
| route_internet="$(aws ec2 describe-route-tables --route-table-ids "$route_id" |grep -c "0.0.0.0/0")" | ||
| if [ "$route_internet" -eq 0 ] | ||
| then | ||
| aws ec2 create-route --route-table-id "$route_id" --destination-cidr-block 0.0.0.0/0 --instance-id "$instance_id" | ||
| else | ||
| aws ec2 replace-route --route-table-id "$route_id" --destination-cidr-block 0.0.0.0/0 --instance-id "$instance_id" | ||
| fi | ||
| done | ||
|
|
||
| echo "### enable IP forwarding and NAT" | ||
| sysctl -q -w net.ipv4.ip_forward=1 | ||
| sysctl -q -w net.ipv4.conf.eth0.send_redirects=0 | ||
| iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE | ||
|
|
||
| echo "### wait for network connection" | ||
| curl --retry 20 http://www.example.com | ||
|
|
||
| echo "### reestablish connections" | ||
| systemctl restart amazon-ssm-agent | ||
|
|
||
| else | ||
| echo "### Determine the network id in the zone" | ||
| timeout=600 | ||
| time_count=0 | ||
| zone_name="$(/opt/aws/bin/ec2-metadata -z | cut -d' ' -f2)" | ||
| eni_id=$(aws ec2 describe-network-interfaces --query NetworkInterfaces[*].NetworkInterfaceId --filters "Name=status,Values=available" "Name=tag:Function,Values=NAT-instance" "Name=availability-zone,Values=$zone_name" --output text) | ||
|
|
||
| while [ -z $eni_id ]; do | ||
| let time_count++ | ||
| sleep 1 | ||
| eni_id=$(aws ec2 describe-network-interfaces --query NetworkInterfaces[*].NetworkInterfaceId --filters "Name=status,Values=available" "Name=tag:Function,Values=NAT-instance" "Name=availability-zone,Values=$zone_name" --output text) | ||
| if [ $time_count -eq $timeout ]; then | ||
| echo "No network interface available to instance" | ||
| shutdown -h now | ||
| fi | ||
| done | ||
|
|
||
| echo "### Attach network interface" | ||
| aws ec2 attach-network-interface \ | ||
| --region "$(/opt/aws/bin/ec2-metadata -z | sed 's/placement: \(.*\).$/\1/')" \ | ||
| --instance-id "$(/opt/aws/bin/ec2-metadata -i | cut -d' ' -f2)" \ | ||
| --device-index 1 \ | ||
| --network-interface-id "$eni_id" | ||
|
|
||
| while ! ip link show dev eth1; do | ||
| sleep 1 | ||
| done | ||
|
|
||
| echo "### enable IP forwarding and NAT" | ||
| sysctl -q -w net.ipv4.ip_forward=1 | ||
| sysctl -q -w net.ipv4.conf.eth1.send_redirects=0 | ||
| iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE | ||
|
|
||
| echo "### switch the default route to eth1" | ||
| ip route del default dev eth0 | ||
|
|
||
| echo "### wait for network connection" | ||
| curl --retry 20 http://www.example.com | ||
|
|
||
| echo "### reestablish connections" | ||
| systemctl restart amazon-ssm-agent | ||
|
|
||
| fi | ||
This file contains hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.