in setup/setup_fabric_environment.py [0:0]
def setup_vpc_endpoint():
print("--------------------------------------------------------")
print ("Checking VPC Endpoints. ")
print("--------------------------------------------------------")
vpcEndpointName=networkDetails['Network']['VpcEndpointServiceName']
response = requests.get('http://169.254.169.254/latest/meta-data/instance-id')
instance_id = response.text
instances = ec2Client.describe_instances(InstanceIds=[instance_id])
if len(instances['Reservations'][0]['Instances']) != 1:
print ("Unable to find instance id when calling EC2! " + instance_id)
#There should only be one instance since we specified the instance id of this instance.
thisEc2Instance = instances['Reservations'][0]['Instances'][0]
#Check to make sure it doesn't already exist
endpoints = ec2Client.describe_vpc_endpoints(Filters=[{'Name': 'service-name', 'Values': [vpcEndpointName]},{'Name': 'vpc-id', 'Values' :[thisEc2Instance['VpcId']]}])
if len(endpoints['VpcEndpoints']) == 1:
print("VPC Endpoint already exists.")
subnetsToAdd=[]
securityGroupsToAdd=[]
if thisEc2Instance['SubnetId'] not in endpoints['VpcEndpoints'][0]['SubnetIds']:
print ("VPC Endpoint does not include this machines subnet. Adding it now.")
subnetsToAdd=[thisEc2Instance['SubnetId']]
hasSgs=False
for endPointSg in endpoints['VpcEndpoints'][0]['Groups']:
if endPointSg in thisEc2Instance['SecurityGroups']:
hasSgs = True
break
if hasSgs == False:
print("VPC Endpoint doesn't have a shared SG. Adding now. ")
securityGroupsToAdd=[thisEc2Instance['SecurityGroups'][0]['GroupId']]
if len(subnetsToAdd) != 0 or len(securityGroupsToAdd) != 0:
ec2Client.modify_vpc_endpoint(VpcEndpointId=endpoints['VpcEndpoints'][0]['VpcEndpointId'],
AddSubnetIds=subnetsToAdd,
AddSecurityGroupIds=securityGroupsToAdd)
update_vpc_security_group(thisEc2Instance['SecurityGroups'][0]['GroupId'])
waitTillVPCEndpointIsReady(vpcEndpointName, thisEc2Instance['VpcId'])
print("--------------------------------------------------------")
else:
vpcCreateResponse = ec2Client.create_vpc_endpoint(
VpcEndpointType='Interface',
VpcId=thisEc2Instance['VpcId'],
ServiceName=vpcEndpointName,
SubnetIds=[thisEc2Instance['SubnetId']],
SecurityGroupIds=[thisEc2Instance['SecurityGroups'][0]['GroupId']],
ClientToken=randomString(),
PrivateDnsEnabled=True
)
update_vpc_security_group(thisEc2Instance['SecurityGroups'][0]['GroupId'])
waitTillVPCEndpointIsReady(vpcEndpointName, thisEc2Instance['VpcId'])
print("--------------------------------------------------------")
print("Successfully created VPC Endpoint. VPC Endpoint is : " + vpcCreateResponse['VpcEndpoint']['VpcEndpointId'] + ". Waiting till its ready")
print("--------------------------------------------------------")