0

I am fairly new to AWS and would like to know a good practice for this:

I have a python application that has separate sets of configuration for development, staging and production.

In the code I currently identify the environment with either a environment variable or a file set in a directory (that is not part of ste SCM obviously)

How do I mark an EC2 instance as staging so that my python application can detect it and apply the correct configuration set?

  • "python application" - Are you using boto? – emmdee May 1 '18 at 17:40
2

There's a least a couple of ways:

  • Easiest is to use tags. Have a tag with the name "environment" and the value development, staging, or production
  • Have separate subnets, VPC, or accounts for each environment. This gives more isolation and can reduce the chance of working on the wrong environment
  • so i can query the tags of the current instane with boto from within the instance? – caliph Apr 28 '18 at 10:39
  • I don't know what boto is, but using the API or web interface you can query tags. I think you should really consider separate VPCs for each environment. – Tim Apr 28 '18 at 19:21
0

This is commonly done with instance tags

An EC2 instance can have any number of tags in key:value format. They are often used liberally to help further processes identify instance roles/types/environments/etc

Some examples:

sometag:somevalue
env:prod
type:webserver
backup:yes

Since you mentioned Python, I assume you're using BOTO. Check the boto documentation, you can even filter on certain tags: https://boto3.readthedocs.io/en/latest/reference/services/ec2.html#EC2.Client.describe_instances

Noted on that doc page:

tag :key =*value* - The key/value combination of a tag assigned to the resource. Specify the key of the tag in the filter name and the value of the tag in the filter value. For example, for the tag Purpose=X, specify tag:Purpose for the filter name and X for the filter value.

Furthering the example from the docs:

Filters=[
    {
        'Name': 'tag:env',
        'Values': [
            'prod'
        ]
    },
],

Other than BOTO, many other AWS interaction utilities will often have a similar filter or query system based on instance tagging. Like Ansible, etc.

Screenshot of the instance tags panel in the EC2 console:

enter image description here

0

Tim's answer is the best. Separate VPCs are best use. Using lambda you can schedule shutdowns for nights and weekends to turn off all the DEV/UAT servers when not in use.

Keeping the networks separated is safer from accidental coding changes to prod.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.