Compute Engine Resources
Resources for Google Cloud Compute Engine. Available resources include:
ComputeEngine
: A Compute Engine VM running a Docker Container.ComputeEnginePostgres
: A Postgres instance running on a VM in Google Compute Engine.ComputeEngineRedis
: A Redis instance running on a VM in Google Compute Engine.
Example Usage
Create a VM running a Postgres instance
1from sqlalchemy import text
2import launchflow as lf
3
4postgres_compute_engine = lf.gcp.ComputeEnginePostgres("ce-postgres-mn-test-2")
5engine = postgres_compute_engine.sqlalchemy_engine()
6
7with engine.connect() as connection:
8 print(connection.execute(text("SELECT 1")).fetchone()) # prints (1,)
Create a VM running a Redis instance
1import launchflow as lf
2
3redis = lf.gcp.ComputeEngineRedis("my-redis-instance")
4
5# Set a key-value pair
6client = redis.redis()
7client.set("my-key", "my-value")
8
9# Async compatible
10async_client = await redis.redis_async()
11await async_client.set("my-key", "my-value")
ComputeEngine
A Compute Engine VM running a Docker container.
Like all Resources, this class configures itself across multiple Environments.
For more information see the official documentation.
Example Usage
1import launchflow as lf
2
3# Automatically creates / connects to a Compute Engine VM with a provided Docker image
4compute_engine = lf.gcp.ComputeEngine("my-compute-engine", vm_config=lf.gcp.compute_engine.VMConfig(
5 additional_outputs={"my_output": "my_value"},
6 docker_cfg=lf.gcp.compute_engine.DockerConfig(
7 image="my-docker-image",
8 args=[],
9 environment_variables=[lf.gcp.compute_engine.EnvironmentVariable("MY_ENV_VAR": "my_value")],
10 ),
11 firewall_cfg=lf.gcp.compute_engine.FirewallConfig(expose_ports=[80]),
12))
initialization
Create a Compute Engine resource.
Args:
name (str)
: The name of the resource. This must be globally unique.vm_config (VMConfig)
: The configuration for the VM.additional_outputs (dict)
: Additional outputs to be returned by the resource.service_account_email (str)
: The email of the service account to use. If none a unique service account will be created. If equal to the literal "environment" the environment service account will be useddocker_cfg (DockerConfig)
: The configuration for the Docker container.image (str)
: The Docker image to run.args (List[str])
: The arguments to pass to the Docker container.environment_variables (List[EnvironmentVariable])
: Environment variables to set in the Docker container.
firewall_cfg (FirewallConfig)
: The configuration for the firewall rules.expose_ports (List[int])
: The ports to expose in the firewall.
ComputeEnginePostgres
A Postgres instance running on a VM in Google Compute Engine.
Example usage
1from sqlalchemy import text
2import launchflow as lf
3
4postgres_compute_engine = lf.gcp.ComputeEnginePostgres("ce-postgres-mn-test-2")
5engine = postgres_compute_engine.sqlalchemy_engine()
6
7with engine.connect() as connection:
8 print(connection.execute(text("SELECT 1")).fetchone()) # prints (1,)
initialization
Create a new Compute Engine Postgres resource.
Args:
name (str)
: The name of the Postgres VM resource. This must be globally unique.password (str)
: The password for the Postgres DB. If not provided, a random password will be generated.
query
1ComputeEnginePostgres.query(query: str)
Executes a SQL query on the Postgres instance running on the Compute Engine VM.
Args:
query (str)
: The SQL query to execute.
Returns:
- The results of the query.
Example usage:
1import launchflow as lf
2
3postgres = lf.gcp.ComputeEnginePostgres("my-pg-db")
4
5# Executes a query on the Postgres instance running on the Compute Engine VM
6postgres.query("SELECT 1")
NOTE: This method is not recommended for production use. Use sqlalchemy_engine
instead.
django_settings
1ComputeEnginePostgres.django_settings()
Returns a Django settings dictionary for connecting to the Postgres instance running on the Compute Engine VM.
Returns:
- A dictionary of Django settings for connecting to the Postgres instance.
Example usage:
1import launchflow as lf
2
3postgres = lf.gcp.ComputeEnginePostgres("my-pg-db")
4
5# settings.py
6DATABASES = {
7 # Connect Django's ORM to the Postgres instance running on the Compute Engine VM
8 "default": postgres.django_settings(),
9}
sqlalchemy_engine_options
1ComputeEnginePostgres.sqlalchemy_engine_options()
Returns the SQLAlchemy engine options for connecting to the Postgres instance running on the Compute Engine VM.
Returns:
- A dictionary of SQLAlchemy engine options for connecting to the Postgres instance.
sqlalchemy_async_engine_options
1async ComputeEnginePostgres.sqlalchemy_async_engine_options()
Returns the async SQLAlchemy engine options for connecting to the Postgres instance running on the Compute Engine VM.
Returns:
- A dictionary of async SQLAlchemy engine options for connecting to the Postgres instance.
sqlalchemy_engine
1ComputeEnginePostgres.sqlalchemy_engine(**engine_kwargs)
Returns a SQLAlchemy engine for connecting to a Postgres instance hosted on GCP compute engine.
Args:
**engine_kwargs
: Additional keyword arguments to pass tosqlalchemy.create_engine
.
Returns:
- The SQLAlchemy engine.
Example usage:
1import launchflow as lf
2db = lf.gcp.ComputeEnginePostgres("my-pg-db")
3engine = db.sqlalchemy_engine()
sqlalchemy_async_engine
1async ComputeEnginePostgres.sqlalchemy_async_engine(**engine_kwargs)
Returns an async SQLAlchemy engine for connecting to a Postgres instance hosted on GCP compute engine.
Args:
**engine_kwargs
: Additional keyword arguments to pass tocreate_async_engine
.
Returns:
- The async SQLAlchemy engine.
Example usage:
1import launchflow as lf
2db = lf.gcp.ComputeEnginePostgres("my-pg-db")
3engine = await db.sqlalchemy_async_engine()
ComputeEngineRedis
A Redis instance running on a VM in Google Compute Engine.
Example usage
1import launchflow as lf
2
3redis = lf.gcp.ComputeEngineRedis("my-redis-instance")
4
5# Set a key-value pair
6client = redis.redis()
7client.set("my-key", "my-value")
8
9# Async compatible
10async_client = await redis.redis_async()
11await async_client.set("my-key", "my-value")
initialization
Create a new Compute Engine Redis resource.
Args:
name (str)
: The name of the Redis VM resource. This must be globally unique.password (str)
: The password for the Redis DB. If not provided, a random password will be generated.
django_settings
1ComputeEngineRedis.django_settings()
Returns a Django settings dictionary for connecting to the Redis instance running on the Compute Engine VM.
Returns:
- A dictionary of Django settings for connecting to the Redis instance.
Example usage:
1import launchflow as lf
2
3redis = lf.gcp.ComputeEngineRedis("my-redis-instance")
4
5# settings.py
6CACHES = {
7 # Connect Django's cache to the Redis instance running on the Compute Engine VM
8 "default": redis.django_settings(),
9}
redis
1ComputeEngineRedis.redis(*, decode_responses: bool = True)
Get a Generic Redis Client object from the redis-py library.
Args:
decode_responses (bool)
: Whether to decode responses from the Redis server. Defaults to True.
Returns:
- The Generic Redis Client from the redis-py library.
redis_async
1async ComputeEngineRedis.redis_async(*, decode_responses: bool = True)
Get an Async Redis Client object from the redis-py library.
Args:
decode_responses (bool)
: Whether to decode responses from the Redis server. Defaults to True.
Returns:
- The Async Redis Client object from the redis-py library.