简体   繁体   中英

How to test if process is “in cloud” on AWS

I have a python application which should run remotely via an AWS pipeline and use secrets to get parameters such as database credentials. When running the application locally the parameters are loaded from a parameters.json file. My problem is how to to test I run remotely (so replacing IN_CLOUD_TEST ):

import boto3
from json import load

if [IN_CLOUD_TEST]:
    params_raw = boto3.client('ssm').get_parameters_by_path(Path='/', Recursive=True)['Parameters']
    params = format_params(params)
else:
    with open('parameters.txt') as json_file:
        params = load(json_file)

I could of course use a try/except, but there must be something nicer.

You could check using AWS APIs, but a simpler alternative (and one that doesn't require making HTTP calls, helping you shave off some latency) is to set an environment variable on your remote server that tells it it's the production server and read it from the code.

import boto3
from json import load
from os import getenv

if getenv('IS_REMOTE', False):
    params_raw = boto3.client('ssm').get_parameters_by_path(Path='/', Recursive=True)['Parameters']
    params = format_params(params)
else:
    with open('parameters.txt') as json_file:
        params = load(json_file)

You could also apply the same logic but defining a variable that equals true when your server is supposed to be the testing one, and setting it on your local testing machine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM