AWS static ip address

961 views Asked by At

I am using AWS code deploy agent and deploying my project to the server through bitbucket plugin.

The code deployment agent first executes the script files which has the command to execute my spring-boot project.

Since I have two environments one development and another production. I want the script to do things differently based on the environment i.e two different instances.

My plan is to fetch the aws static ip-address which is mapped and from that determine the environment (production or stage).

How to fetch the elastic ip address through sh commands.

edited

4

There are 4 answers

0
Brandon Miller On

You could do the following:

id=$( curl http://169.254.169.254/latest/meta-data/instance-id )
eip=$( aws ec2 describe-addresses --filter Name=instance-id,Values=${id} | aws ec2 describe-addresses | jq .Addresses[].PublicIp --raw-output )

The above gets the instance-id from metadata, then uses the aws cli to look for elastic IPs filtered by the id from metadata. Using jq this output can then be parsed down to the IP you are looking for.

0
helloV On

Query the metadata server

eip=`curl -s 169.254.169.254/latest/meta-data/public-ipv4`
echo $eip
0
Artist On

The solution is completely off tangent to what I originally asked but it was enough for my requirement.

I just needed to know the environment I am in to do certain actions. So what I did was to set an environment variable in an independent script file where the environment variable is set and the value is that of the environment. ex: let's say in a file env-variables.sh export profile= stage

In the script file where the commands have to be executed based on the environment I access it this way

 source /test/env-variables.sh
    echo current profile is $profile
    if [ $profile = stage ]
    then
        echo stage
    elif [ $profile = production ]
    then
        echo production
else
        echo failure
    fi

Hope some one finds it useful

0
Bangxi Yu On

Static IP will work.

Here is a more nature CodeDeploy way to solve this is - set up 2 CodeDeploy deployment groups, one for your development env and the other for your production env. Then in your script, you can use environment variables that CodeDeploy will set during the deployment to understand which env you are deploying to.

Here is a blog post about how to use CodeDeploy environment variables: https://aws.amazon.com/blogs/devops/using-codedeploy-environment-variables/