Skip to content

Commit ffd536d

Browse files
author
skpathak2
committedApr 2, 2021
EMR AL2 refactor
1 parent f029abf commit ffd536d

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed
 

‎EMR/Assign_Private_IP/README.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@ This is a Python script that can be used as a bootstrap action or as an EMR step
44

55
What does this script do:
66

7-
- takes the private IP address as its argument
7+
- takes the private IP address and region as its argument
88
- associate that IP to the eth0 interface of the master node
99
- setup the necessary network configuration to ensure that all the traffic is redirected from the secondary to the primary IP address
10+
11+
Steps to execute this script:
12+
13+
- Please confirm that your AWS Identity and Access Management (IAM) policy allows permissions for EMR_EC2_DefaultRole and ec2:AssignPrivateIpAddresses.
14+
- Download the assign_private_ip_region.py script from awslabs github repo.
15+
- Save the script in an Amazon Simple Storage Service (Amazon S3) bucket.
16+
- Specify the script as a [custom bootstrap action](https://docs.aws.amazon.com/emr/latest/ManagementGuide/emr-plan-bootstrap.html#bootstrapCustom) while launching an Amazon EMR cluster. You can also run the script as an [Amazon EMR step](https://docs.aws.amazon.com/emr/latest/ManagementGuide/emr-work-with-steps.html#emr-add-steps). The script requires an argument, which is a private IP address from the CIDR range of your subnet and the region. The script attaches that private IP address to the network interface (eth0) of the master node. The script also configures the network settings to redirect all traffic from the secondary IP address to the primary IP address.
17+
eg
18+
- From bash shell (on master node)
19+
s3://<bucekt>/assign_private_ip.py 172.31.45.7 us-east-1
20+
21+
- Using BA
22+
Script location:- s3://<s3 bucekt>/assign_private_ip.py
23+
Optional arguments:- 172.31.45.13 us-east-1
24+
25+
- To find the new IP address, open the Amazon Elastic Compute Cloud (Amazon EC2) console. Then, select the EC2 instance that's acting as the master node of the EMR cluster. The new IP address appears on the Description tab, in the Secondary private IPs field.

‎EMR/Assign_Private_IP/assign_private_ip.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,23 @@
1717

1818
if is_master == "true":
1919
private_ip = str(sys.argv[1])
20+
region = str(sys.argv[2])
2021
instance_id = subprocess.check_output(['/usr/bin/curl -s http://169.254.169.254/latest/meta-data/instance-id'], shell=True)
21-
interface_id = subprocess.check_output(['aws ec2 describe-instances --instance-ids %s | jq .Reservations[].Instances[].NetworkInterfaces[].NetworkInterfaceId' % instance_id], shell=True).strip().strip('"')
22+
interface_id = subprocess.check_output(['aws ec2 describe-instances --region %s --instance-ids %s | jq .Reservations[].Instances[].NetworkInterfaces[].NetworkInterfaceId' % (region, instance_id)], shell=True).strip().strip('"')
2223

2324
#Assign private IP to the master instance:
24-
subprocess.check_call(['aws ec2 assign-private-ip-addresses --network-interface-id %s --private-ip-addresses %s' % (interface_id, private_ip)], shell=True)
25+
subprocess.check_call(['aws ec2 assign-private-ip-addresses --region %s --network-interface-id %s --private-ip-addresses %s' % (region, interface_id, private_ip)], shell=True)
2526

26-
subnet_id = subprocess.check_output(['aws ec2 describe-instances --instance-ids %s | jq .Reservations[].Instances[].NetworkInterfaces[].SubnetId' % instance_id], shell=True).strip().strip('"').strip().strip('"')
27+
subnet_id = subprocess.check_output(['aws ec2 describe-instances --region %s --instance-ids %s | jq .Reservations[].Instances[].NetworkInterfaces[].SubnetId' % (region, instance_id)], shell=True).strip().strip('"').strip().strip('"')
2728

28-
subnet_cidr = subprocess.check_output(['aws ec2 describe-subnets --subnet-ids %s | jq .Subnets[].CidrBlock' % subnet_id], shell=True).strip().strip('"')
29+
subnet_cidr = subprocess.check_output(['aws ec2 describe-subnets --region %s --subnet-ids %s | jq .Subnets[].CidrBlock' % (region, subnet_id)], shell=True).strip().strip('"')
2930
cidr_prefix = subnet_cidr.split("/")[1]
3031

3132
#Add the private IP address to the default network interface:
3233
subprocess.check_call(['sudo ip addr add dev eth0 %s/%s' % (private_ip, cidr_prefix)], shell=True)
3334

3435
#Configure iptablles rules such that traffic is redirected from the secondary to the primary IP address:
35-
primary_ip = subprocess.check_output(['/sbin/ifconfig eth0 | grep \'inet addr:\' | cut -d: -f2 | awk \'{ print $1}\''], shell=True).strip()
36+
primary_ip = subprocess.check_output(['/usr/bin/curl -s http://169.254.169.254/latest/meta-data/local-ipv4'], shell=True, universal_newlines=True).strip()
3637
subprocess.check_call(['sudo iptables -t nat -A PREROUTING -d %s -j DNAT --to-destination %s' % (private_ip, primary_ip)], shell=True)
3738
else:
3839
print "Not the master node"

0 commit comments

Comments
 (0)
Please sign in to comment.