Terraform, Windows, Userdata with a batch script - amazon-ec2

I have a batch script called Windows_network_adp.bat. The contents are:
<script>
netsh interface ipv4 set address name="Ethernet 3" static 10.*.*.* 255.*.*.*
</script>
It basically adds a static IP to the 2nd NIC on a Windows EC2. This batch file works if i manually paste the contents into the AWS Console user data section. I want it to come in automatically with terraform.
I have tried the following so far:
1st Try
user_data_base64 = base64encode(<<EOF
<script>
netsh interface ipv4 set address name="Ethernet 3" static 10.*.*.* 255.*.*.*
</script>
EOF
)
2nd Try
user_data_base64 = base64encode(file("build/Windows_network_adp.bat"))
No errors on terraform apply. but when i remote into the windows ec2 the network adaptor is still set to dhcp and my static entry has not been applied.
Any help is greatly appreciated.

So Just to Update problem solved.
The syntax of how to do user data in this instance is below. unfortunately the problem was the 2nd interface is being created in terraform after the Windows EC2 therefore my userdata configuring a static IP on the 2nd NIC is irrelevant as it isn't created yet. No base 64 encoding is needed for user data as i have a powershell script now doing another function. There isn't much information on userdata for a batch script out there so i hope this helps other people and prevent them going down a rabbit hole like we did.
user_data = <<EOF
<script>
netsh interface ip set address "Ethernet 3" static "10.*.*.*" "*.*.*.*"
</script>
EOF

Related

set browser to open local address instead of internet address

