Skip to content

Commit 98bbc45

Browse files
oavdeevtgvashworth
andauthored
Custom local file location (#81)
* Inline content of db_migrate_lambda file This removes the need for a .py file to be generated. * Add variable for the DB migrate lambda path This allows the path to be set to a specific location and committed or cached, so that it is not recreated unnecessarily in an automation context. * docs --------- Co-authored-by: Tom Ashworth <[email protected]>
1 parent 4bb245d commit 98bbc45

File tree

7 files changed

+27
-15
lines changed

7 files changed

+27
-15
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ resource "local_file" "metaflow_config" {
110110
| <a name="input_compute_environment_min_vcpus"></a> [compute\_environment\_min\_vcpus](#input\_compute\_environment\_min\_vcpus) | Minimum VCPUs for Batch Compute Environment [0-16] for EC2 Batch Compute Environment (ignored for Fargate) | `number` | `8` | no |
111111
| <a name="input_db_engine_version"></a> [db\_engine\_version](#input\_db\_engine\_version) | n/a | `string` | `"11"` | no |
112112
| <a name="input_db_instance_type"></a> [db\_instance\_type](#input\_db\_instance\_type) | RDS instance type to launch for PostgresQL database. | `string` | `"db.t2.small"` | no |
113+
| <a name="input_db_migrate_lambda_zip_file"></a> [db\_migrate\_lambda\_zip\_file](#input\_db\_migrate\_lambda\_zip\_file) | Output path for the zip file containing the DB migrate lambda | `string` | `null` | no |
113114
| <a name="input_enable_custom_batch_container_registry"></a> [enable\_custom\_batch\_container\_registry](#input\_enable\_custom\_batch\_container\_registry) | Provisions infrastructure for custom Amazon ECR container registry if enabled | `bool` | `false` | no |
114115
| <a name="input_enable_step_functions"></a> [enable\_step\_functions](#input\_enable\_step\_functions) | Provisions infrastructure for step functions if enabled | `bool` | n/a | yes |
115116
| <a name="input_extra_ui_backend_env_vars"></a> [extra\_ui\_backend\_env\_vars](#input\_extra\_ui\_backend\_env\_vars) | Additional environment variables for UI backend container | `map(string)` | `{}` | no |

main.tf

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ module "metaflow-metadata-service" {
2727
database_name = module.metaflow-datastore.database_name
2828
database_password = module.metaflow-datastore.database_password
2929
database_username = module.metaflow-datastore.database_username
30+
db_migrate_lambda_zip_file = var.db_migrate_lambda_zip_file
3031
datastore_s3_bucket_kms_key_arn = module.metaflow-datastore.datastore_s3_bucket_kms_key_arn
3132
enable_api_basic_auth = var.metadata_service_enable_api_basic_auth
3233
enable_api_gateway = var.metadata_service_enable_api_gateway

modules/metadata-service/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ If the `access_list_cidr_blocks` variable is set, only traffic originating from
2020
| <a name="input_database_password"></a> [database\_password](#input\_database\_password) | The database password | `string` | n/a | yes |
2121
| <a name="input_database_username"></a> [database\_username](#input\_database\_username) | The database username | `string` | n/a | yes |
2222
| <a name="input_datastore_s3_bucket_kms_key_arn"></a> [datastore\_s3\_bucket\_kms\_key\_arn](#input\_datastore\_s3\_bucket\_kms\_key\_arn) | The ARN of the KMS key used to encrypt the Metaflow datastore S3 bucket | `string` | n/a | yes |
23+
| <a name="input_db_migrate_lambda_zip_file"></a> [db\_migrate\_lambda\_zip\_file](#input\_db\_migrate\_lambda\_zip\_file) | Output path for the zip file containing the DB migrate lambda | `string` | `null` | no |
2324
| <a name="input_enable_api_basic_auth"></a> [enable\_api\_basic\_auth](#input\_enable\_api\_basic\_auth) | Enable basic auth for API Gateway? (requires key export) | `bool` | `true` | no |
2425
| <a name="input_enable_api_gateway"></a> [enable\_api\_gateway](#input\_enable\_api\_gateway) | Enable API Gateway for public metadata service endpoint | `bool` | `true` | no |
2526
| <a name="input_fargate_execution_role_arn"></a> [fargate\_execution\_role\_arn](#input\_fargate\_execution\_role\_arn) | The IAM role that grants access to ECS and Batch services which we'll use as our Metadata Service API's execution\_role for our Fargate instance | `string` | n/a | yes |

modules/metadata-service/lambda.tf

+9-11
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,13 @@ resource "aws_iam_role_policy" "grant_lambda_ecs_vpc" {
7878
policy = data.aws_iam_policy_document.lambda_ecs_task_execute_policy_vpc.json
7979
}
8080

81-
resource "local_file" "db_migrate_lambda" {
82-
content = <<EOF
81+
data "archive_file" "db_migrate_lambda" {
82+
type = "zip"
83+
output_file_mode = "0666"
84+
output_path = local.db_migrate_lambda_zip_file
85+
86+
source {
87+
content = <<EOF
8388
import os, json
8489
from urllib import request
8590
@@ -101,15 +106,8 @@ def handler(event, context):
101106
print(response)
102107
return(response)
103108
EOF
104-
filename = local.db_migrate_lambda_source_file
105-
}
106-
107-
data "archive_file" "db_migrate_lambda" {
108-
type = "zip"
109-
source_file = local.db_migrate_lambda_source_file
110-
output_file_mode = "0666"
111-
output_path = local.db_migrate_lambda_zip_file
112-
depends_on = [local_file.db_migrate_lambda]
109+
filename = "index.py"
110+
}
113111
}
114112

115113
resource "aws_lambda_function" "db_migrate_lambda" {

modules/metadata-service/locals.tf

+3-4
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@ locals {
2222
api_gateway_stage_name = "api"
2323
api_gateway_usage_plan_name = "${var.resource_prefix}usage-plan${var.resource_suffix}"
2424

25-
db_migrate_lambda_source_file = "${path.module}/index.py"
26-
db_migrate_lambda_zip_file = "${path.module}/db_migrate_lambda.zip"
27-
db_migrate_lambda_name = "${var.resource_prefix}db_migrate${var.resource_suffix}"
28-
lambda_ecs_execute_role_name = "${var.resource_prefix}lambda_ecs_execute${var.resource_suffix}"
25+
db_migrate_lambda_zip_file = coalesce(var.db_migrate_lambda_zip_file, "${path.module}/db_migrate_lambda.zip")
26+
db_migrate_lambda_name = "${var.resource_prefix}db_migrate${var.resource_suffix}"
27+
lambda_ecs_execute_role_name = "${var.resource_prefix}lambda_ecs_execute${var.resource_suffix}"
2928

3029
cloudwatch_logs_arn_prefix = "arn:${var.iam_partition}:logs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}"
3130
}

modules/metadata-service/variables.tf

+6
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ variable "enable_api_gateway" {
3737
description = "Enable API Gateway for public metadata service endpoint"
3838
}
3939

40+
variable "db_migrate_lambda_zip_file" {
41+
type = string
42+
description = "Output path for the zip file containing the DB migrate lambda"
43+
default = null
44+
}
45+
4046
variable "fargate_execution_role_arn" {
4147
type = string
4248
description = "The IAM role that grants access to ECS and Batch services which we'll use as our Metadata Service API's execution_role for our Fargate instance"

variables.tf

+6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ variable "batch_type" {
1010
default = "ec2"
1111
}
1212

13+
variable "db_migrate_lambda_zip_file" {
14+
type = string
15+
description = "Output path for the zip file containing the DB migrate lambda"
16+
default = null
17+
}
18+
1319
variable "enable_custom_batch_container_registry" {
1420
type = bool
1521
default = false

0 commit comments

Comments
 (0)