Skip to content

Chris' Laboratory

chrislee.kr – Personal blog as bookshelves

Menu
  • Home
  • Github
  • Contact Me
Menu

Get AWS EC2 tags to environment parameters for apache2

Posted on 17/12/201622/01/2017 by Chris Lee
Read Time:1 Minute, 51 Second

Objective:

  • AWS instances are assigned tags per instance.
  • Need tags to be environment parameters for Apache2; thus, web application can retrieve environment parameter.

 

Step 1: Configure tags in AWS EC2 instance

 

aws_ec2_tag

Step 2: Setup IAM user and get access key id and secret access key

 

Step 3: Access to SSH and append following shell scripts

$ sudo apt-get install -y jq curl awscli
$ sudo nano /etc/apache2/envvars

Append following script to /etc/apache2/envvars

  • Note that you need to replace AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY and AWS_DEFAULT_REGION
  • Note that you need  to update allowed_tag variable if you want to add different tag name rather than ENVIRONMENT
######
# Customized By CL
# Original Author: Marcello de Sales (marcello.desales@gmail.com)
# Reference: https://github.com/12moons/ec2-tags-env/blob/master/import-tags.sh
### Requirements:
# 1. Install jq library (sudo apt-get install -y jq)
# 2. Configure IAM for permission AmazonEC2ReadOnlyAccess
### Installation:
# 1. Add tags to an EC2 host or Image Profile
# 2. Append following scripts to /etc/apache2/envvars
# 3. Restart apache2
# 4. Access to web application to get environment parameters
####

export AWS_ACCESS_KEY_ID=XXXXXXXXXX
export AWS_SECRET_ACCESS_KEY=XXXXXXXXXX
export AWS_DEFAULT_REGION=ap-southeast-2

# get instance tags
instance_id=$(/usr/bin/curl --silent http://169.254.169.254/latest/meta-data/instance-id)
instance_tags=$(aws ec2 describe-tags --filters "Name=resource-id,Values=$instance_id")

ami_id=$(/usr/bin/curl --silent http://169.254.169.254/latest/meta-data/ami-id)
ami_tags=$(aws ec2 describe-tags --filters "Name=resource-id,Values=$ami_id")

tags_to_env () {
    tags=$1

    for key in $(echo $tags | /usr/bin/jq -r ".[][].Key"); do
        value=$(echo $tags | /usr/bin/jq -r ".[][] | select(.Key==\"$key\") | .Value")
        key=$(echo $key | /usr/bin/tr '-' '_' | /usr/bin/tr '[:lower:]' '[:upper:]')

        allowed_tag=$(echo "ENVIRONMENT")
        if [ "${allowed_tag}" = "${key}" ] ; then
                export $key="$value"
        fi
    done
}

tags_to_env "$ami_tags"
tags_to_env "$instance_tags"

 

Step 4: Restart Apache2

$ sudo /etc/init.d/apache2 restart

 

Step 5: Check environment parameters

<?php
    $environment = getenv('ENVIRONMENT');
    echo "environment => ".$environment.PHP_EOL;

 

 

Share

Facebook
Twitter
LinkedIn
Email

Related

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Categories

  • Databases (11)
    • MongoDB (4)
    • MS-SQL (1)
    • MySQL (6)
  • E-Commerce (8)
    • Magento (8)
  • Finance (2)
  • Frameworks (84)
    • Adobe Flex (1)
    • Angular (ngx) (3)
    • Codeigniter (6)
    • CSS (5)
    • Django (2)
    • Javascript (13)
    • Node.js (6)
    • PHP (17)
    • React Native (4)
    • React.js (1)
    • Sencha Touch (4)
    • Terraform (1)
    • Vue.js (1)
    • WordPress (4)
    • Yii2 (3)
  • General Documents (15)
  • Marketing (3)
  • Mobile Development (33)
    • Android (20)
    • iPhone (13)
  • Platforms (21)
    • Arduino (2)
    • Docker (5)
    • Google App Engine (5)
    • Raspberry Pi (5)
    • Samsung Smart TV (4)
  • Security (17)
  • Server (31)
    • Linux (13)
  • Tools (14)
    • SVN (7)
  • Uncategorized (2)

Search

Recent Posts

  • Bootstrapping Kubernetes cluster with kubeadm
  • Taint all resources in the one module
  • Alpine – Plugin caching_sha2_password could not be loaded
  • npm link with peerDependencies
  • How to setup Gitlab runner with KVM enabled

Recent Comments

  • Obayed on Binance Auto Trading Bot – Buy low/Sell high with stop loss limit/Trade multiple coins
  • Ari on How to install memcache.so/memcached.so for MAMP Pro (Mac)
  • Mida ali on Binance Auto Trading Bot – Buy low/Sell high with stop loss limit/Trade multiple coins
  • Chris Lee on How to install memcache.so/memcached.so for MAMP Pro (Mac)
  • Chris Lee on Setting Up A VPN Server On OSX 10.6

Tags

1 ajax amazon android android-addpart browser chrislee-kr codeigniter codeigniter-tcpdf com-apple-net-racoon CSS CSS history hack delpaigmail-com entity-addpart-double exception-printing-is-disabled-by-default-for-security-reasons ext-plugins-listpagingplugin ext-plugins-listpagingplugin-example f iphone javascript jquery-defaultchecked jquery-samsung-smart-tv listpagingplugin mac magento-exception-printing-is-disabled-by-default-for-security-reasons magento-sample-data-exception-printing-is-disabled-by-default-for-security-reasons nu-vot null-core-errors-confignotfound-config-mk9engine-ini php samsung-smart-tv-jquery samsung-smart-tv-sdk-ajax samsung-smart-tv-sdk-jquery samsung-tv-sdk samsung-tv-sdk-jquery samsung tv sencha-smart-tv sencha-touch-list-paging smart-tv-jquery sqlite subversion svn tcedook tcpdf-codeigniter uilinebreakmodecharacterwrap-is-deprecated unknown-column-link-area

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
© 2023 Chris' Laboratory | Powered by Minimalist Blog WordPress Theme
 

Loading Comments...