I use 2 NICs card on my windows, one of the local and another one is connected to the internet, now my question is how can I set the specific browser to open just the Local website instead of the internet? I set the route command to route the IP address that I want to open locally but when I check the logs I find the IP open with internet NIC instead of Local NIC
There two methods of getting this done as per your request the first method would work for you:-
The first method:-
we can use static route route but we have to obtain the IDs for our NICs by running the command below:-
netsh interface ipv4 show interfaces
The above command will get you the NIC ID
Add a persistent route by using the command below in CMD this will lock the specified traffic from a specific network to the specified network interface using the NIC IDx see syntax command below via CMD.
route -p add local_subnet mask local_subnet_mask 172.132.45.201 IF
interface_ID
For example command below, please change the values as per your environment or network:-
route -p add 192.168.0.6 mask 255.255.255.255 172.132.45.201 IF 13
The Second Method:
You will need to edit your hosts file that will enable you to override the DNS for a domain on a specific machine.
Modifying your hosts file causes your machine to look directly at the IP address that you specify
Modifying the hosts file involves adding entries to it to the file . Each entry contains the IP address to which you want the site to resolve.
192.168.190.4 www.local_domain.com
192.168.190.4 local_domain.com
13.14.15.16 www.external_domain.com
13.14.15.16 external_domain.com
Below are the steps to edit the hosts file in Windows 10/11 and Windows Server 2012 and newer versions (you didn't specify which version of Windows).
Press the Windows key.
Type Notepad in the search field.
In the search results, right-click Notepad and select Run as administrator.
From Notepad, open the following file: c:\Windows\System32\Drivers\etc\hosts
Make the necessary changes to the file.
Select File > Save to save your changes.
For Windows, you may have to reboot your computer and this will work independently of the NIC setup.

Anyone used Unified Write Filter (uwfmgr) and netsh together? I'm seeing some off things after running netsh and then rebooting

I've have a Win10IoT Enterprise image I've built.
It is running with Unified Write Filter enabled on the system (C:) volume.
I have the following registry exclusions added to the system disk (the only protected volume) to allow changing IP addresses via netsh to persist and be saved through the filter.
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class{4d36e972-e325-11ce-bfc1-08002be10318}
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces
When I boot the machine with the uwf filter ON I do the following actions....
Run netsh to set a static IP, Subnet, and gateway
Verify the commanbd worked with ipconfig
Check the NIC card GUI in Windows and also verify it looks good there.
Everything at this point looks great. I now reboot the machine (uwf filter still on).
When the machine reboots I check the IP address again via ipconfig and the NIC GUI to verify they are still good.
This is when things get odd. The ipconfig looks fine, but in the GUI window the IP is listed as the last static IP address I set with the uwf filter OFF (192.168.0.20 in this example). If I click the advanced settings there are two address listed under the IP Addresses, my expected 192.168.0.50 address as well as the old 192.168.0.20.
Some more info, the machine will respond to pings to 192.168.0.50 but will not respond to 192.168.0.20. I'm guessing there is another registry setting somewhere that is getting reverted causing the GUI to not update correctly. Anyone have any ideas?
Thanks!
Post reboot images....
I had the same problem.
But when I add HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Nsi to my exclusion list, it works

Can you execute an offline/local program from aws lambda?

I'm playing around with lambda functions and I was wondering if there was a way to execute a a script on my windows pc from the lambda function. I'm assuming I need to send a request to my windows pc somehow to execute the script, but I'm not an expert here. Any pointers would be awesome, sorry if it's a dumb question.
Thanks
Here is how it is possible:
Your PC with NAT Exposed public IP and port to connect and run the script by a webserver or any other service.
From Lambda you connect to your machine with public ip and run the desired action needed.
If you don't have a fixed IP on your server you can use public dns service to create a dns entry that keeps constantly updating your ip address to that service, this way if your IP changes, you will not have any issues connecting to your machine.
Hope it helps.

Magento base_url reset every time I reboot the EC2 instance

Looks like there is a script running every time I reboot the Magento EC2 instance. How do I stop the script or change it to something else?
Thanks
this script updates the IP address. The tool is called "updateip" and it is in "/opt/bitnami" folder. You can use it to set your hostname or you can simply rename it to avoid to change your IP address.
and more.

Solution for local ip changes of AWS EC2 instances

Amazon only gives you a certain number of static ip address and the local (private) ips of each EC2 instance can change when the machine is restarted. This makes creating a stable platform where EC2 instances depend on each other ridiculously hard to use as far as I can tell.
I've search online a lot about various solutions and so far have found nothing reasonable outside of assigning an elastic ip address on ever EC2 even if its not public facing. Does anyone have any other good ideas that is actually easy to execute on?
Thanks!
See the AWS team's response to question Static local IP:
The internal IP address of EC2 instances is allocated via DHCP. On
instance shutdown, or when the DHCP lease expires, the IP address is
returned to the general EC2 DHCP pool of addresses available for other
instances.
There is no way to guarantee that you will obtain the same DHCP
address across reboots.
Edit: The answer is to use Amazon VPC. There is no downside except a trivial amount of extra setup because now you control the router. It's a world apart from plain old EC2 instance on AWS. It's so necessary in fact that VPC will be enabled for all future AWS setups by default. See this post for more information: http://www.reddit.com/r/aws/comments/1a3n0r/ec2_update_virtual_private_clouds_for_everyone/
The stock answers are:
Use AWS VPC so you have complete control over instance addressing
Use Elastic IPs, which will resolve to the instance's local address (not the public, as you'd expect) when used to communicate between EC2 instances
I stumbled upon third option. There's ec2-ssh by the Instragram folks. It's a python shell script that you install globally and lets you both query the public dns of your ec2 instances by tag name and also ssh in via tag name as well.
The documentation for it is virtually nonexistent. I've written down the steps to install below:
To install ec2-ssh:
sudo yum install python-boto (python wrapper for ec2 api)
git clone https://github.com/Instagram/ec2-ssh
In your ~/.bash_profile set your AWS access key and secret like so:
export AWS_ACCESS_KEY_ID=XYZ123
export AWS_SECRET_ACCESS_KEY=XYZ123
cd into the bin folder of the repo, there will be two files:
ec2-host and ec2-ssh
copy them to your /usr/bin or /usr/local/bin.
Now you can do awesome stuff like:
$ ec2-host ZenWorker
ec2-999-xy-999-99.compute-1.amazonaws.com
and
$ ec2-ssh ZenWorker
Connecting to ec2-999-xy-999-99.compute-1.amazonaws.com.
Note that in your regular shell scripts you can use backticks to call these global tools. I've timed these calls and they take between 0.25 and 0.5 second using an EC2 instance, so that's really the only downside. Perhaps you can live with the delay, or use the fact that public DNS only changes for an instance on reboot to work up a solution.
Note that these two programs are commandline scripts and you don't need any Python knowledge to use them. For PHP fans, or those that also want an easy way to scp files without knowing the changing public DNS, you can checkout ec2dns.
I was in the same situation once. I still dont have the expertise to solve it properly. My ugly solution was to use elb not really for load balancing but just for the endpoint.
But I think a good solution can be obtained by using aws vpc.
Here's another Ruby solution for Updating Route 53 DNS from instance on AWS. You shouldn't reference raw 3rd party system IP addresses in your applications or server configurations.
you can change Ip Address using Elastic Ip:
You Can Do Using C# Code:
var associateRequest = new AssociateAddressRequest
{
PublicIp = your Elastic Ip,
InstanceId = Your Instance Id Which You Assign
};
amazonEc2Client.AssociateAddress(associateRequest);
after That DeAssociate It.
var disAssociateRequest = new isassociateAddressRequest(publicIp.ElasticIpAddress1);
AmazonEc2Client.DisassociateAddress(your Elastic Ip);
your Public Ip Will Change

Resources