如何通过Shell命令获取AWS弹性IP,区分部署环境执行不同脚本?
Hey there! Let's tackle your problem step by step. You want to grab the Elastic IP (EIP) of your EC2 instance via shell commands to distinguish between your dev and prod environments for CodeDeploy scripts—here are two reliable ways to do this, plus how to integrate it into your deployment workflow:
1. Get Elastic IP via EC2 Instance Metadata Service (No AWS CLI Required)
This is the lightest approach, since it doesn't require installing or configuring the AWS CLI on your instance. The EC2 metadata service provides direct access to instance details locally:
# Fetch the public IP (which is your Elastic IP if one is attached) EIP=$(curl -s http://169.254.169.254/latest/meta-data/public-ipv4) echo "Current instance's Elastic IP: $EIP"
If your instance has an Elastic IP bound to it, this command will return that exact IP. If not, it'll return the instance's dynamic public IP (but since you mentioned using static mapped IPs, this should work perfectly for your use case).
2. Get Elastic IP via AWS CLI (Requires IAM Permissions)
If you already have the AWS CLI set up on your instance, you can use it to fetch the EIP by first getting the instance ID, then querying EC2 for the attached address:
# First get the current instance ID from metadata INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id) # Use AWS CLI to fetch the Elastic IP attached to this instance EIP=$(aws ec2 describe-addresses --filters "Name=instance-id,Values=$INSTANCE_ID" --query 'Addresses[0].PublicIp' --output text) echo "Current instance's Elastic IP: $EIP"
Note: For this to work, your EC2 instance's IAM role needs the ec2:DescribeAddresses permission (you can attach the AmazonEC2ReadOnlyAccess managed policy for simplicity, or create a custom policy with just that permission).
Integrate into Your CodeDeploy Script
Here's a complete example script that uses the metadata method to detect the environment and run the appropriate Spring Boot startup command:
#!/bin/bash # Fetch the instance's Elastic IP EIP=$(curl -s http://169.254.169.254/latest/meta-data/public-ipv4) # Define your environment IPs PROD_EIP="192.168.1.100" # Replace with your production EIP STAGING_EIP="192.168.1.200" # Replace with your staging EIP # Determine environment and execute corresponding startup if [ "$EIP" == "$PROD_EIP" ]; then echo "Starting Spring Boot app in PRODUCTION environment..." java -jar your-spring-boot-app.jar --spring.profiles.active=prod elif [ "$EIP" == "$STAGING_EIP" ]; then echo "Starting Spring Boot app in STAGING environment..." java -jar your-spring-boot-app.jar --spring.profiles.active=dev else echo "Error: Unknown environment with IP $EIP" exit 1 fi
Quick Tips
- Make sure your EC2 instance's security group allows outbound access to the metadata service (this is enabled by default, but double-check if you've modified network rules).
- If you ever need a more flexible approach (e.g., changing IPs without updating scripts), consider tagging your EC2 instances with
Environment=prodorEnvironment=stagingand fetching that tag via metadata or CLI instead.
内容的提问来源于stack exchange,提问作者Artist

