gpt4 book ai didi

How to run alembic migrations inside of an AWS Lambda?(如何在AWS Lambda内部运行alembic迁移?)

转载 作者:bug小助手 更新时间:2023-10-25 15:24:54 25 4
gpt4 key购买 nike



I created a CircleCI pipeline that builds an AWS infrastructure using Terraform.

我创建了一个CircleCI管道,使用Terraform构建AWS基础设施。


The problem now is how to run the Alembic migrations.

现在的问题是如何运行Alembic迁移。


So, how to run alembic migrations inside of an AWS Lambda?

那么,如何在AWS Lambda内部运行Alembic迁移?


更多回答

Does this answer your question? Using Alembic API from inside application code

这回答了你的问题吗?从应用程序代码内部使用Alembic API

优秀答案推荐

I tried multiple ways until I found a solution. I hope it can help someone.
Here is how I started creating an Alembic Lambda layer in CircleCI:

我尝试了多种方法,直到找到了解决方案。我希望它能帮助一些人。以下是我开始在CircleCI中创建Alembic Lambda层的方式:


mkdir profile_alembic_lambda_layer
pip install alembic -t profile_alembic_lambda_layer/

Here is my main Terraform code

下面是我的主要Terraform代码


data "archive_file" "lambda_alembic_migrations_archive" {
type = "zip"
source_dir = "lambda_alembic_migrations"
output_path = "lambda_alembic_migrations.zip"
}

data "archive_file" "profile_alembic_lambda_layer_archive" {
type = "zip"
source_dir = "profile_alembic_lambda_layer"
output_path = "profile_alembic_lambda_layer.zip"
}

resource "aws_lambda_layer_version" "profile_alembic_lambda_layer" {
filename = data.archive_file.profile_alembic_lambda_layer_archive.output_path
layer_name = "profile_alembic_lambda_layer"
compatible_runtimes = ["python3.8"] # Specify the runtime(s) your layer is compatible with
}

resource "aws_lambda_function" "alembic_migrations_lambda" {
filename = data.archive_file.lambda_alembic_migrations_archive.output_path
function_name = "alembic_migrations_lambda_handler"
role = aws_iam_role.lambda_execution_role.arn
handler = "src.app.lambda_postgresql.lambdas.alembic_migration.alembic_migration_lambda_handler"
runtime = var.aws_lambda_python_runtime
source_code_hash = data.archive_file.lambda_alembic_migrations_archive.output_base64sha256
depends_on = [
data.archive_file.lambda_alembic_migrations_archive,
aws_cloudwatch_log_group.lambda_alembic_migrations_log_group,
]
timeout = 900
layers = [
aws_lambda_layer_version.profile_lambda_layer.arn,
aws_lambda_layer_version.profile_alembic_lambda_layer.arn
]

vpc_config {
subnet_ids = var.subnet_ids
security_group_ids = var.lambda_security_group_ids
}
environment {
variables = {
DATABASE_HOST = var.postgres_db_host
DATABASE_PORT = var.postgres_db_port
DATABASE_NAME = var.postgres_db_name
DATABASE_USER = var.postgres_db_user
DATABASE_PASSWORD = var.postgres_db_user_password
LD_LIBRARY_PATH = "/var/task"
}
}
}

Here is the Lambda function that I used:

下面是我使用的lambda函数:


import sys


sys.path.append('/opt')


import alembic.config


def alembic_migration_lambda_handler(event, context):
""" Create Company Lambda Function Handler """
print("Start")

alembicArgs = [
'--raiseerr',
'upgrade', 'head',
]
alembic.config.main(argv=alembicArgs)

Thanks @mimo for the comment. That was part of the solution!

感谢@MIMO的评论。这是解决方案的一部分!


更多回答

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com