Configure stack to use pre-created static Elastic IP placeholders

This commit is contained in:
vickytechkey 2026-08-06 11:31:47 +05:30
parent bbf1df9b6c
commit 2af53a3ccb

View file

@ -69,19 +69,17 @@ export class AwsCdkStack extends cdk.Stack {
'apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin',
'systemctl enable docker',
'systemctl start docker',
// Setup wireguard directory and fetch Elastic IP dynamically using IMDSv2
// Setup wireguard directory
'mkdir -p /home/ubuntu/wireguard',
'cd /home/ubuntu/wireguard',
'TOKEN=$(curl -s -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600")',
'PUBLIC_IP=$(curl -s -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/public-ipv4)',
// Write the docker-compose.yml file substituting the resolved public Elastic IP
// Write the docker-compose.yml file with hardcoded Elastic IP
'cat <<EOF > docker-compose.yml',
'services:',
' wg-easy:',
' image: weejewel/wg-easy',
' container_name: wg-easy',
' environment:',
' - WG_HOST=${PUBLIC_IP}',
' - WG_HOST=YOUR_ELASTIC_IP_HERE', // <--- Replace with your manually created Elastic IP
' - PASSWORD=admin123',
' volumes:',
' - ./config:/etc/wireguard',
@ -101,22 +99,18 @@ export class AwsCdkStack extends cdk.Stack {
'chown -R ubuntu:ubuntu /home/ubuntu/wireguard'
);
// 5. Provision Elastic IP and Associate with EC2
const eip = new ec2.CfnEIP(this, 'MailServerEIP', {
domain: 'vpc',
});
// 5. Associate your pre-created Elastic IP using its Allocation ID
new ec2.CfnEIPAssociation(this, 'MailServerEIPAssociation', {
allocationId: eip.attrAllocationId,
allocationId: 'YOUR_ALLOCATION_ID_HERE', // <--- Replace with your Elastic IP Allocation ID (e.g., eipalloc-xxxx)
instanceId: instance.instanceId,
});
// Outputs
new cdk.CfnOutput(this, 'MailServerPublicIP', {
value: eip.ref,
value: 'YOUR_ELASTIC_IP_HERE', // <--- Replace with your Elastic IP
description: 'The Elastic IP address of your Mail Server',
});
}
}