221 lines
5.5 KiB
Text
221 lines
5.5 KiB
Text
|
|
#!/bin/bash
|
||
|
|
# AWS VPC and Network provisioning script
|
||
|
|
# Generated by provisioning {{ provisioning_version }} at {{ now }}
|
||
|
|
# Reference: AWS EC2 VPC - https://docs.aws.amazon.com/vpc/latest/userguide/what-is-amazon-vpc.html
|
||
|
|
|
||
|
|
set -e
|
||
|
|
{% if debug and debug == "yes" %}set -x{% endif %}
|
||
|
|
|
||
|
|
# Validate AWS CLI is installed
|
||
|
|
if ! command -v aws &> /dev/null; then
|
||
|
|
echo "ERROR: AWS CLI is not installed"
|
||
|
|
exit 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
# Validate required environment
|
||
|
|
if [ -z "$AWS_REGION" ]; then
|
||
|
|
echo "ERROR: AWS_REGION environment variable not set"
|
||
|
|
exit 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
# VPC Configuration
|
||
|
|
VPC_NAME="{{ vpc.name | default('provisioning-vpc') }}"
|
||
|
|
VPC_CIDR="{{ vpc.cidr_block | default('10.0.0.0/16') }}"
|
||
|
|
VPC_DESCRIPTION="{{ vpc.description | default('VPC created by provisioning') }}"
|
||
|
|
|
||
|
|
# Validate required fields
|
||
|
|
if [ -z "$VPC_NAME" ]; then
|
||
|
|
echo "ERROR: VPC name is required"
|
||
|
|
exit 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
echo "Creating VPC: $VPC_NAME with CIDR: $VPC_CIDR..."
|
||
|
|
|
||
|
|
# Create VPC
|
||
|
|
VPC_ID=$(aws ec2 create-vpc \
|
||
|
|
--cidr-block "$VPC_CIDR" \
|
||
|
|
--tag-specifications "ResourceType=vpc,Tags=[{Key=Name,Value=$VPC_NAME}]" \
|
||
|
|
--region "$AWS_REGION" \
|
||
|
|
--query 'Vpc.VpcId' \
|
||
|
|
--output text)
|
||
|
|
|
||
|
|
if [ -z "$VPC_ID" ]; then
|
||
|
|
echo "ERROR: Failed to create VPC"
|
||
|
|
exit 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
echo "✓ VPC created successfully"
|
||
|
|
echo " VPC ID: $VPC_ID"
|
||
|
|
echo " CIDR: $VPC_CIDR"
|
||
|
|
|
||
|
|
# Enable DNS support
|
||
|
|
aws ec2 modify-vpc-attribute \
|
||
|
|
--vpc-id "$VPC_ID" \
|
||
|
|
--enable-dns-support \
|
||
|
|
--region "$AWS_REGION"
|
||
|
|
|
||
|
|
aws ec2 modify-vpc-attribute \
|
||
|
|
--vpc-id "$VPC_ID" \
|
||
|
|
--enable-dns-hostnames \
|
||
|
|
--region "$AWS_REGION"
|
||
|
|
|
||
|
|
echo "✓ DNS support enabled for VPC"
|
||
|
|
|
||
|
|
{% if vpc.subnets %}
|
||
|
|
# Create Subnets
|
||
|
|
echo ""
|
||
|
|
echo "Creating subnets..."
|
||
|
|
{% for subnet in vpc.subnets %}
|
||
|
|
|
||
|
|
SUBNET_NAME="{{ subnet.name | default('subnet-' + loop.index0 | string) }}"
|
||
|
|
SUBNET_CIDR="{{ subnet.cidr_block | default('10.0.' + loop.index0 | string + '.0/24') }}"
|
||
|
|
SUBNET_AZ="{{ subnet.availability_zone | default('') }}"
|
||
|
|
|
||
|
|
echo "Creating subnet: $SUBNET_NAME with CIDR: $SUBNET_CIDR..."
|
||
|
|
|
||
|
|
# Build aws ec2 create-subnet command
|
||
|
|
CREATE_SUBNET_CMD="aws ec2 create-subnet \
|
||
|
|
--vpc-id $VPC_ID \
|
||
|
|
--cidr-block $SUBNET_CIDR"
|
||
|
|
|
||
|
|
{% if subnet.availability_zone %}
|
||
|
|
CREATE_SUBNET_CMD="$CREATE_SUBNET_CMD --availability-zone $SUBNET_AZ"
|
||
|
|
{% endif %}
|
||
|
|
|
||
|
|
CREATE_SUBNET_CMD="$CREATE_SUBNET_CMD \
|
||
|
|
--tag-specifications 'ResourceType=subnet,Tags=[{Key=Name,Value=$SUBNET_NAME}]' \
|
||
|
|
--region $AWS_REGION \
|
||
|
|
--query 'Subnet.SubnetId' \
|
||
|
|
--output text"
|
||
|
|
|
||
|
|
SUBNET_ID=$(eval "$CREATE_SUBNET_CMD")
|
||
|
|
|
||
|
|
if [ -z "$SUBNET_ID" ]; then
|
||
|
|
echo "ERROR: Failed to create subnet $SUBNET_NAME"
|
||
|
|
exit 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
echo " ✓ Subnet created: $SUBNET_ID"
|
||
|
|
echo " CIDR: $SUBNET_CIDR"
|
||
|
|
|
||
|
|
{% if subnet.map_public_ip_on_launch %}
|
||
|
|
# Enable public IP assignment
|
||
|
|
aws ec2 modify-subnet-attribute \
|
||
|
|
--subnet-id "$SUBNET_ID" \
|
||
|
|
--map-public-ip-on-launch \
|
||
|
|
--region "$AWS_REGION"
|
||
|
|
|
||
|
|
echo " ✓ Public IP assignment enabled"
|
||
|
|
{% endif %}
|
||
|
|
|
||
|
|
{% endfor %}
|
||
|
|
{% endif %}
|
||
|
|
|
||
|
|
{% if vpc.enable_nat_gateway %}
|
||
|
|
# Create Internet Gateway
|
||
|
|
echo ""
|
||
|
|
echo "Creating Internet Gateway..."
|
||
|
|
|
||
|
|
IGW_NAME="{{ vpc.name }}-igw"
|
||
|
|
IGW_ID=$(aws ec2 create-internet-gateway \
|
||
|
|
--tag-specifications "ResourceType=internet-gateway,Tags=[{Key=Name,Value=$IGW_NAME}]" \
|
||
|
|
--region "$AWS_REGION" \
|
||
|
|
--query 'InternetGateway.InternetGatewayId' \
|
||
|
|
--output text)
|
||
|
|
|
||
|
|
if [ -z "$IGW_ID" ]; then
|
||
|
|
echo "ERROR: Failed to create Internet Gateway"
|
||
|
|
exit 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
# Attach IGW to VPC
|
||
|
|
aws ec2 attach-internet-gateway \
|
||
|
|
--internet-gateway-id "$IGW_ID" \
|
||
|
|
--vpc-id "$VPC_ID" \
|
||
|
|
--region "$AWS_REGION"
|
||
|
|
|
||
|
|
echo "✓ Internet Gateway created and attached"
|
||
|
|
echo " IGW ID: $IGW_ID"
|
||
|
|
|
||
|
|
{% if vpc.create_nat_instance %}
|
||
|
|
# Create NAT Gateway (requires Elastic IP)
|
||
|
|
echo ""
|
||
|
|
echo "Creating NAT Gateway..."
|
||
|
|
|
||
|
|
# Allocate Elastic IP
|
||
|
|
EIP_ID=$(aws ec2 allocate-address \
|
||
|
|
--domain vpc \
|
||
|
|
--region "$AWS_REGION" \
|
||
|
|
--query 'AllocationId' \
|
||
|
|
--output text)
|
||
|
|
|
||
|
|
if [ -z "$EIP_ID" ]; then
|
||
|
|
echo "ERROR: Failed to allocate Elastic IP"
|
||
|
|
exit 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
echo " ✓ Elastic IP allocated: $EIP_ID"
|
||
|
|
|
||
|
|
# Create NAT Gateway in first public subnet
|
||
|
|
{% if vpc.subnets %}
|
||
|
|
FIRST_SUBNET_ID=$(aws ec2 describe-subnets \
|
||
|
|
--filters "Name=vpc-id,Values=$VPC_ID" \
|
||
|
|
--region "$AWS_REGION" \
|
||
|
|
--query 'Subnets[0].SubnetId' \
|
||
|
|
--output text)
|
||
|
|
|
||
|
|
NAT_GW_ID=$(aws ec2 create-nat-gateway \
|
||
|
|
--subnet-id "$FIRST_SUBNET_ID" \
|
||
|
|
--allocation-id "$EIP_ID" \
|
||
|
|
--tag-specifications "ResourceType=natgateway,Tags=[{Key=Name,Value={{ vpc.name }}-nat}]" \
|
||
|
|
--region "$AWS_REGION" \
|
||
|
|
--query 'NatGateway.NatGatewayId' \
|
||
|
|
--output text)
|
||
|
|
|
||
|
|
if [ -z "$NAT_GW_ID" ]; then
|
||
|
|
echo "ERROR: Failed to create NAT Gateway"
|
||
|
|
exit 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
echo " ✓ NAT Gateway created: $NAT_GW_ID"
|
||
|
|
|
||
|
|
# Wait for NAT Gateway to be available
|
||
|
|
echo " Waiting for NAT Gateway to become available..."
|
||
|
|
aws ec2 wait nat-gateway-available \
|
||
|
|
--nat-gateway-ids "$NAT_GW_ID" \
|
||
|
|
--region "$AWS_REGION"
|
||
|
|
|
||
|
|
echo " ✓ NAT Gateway is available"
|
||
|
|
{% endif %}
|
||
|
|
{% endif %}
|
||
|
|
{% endif %}
|
||
|
|
|
||
|
|
{% if vpc.tags %}
|
||
|
|
# Apply additional tags
|
||
|
|
echo ""
|
||
|
|
echo "Applying tags to VPC..."
|
||
|
|
{% for key, value in vpc.tags.items() %}
|
||
|
|
aws ec2 create-tags \
|
||
|
|
--resources "$VPC_ID" \
|
||
|
|
--tags "Key={{ key }},Value={{ value }}" \
|
||
|
|
--region "$AWS_REGION"
|
||
|
|
{% endfor %}
|
||
|
|
echo "✓ Tags applied"
|
||
|
|
{% endif %}
|
||
|
|
|
||
|
|
# Output VPC details
|
||
|
|
echo ""
|
||
|
|
echo "VPC Configuration Summary:"
|
||
|
|
echo " VPC ID: $VPC_ID"
|
||
|
|
echo " VPC Name: $VPC_NAME"
|
||
|
|
echo " CIDR Block: $VPC_CIDR"
|
||
|
|
echo " Region: $AWS_REGION"
|
||
|
|
|
||
|
|
aws ec2 describe-vpcs \
|
||
|
|
--vpc-ids "$VPC_ID" \
|
||
|
|
--region "$AWS_REGION" \
|
||
|
|
--query 'Vpcs[0].[VpcId,CidrBlock,State]' \
|
||
|
|
--output table
|
||
|
|
|
||
|
|
exit 0
|