5

I would like to access the Metadata Service from a App Engine Standard application. I tried doing a urlfetch to http://metadata.google.internal/computeMetadata/v1/project/attributes and got back DNS lookup failed:

    logging.info(urlfetch.fetch('http://metadata.google.internal/computeMetadata/v1/project/attributes/').content)

Is this possible? I'd like to share config between App Engine Flex and Standard code in the same project.

jcjones1515
  • 471
  • 4
  • 12

2 Answers2

8

Some friendly folks on the GCP slack channel pointed me to the RuntimeConfig API for sharing configuration across multiple types of services in Google Cloud. This solves the problem of sharing configs that I was looking for.

For those curious you have to:

  1. Enable the API in API Manager for your google cloud project
  2. Run some gcloud commands:

    gcloud beta deployment-manager runtime-configs create foo-credentials
    gcloud beta deployment-manager runtime-configs variables set "bar-variable-name" "baz-value"  --config-name "foo-credentials"```
    
  3. Add the python google-cloud-runtimeconfig library to your project (I did it via pip)

  4. Add some python code to fetch the variable at runtime:

    config_client = runtimeconfig.Client()
    config = config_client.config('foo-credentials')
    bar = config.get_variable('bar-variable-name')```
    
jcjones1515
  • 471
  • 4
  • 12
2

No, you can't access the (GCE-specific) metadata from a GAE standard instance since it's not a GCE VM/instance. From Getting metadata (emphasis mine):

You can query the contents of the metadata server by making a request to the following root URLs from within a virtual machine instance. Use the http://metadata.google.internal/computeMetadata/v1/ URL to make requests to the metadata server.

The DNS failure you see for metadata.google.internal is a likely indicator that it's a special host DNS entry available only inside the GCE network or machine.

But in general it is possible to share files across GAE services/modules by symlinking the same file (ideally placed in the app dir) inside each of the service/module dir requiring it. See examples here: Sharing entities between App Engine modules and here: https://stackoverflow.com/a/34111170/4495081

As long as the flex service/module uses the same file(s) content(s) the same way as the standard one does, this technique should work for them as well, meaning you can share configs by sharing an appengine_config.py file, for example.

Community
  • 1
  • 1
Dan Cornilescu
  • 39,470
  • 12
  • 57
  • 97
  • thanks @Dan, that's my suspicion too -- I don't think the symlink trick between services (although cool) would work between App Engine flex and App Engine standard? – jcjones1515 Dec 07 '16 at 15:29
  • I didn't yet play with flex env, so I can't tell with certainty. But maybe it's worth a try? – Dan Cornilescu Dec 07 '16 at 15:42
  • the thing @Dan is I have several independent services which are not from one common code base, but I want them to be able to share metadata for configuration. Having symlinks between them doesn't make sense because they aren't even aware of each other in some cases – jcjones1515 Dec 08 '16 at 16:40
  • @jcjones1515: I mentioned the symlinks solely thinking about having the relevant configs in sync across modules (duplicating info is error prone). Of course that has to be sanity-checked in your actual project context. – Dan Cornilescu Dec 08 '16 at 16:53