Introduction
Having got the basic networking in place with public and private subnets, there is one more networking ingredient that is required. If you want to be able to use many of the AWS services in your private subnets, instances in these subnets need internet access.In this post, I'll create a NAT instances to give internet access to private subnets.
To build the NAT instance I'll use Cloudformation, and then I'll create a Powershell script to create or replace the NAT instance.
NAT instance
You can create an EC2 NAT instance by using one of the community NAT AMIs - I'm sure that's OK, but the one I happened to pick didn't work, and using this approach is a lot more flexible, and just as easy.Amazon provide a very good article on how to set up high availability NAT. What I'm doing here is quite a bit simpler - it will not give the high availability of the Amazon solution, but will allow a fairly rapid replacement of a malfunctioning NAT instance (probably 10 minutes rather than 10 seconds). I also needed to NAT an incoming port, forwarding it to another instance, meaning I could only have one working instance.
Cloudformation
Cloudformation is a fantastic service. There is no cost associated with Cloudformation - you pay for what you create. In this case I'm creating an EC2 instance and will pay the hourly rate as soon as it starts. With Cloudformation you use a template to create a Cloudformation "stack".The stack I'll create here contains an EC2 instance doing NAT and a security group.
The template I use is quite simple, and is easy to deconstruct and see how templates work. If you want to, feel free to download the template from here, modify it, and use Cloudformation to build it. If you are familiar with linux and bash, you will see how "UserData" in the instance properties can be used to do virtually any configuration of a linux instance. A note here - the template is in JSON format, which can be very easy to get wrong. I use Notepad ++ in a Windows environment, and add the JSON viewer plug-in. This allows you to select your whole template and quickly verify if the JSON is OK.
The instance you create with this template may be included in the free tier, costing nothing. However, it's a pretty useless instance on it's own - without modification you won't be able to access it except from other instances in your VPC. To allow external access, make a second copy of the line:
{ "IpProtocol" : "tcp", "FromPort" : {"Ref" : "ForwardPort" }, "ToPort" : {"Ref" : "ForwardPort" }, "CidrIp" : "0.0.0.0/0" } ,
replacing "ForwardPort" with "SSHPort", and preferably locking down the CidrIp to your own IP address.
Once the stack has been created, I need to make it work. In my previous post I mentioned the two routing tables. Once I have the NAT instance in place, I create a routing table with the NAT instance as default route, and I associate this routing table with the Private subnets.
PowerShell
I use a Powershell script to create or replace the NAT instance. This script can be called by a monitoring server, or manually.PowerShell may seem an odd choice - while the PowerShell module for AWS is very good, I'm pretty sure it's development is behind the normal AWS CLI (Command Line Interface), that can be used from any platform. The script would have been very easy to create in bash, python or whatever. There are two reasons I'm using PowerShell - the first is that I quite like PowerShell, but the real reason is that I'm currently building a Windows environment. In a future post I hope to share my script for creating a full mirrored SQL server environment. For this you really do need PowerShell, so I'd rather give my clients a consistent tool set.
The script can be used to create an initial NAT instance, as well as to replace it, either after updating the template, to change the parameters, or if the instance stops working as expected.
To use the script, ensure you have initialised your AWS settings in Powershell as explained in a previous post. Change to the directory where the script is saved. Upload the template to an S3 bucket (if you have a local copy, you can just use the following PowerShell commands):
New-S3Bucket <uniquebucketname>
Write-S3Object -File NATInstance.template -BucketName <uniquebucketname> -Key NatInstance.template
After this, you can use the template you have uploaded with the script. You can get the url of the file from the AWS console, or use https://s3-eu-west-1.amazonaws.com/uniquebucketname/NATInstance.template (or similar, depending on your region).
To create an initial NAT instance that serves purely to allow outgoing internet access, make sure at least one subnet has "auto-assign Public IP" enabled, and you have created a second routing table (you don't need to have anything in the routing table). Then simply run:
./Replace-NAT.ps1 -NatTemplateURL https://.....
This should create a NAT instance. For private subnets to start using it, associate your private subnets with this routing table.
If you need to use incoming NAT on a specific port, you can create and assign an EIP to this instance (that allows you to retain the same IP address for future use).
You can replace the instance at any time, whether because of issues, or because you want to make changes (e.g. to implement incoming NAT you can simply add the -ForwardHost and -ForwardPort parameters). The script will, by default, replace the route and delete the previous stack (after confirming with you, or without confirmation if you specify -Force).
For more information on the script (help is very limited, but at least it will list available parameters if you don't feel like editing the script), run
Get-Help ./Replace-Nat.ps1
That's all for this post - next post I'm planning on showing an alternative way to customise a linux instance.
Nice article on AWS cloud formation, thanks for providing useful info!
ReplyDeleteBest Regards,
AWS Online Training
AWS Training
Amazon Web Services Online Training in Hyderabad
AWS Online Training in Hyderabad
AWS Certification Online Training
AWS Training Online
AWS Certification Training
AWS Training and Certification
Today i learn new thing through your blog thanks for sharing for more updates AWS Online Training Bangalore
ReplyDelete