openvidu-deployment: AWS - wait for the Elastic IP before deriving the domain

NewEIP was created with InstanceId, so the Elastic IP was attached only
after the instance existed, racing with the boot scripts: the domain could
be derived from whatever public IP the instance had at that moment (the
subnet's ephemeral one, or none), and with no public IP there was no
Internet access for apt-get, cfn-init or the installer either.

In the CE single node, Pro single node and Pro elastic templates, NewEIP is
now allocated on its own and attached by a separate NewEIPAssociation, so
its address is known up front. UserData waits up to 10 minutes for the
instance to have that address before doing anything else, and install.sh
reads public-ipv4 instead of public-hostname (empty in VPCs without DNS
hostnames).


Assisted-by: claude-code - Anthropic - Claude Opus 5.5
media-node-health
cruizba 2026-10-06 16:00:49 +02:00
parent 3208097dd4
commit b372409ccf
3 changed files with 191 additions and 122 deletions

View File

@ -706,8 +706,7 @@ Resources:
# Configure Domain # Configure Domain
if [[ "${DomainName}" == '' ]]; then if [[ "${DomainName}" == '' ]]; then
PublicHostname=$(curl -s -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/public-hostname) DOMAIN=$(curl -s -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/public-ipv4)
DOMAIN=$(echo "$PublicHostname" | cut -d'.' -f1 | sed 's/^ec2-//' | tr '-' '.')
else else
DOMAIN=${DomainName} DOMAIN=${DomainName}
fi fi
@ -1100,10 +1099,28 @@ Resources:
- Key: Name - Key: Name
Value: !Ref 'AWS::StackName' Value: !Ref 'AWS::StackName'
UserData: UserData:
Fn::Base64: !Sub | Fn::Base64: !Sub
- |
#!/bin/bash -x #!/bin/bash -x
set -eu -o pipefail set -eu -o pipefail
# Wait for the Elastic IP: CloudFormation associates it only after the instance
# is created, and until then the instance may have another public IP (or none,
# and no Internet access)
TOKEN=$(curl -s -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600")
CURRENT_PUBLIC_IP=""
for _ in $(seq 1 120); do
CURRENT_PUBLIC_IP=$(curl -sf -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/public-ipv4 || true)
if [[ "$CURRENT_PUBLIC_IP" == "${ExpectedPublicIp}" ]]; then
break
fi
sleep 5
done
if [[ "$CURRENT_PUBLIC_IP" != "${ExpectedPublicIp}" ]]; then
echo "[OpenVidu] Elastic IP ${ExpectedPublicIp} not attached after 10 minutes"
exit 1
fi
echo "DPkg::Lock::Timeout \"-1\";" > /etc/apt/apt.conf.d/99timeout echo "DPkg::Lock::Timeout \"-1\";" > /etc/apt/apt.conf.d/99timeout
apt-get update && apt-get install -y \ apt-get update && apt-get install -y \
@ -1143,6 +1160,7 @@ Resources:
# sending the finish call # sending the finish call
cfn-signal -e $? --stack ${AWS::StackId} --resource WaitCondition --region ${AWS::Region} cfn-signal -e $? --stack ${AWS::StackId} --resource WaitCondition --region ${AWS::Region}
- ExpectedPublicIp: !If [PublicElasticIPPresent, !Ref PublicElasticIP, !Ref NewEIP]
BlockDeviceMappings: BlockDeviceMappings:
- DeviceName: /dev/sda1 - DeviceName: /dev/sda1
@ -1161,8 +1179,13 @@ Resources:
NewEIP: NewEIP:
Type: 'AWS::EC2::EIP' Type: 'AWS::EC2::EIP'
Condition: PublicElasticIPAbsent Condition: PublicElasticIPAbsent
NewEIPAssociation:
Type: 'AWS::EC2::EIPAssociation'
Condition: PublicElasticIPAbsent
Properties: Properties:
InstanceId: !Ref OpenviduServer InstanceId: !Ref OpenviduServer
AllocationId: !GetAtt NewEIP.AllocationId
IMDSv2LaunchTemplate: IMDSv2LaunchTemplate:
Type: AWS::EC2::LaunchTemplate Type: AWS::EC2::LaunchTemplate

View File

@ -1410,8 +1410,13 @@ Resources:
NewEIP: NewEIP:
Type: 'AWS::EC2::EIP' Type: 'AWS::EC2::EIP'
Condition: PublicElasticIPAbsent Condition: PublicElasticIPAbsent
NewEIPAssociation:
Type: 'AWS::EC2::EIPAssociation'
Condition: PublicElasticIPAbsent
Properties: Properties:
InstanceId: !Ref OpenViduMasterNode InstanceId: !Ref OpenViduMasterNode
AllocationId: !GetAtt NewEIP.AllocationId
IMDSv2LaunchTemplateMasterNode: IMDSv2LaunchTemplateMasterNode:
Type: AWS::EC2::LaunchTemplate Type: AWS::EC2::LaunchTemplate
@ -1469,8 +1474,7 @@ Resources:
# Configure Domain and other parameters # Configure Domain and other parameters
if [[ "${DomainName}" == '' ]]; then if [[ "${DomainName}" == '' ]]; then
PublicHostname=$(curl -s -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/public-hostname) DOMAIN=$(curl -s -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/public-ipv4)
DOMAIN=$(echo "$PublicHostname" | cut -d'.' -f1 | sed 's/^ec2-//' | tr '-' '.')
else else
DOMAIN=${DomainName} DOMAIN=${DomainName}
fi fi
@ -1940,10 +1944,28 @@ Resources:
- Key: Name - Key: Name
Value: !Sub ${AWS::StackName} - OpenVidu Elastic - Master Node Value: !Sub ${AWS::StackName} - OpenVidu Elastic - Master Node
UserData: UserData:
Fn::Base64: !Sub | Fn::Base64: !Sub
- |
#!/bin/bash #!/bin/bash
set -eu -o pipefail set -eu -o pipefail
# Wait for the Elastic IP: CloudFormation associates it only after the instance
# is created, and until then the instance may have another public IP (or none,
# and no Internet access)
TOKEN=$(curl -s -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600")
CURRENT_PUBLIC_IP=""
for _ in $(seq 1 120); do
CURRENT_PUBLIC_IP=$(curl -sf -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/public-ipv4 || true)
if [[ "$CURRENT_PUBLIC_IP" == "${ExpectedPublicIp}" ]]; then
break
fi
sleep 5
done
if [[ "$CURRENT_PUBLIC_IP" != "${ExpectedPublicIp}" ]]; then
echo "[OpenVidu] Elastic IP ${ExpectedPublicIp} not attached after 10 minutes"
exit 1
fi
echo "DPkg::Lock::Timeout \"-1\";" > /etc/apt/apt.conf.d/99timeout echo "DPkg::Lock::Timeout \"-1\";" > /etc/apt/apt.conf.d/99timeout
apt-get update && apt-get install -y \ apt-get update && apt-get install -y \
@ -2005,6 +2027,7 @@ Resources:
# sending the finish call # sending the finish call
cfn-signal -e $? --stack ${AWS::StackId} --resource WaitCondition --region ${AWS::Region} cfn-signal -e $? --stack ${AWS::StackId} --resource WaitCondition --region ${AWS::Region}
- ExpectedPublicIp: !If [PublicElasticIPPresent, !Ref PublicElasticIP, !Ref NewEIP]
BlockDeviceMappings: BlockDeviceMappings:
- DeviceName: /dev/sda1 - DeviceName: /dev/sda1

View File

@ -808,8 +808,7 @@ Resources:
# Configure Domain # Configure Domain
if [[ "${DomainName}" == '' ]]; then if [[ "${DomainName}" == '' ]]; then
PublicHostname=$(curl -s -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/public-hostname) DOMAIN=$(curl -s -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/public-ipv4)
DOMAIN=$(echo "$PublicHostname" | cut -d'.' -f1 | sed 's/^ec2-//' | tr '-' '.')
else else
DOMAIN=${DomainName} DOMAIN=${DomainName}
fi fi
@ -1217,10 +1216,28 @@ Resources:
- Key: Name - Key: Name
Value: !Ref 'AWS::StackName' Value: !Ref 'AWS::StackName'
UserData: UserData:
Fn::Base64: !Sub | Fn::Base64: !Sub
- |
#!/bin/bash -x #!/bin/bash -x
set -eu -o pipefail set -eu -o pipefail
# Wait for the Elastic IP: CloudFormation associates it only after the instance
# is created, and until then the instance may have another public IP (or none,
# and no Internet access)
TOKEN=$(curl -s -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600")
CURRENT_PUBLIC_IP=""
for _ in $(seq 1 120); do
CURRENT_PUBLIC_IP=$(curl -sf -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/public-ipv4 || true)
if [[ "$CURRENT_PUBLIC_IP" == "${ExpectedPublicIp}" ]]; then
break
fi
sleep 5
done
if [[ "$CURRENT_PUBLIC_IP" != "${ExpectedPublicIp}" ]]; then
echo "[OpenVidu] Elastic IP ${ExpectedPublicIp} not attached after 10 minutes"
exit 1
fi
echo "DPkg::Lock::Timeout \"-1\";" > /etc/apt/apt.conf.d/99timeout echo "DPkg::Lock::Timeout \"-1\";" > /etc/apt/apt.conf.d/99timeout
apt-get update && apt-get install -y \ apt-get update && apt-get install -y \
@ -1260,6 +1277,7 @@ Resources:
# sending the finish call # sending the finish call
cfn-signal -e $? --stack ${AWS::StackId} --resource WaitCondition --region ${AWS::Region} cfn-signal -e $? --stack ${AWS::StackId} --resource WaitCondition --region ${AWS::Region}
- ExpectedPublicIp: !If [PublicElasticIPPresent, !Ref PublicElasticIP, !Ref NewEIP]
BlockDeviceMappings: BlockDeviceMappings:
- DeviceName: /dev/sda1 - DeviceName: /dev/sda1
@ -1278,8 +1296,13 @@ Resources:
NewEIP: NewEIP:
Type: 'AWS::EC2::EIP' Type: 'AWS::EC2::EIP'
Condition: PublicElasticIPAbsent Condition: PublicElasticIPAbsent
NewEIPAssociation:
Type: 'AWS::EC2::EIPAssociation'
Condition: PublicElasticIPAbsent
Properties: Properties:
InstanceId: !Ref OpenviduServer InstanceId: !Ref OpenviduServer
AllocationId: !GetAtt NewEIP.AllocationId
IMDSv2LaunchTemplate: IMDSv2LaunchTemplate:
Type: AWS::EC2::LaunchTemplate Type: AWS::EC2::LaunchTemplate