From 3491de13734271b24d4680e8d0d187d1e8789d89 Mon Sep 17 00:00:00 2001 From: Michal Kimle Date: Mon, 16 May 2022 15:00:37 +0200 Subject: [PATCH 1/4] Remove unnecessary resource suffixes --- .../terraform/airnode/aws/modules/apigateway/main.tf | 8 ++++---- .../terraform/airnode/aws/modules/function/main.tf | 8 ++++---- .../terraform/airnode/aws/variables.tf | 7 +++++++ .../terraform/airnode/gcp/modules/apigateway/main.tf | 10 +++++----- .../terraform/airnode/gcp/modules/function/main.tf | 6 +++--- .../terraform/airnode/gcp/variables.tf | 7 +++++++ 6 files changed, 30 insertions(+), 16 deletions(-) diff --git a/packages/airnode-deployer/terraform/airnode/aws/modules/apigateway/main.tf b/packages/airnode-deployer/terraform/airnode/aws/modules/apigateway/main.tf index 85be2b6eac..2d61ab9d57 100644 --- a/packages/airnode-deployer/terraform/airnode/aws/modules/apigateway/main.tf +++ b/packages/airnode-deployer/terraform/airnode/aws/modules/apigateway/main.tf @@ -3,14 +3,14 @@ resource "aws_iam_role" "api_gateway_role" { assume_role_policy = data.aws_iam_policy_document.role_policy.json tags = { - Name = "${var.name}-Role" + Name = var.name } } resource "aws_iam_role_policy" "lambda_invoke_role_policy" { count = length(var.lambdas) != 0 ? 1 : 0 - name = "${var.name}-Lambda-Invoke-Policy" + name = "${var.name}-lambda-invoke" role = aws_iam_role.api_gateway_role.id policy = data.aws_iam_policy_document.lambda_invoke_policy.json } @@ -44,7 +44,7 @@ resource "aws_api_gateway_stage" "stage" { } resource "aws_api_gateway_usage_plan" "usage_plan" { - name = "${var.name}-Usage-Plan" + name = var.name api_stages { api_id = aws_api_gateway_rest_api.api_gateway.id @@ -53,7 +53,7 @@ resource "aws_api_gateway_usage_plan" "usage_plan" { } resource "aws_api_gateway_api_key" "api_key" { - name = "${var.name}-API-Key" + name = var.name value = var.api_key } diff --git a/packages/airnode-deployer/terraform/airnode/aws/modules/function/main.tf b/packages/airnode-deployer/terraform/airnode/aws/modules/function/main.tf index 5f98958972..3c320769ee 100644 --- a/packages/airnode-deployer/terraform/airnode/aws/modules/function/main.tf +++ b/packages/airnode-deployer/terraform/airnode/aws/modules/function/main.tf @@ -21,7 +21,7 @@ EOC } resource "aws_iam_role" "lambda_role" { - name = "${var.name}-role" + name = var.name assume_role_policy = data.aws_iam_policy_document.role_policy.json } @@ -31,7 +31,7 @@ resource "aws_cloudwatch_log_group" "cloudwatch_log_group" { } resource "aws_iam_role_policy" "lambda_log_role_policy" { - name = "${var.name}-log-policy" + name = "${var.name}-log" role = aws_iam_role.lambda_role.id policy = data.aws_iam_policy_document.cloudwatch_log_policy.json } @@ -39,7 +39,7 @@ resource "aws_iam_role_policy" "lambda_log_role_policy" { resource "aws_iam_role_policy" "invoke_lambda_role_policy" { count = length(var.invoke_targets) != 0 ? 1 : 0 - name = "${var.name}-invoke-policy" + name = "${var.name}-invoke" role = aws_iam_role.lambda_role.id policy = data.aws_iam_policy_document.lambda_invoke_policy.json } @@ -66,7 +66,7 @@ resource "aws_lambda_function" "lambda" { resource "aws_cloudwatch_event_rule" "lambda_schedule_rule" { count = var.schedule_interval == 0 ? 0 : 1 - name = "${var.name}-schedule-rule" + name = "${var.name}-schedule" schedule_expression = "cron(0/${var.schedule_interval} * * * ? *)" } diff --git a/packages/airnode-deployer/terraform/airnode/aws/variables.tf b/packages/airnode-deployer/terraform/airnode/aws/variables.tf index c52dc5aa8f..84a0b50039 100644 --- a/packages/airnode-deployer/terraform/airnode/aws/variables.tf +++ b/packages/airnode-deployer/terraform/airnode/aws/variables.tf @@ -1,4 +1,11 @@ locals { + # Be aware when using `name-prefix` for naming resources + # as it can be up to 32 characters long: + # + # infrastructure_name - "airnode" - 7 characters + # airnode_address_short - 7 characters + # stage - up to 16 characters + # dashes between - 2 characters name_prefix = "${var.infrastructure_name}-${var.airnode_address_short}-${var.stage}" } diff --git a/packages/airnode-deployer/terraform/airnode/gcp/modules/apigateway/main.tf b/packages/airnode-deployer/terraform/airnode/gcp/modules/apigateway/main.tf index eeefff423e..1c42de4eec 100644 --- a/packages/airnode-deployer/terraform/airnode/gcp/modules/apigateway/main.tf +++ b/packages/airnode-deployer/terraform/airnode/gcp/modules/apigateway/main.tf @@ -8,7 +8,7 @@ resource "random_string" "api_gateway_service_account_id" { resource "google_service_account" "api_gateway_service_account" { account_id = random_string.api_gateway_service_account_id.result - display_name = "${var.name}-service-account" + display_name = var.name } resource "google_project_iam_member" "api_gateway_monitoring_writer_role" { @@ -35,8 +35,8 @@ resource "google_cloudfunctions_function_iam_member" "invoker" { resource "google_api_gateway_api" "api_gateway_api" { provider = google-beta - api_id = lower("${var.name}-api") - display_name = "${var.name}-api" + api_id = lower(var.name) + display_name = var.name } resource "google_api_gateway_api_config" "api_gateway_api_config" { @@ -44,8 +44,8 @@ resource "google_api_gateway_api_config" "api_gateway_api_config" { api = google_api_gateway_api.api_gateway_api.api_id # Generating a suffix manually because the one generated with `api_config_id_prefix` is far too long - api_config_id = lower("${var.name}-config-${substr(uuid(), 0, 8)}") - display_name = "${var.name}-config" + api_config_id = lower("${var.name}-${substr(uuid(), 0, 8)}") + display_name = var.name openapi_documents { document { diff --git a/packages/airnode-deployer/terraform/airnode/gcp/modules/function/main.tf b/packages/airnode-deployer/terraform/airnode/gcp/modules/function/main.tf index 33e91d0a28..072905e7e1 100644 --- a/packages/airnode-deployer/terraform/airnode/gcp/modules/function/main.tf +++ b/packages/airnode-deployer/terraform/airnode/gcp/modules/function/main.tf @@ -30,7 +30,7 @@ EOC resource "google_service_account" "function_service_account" { account_id = random_string.function_service_account_id.result - display_name = "${var.name}-service-account" + display_name = "${var.name}-function" } resource "google_project_iam_member" "function_monitoring_writer_role" { @@ -104,7 +104,7 @@ resource "google_service_account" "scheduler_service_account" { count = var.schedule_interval == 0 ? 0 : 1 account_id = random_string.scheduler_service_account_id[0].result - display_name = "${var.name}-service-account" + display_name = "${var.name}-scheduler" } resource "google_cloudfunctions_function_iam_member" "scheduler_invoker" { @@ -119,7 +119,7 @@ resource "google_cloudfunctions_function_iam_member" "scheduler_invoker" { resource "google_cloud_scheduler_job" "scheduler_job" { count = var.schedule_interval == 0 ? 0 : 1 - name = "${var.name}-scheduler-job" + name = var.name schedule = "*/${var.schedule_interval} * * * *" attempt_deadline = "${var.timeout}s" diff --git a/packages/airnode-deployer/terraform/airnode/gcp/variables.tf b/packages/airnode-deployer/terraform/airnode/gcp/variables.tf index c34cec548e..24391c5ca5 100644 --- a/packages/airnode-deployer/terraform/airnode/gcp/variables.tf +++ b/packages/airnode-deployer/terraform/airnode/gcp/variables.tf @@ -1,4 +1,11 @@ locals { + # Be aware when using `name-prefix` for naming resources + # as it can be up to 32 characters long: + # + # infrastructure_name - "airnode" - 7 characters + # airnode_address_short - 7 characters + # stage - up to 16 characters + # dashes between - 2 characters name_prefix = "${var.infrastructure_name}-${var.airnode_address_short}-${var.stage}" } From d8125a488efe756431019e39b37bc9f19ee3387c Mon Sep 17 00:00:00 2001 From: Michal Kimle Date: Tue, 17 May 2022 10:07:04 +0200 Subject: [PATCH 2/4] Shorten resource names --- packages/airnode-deployer/src/entrypoint.ts | 6 ++- .../terraform/airnode/aws/main.tf | 36 ++++++++--------- .../terraform/airnode/aws/outputs.tf | 4 +- ...ttpApiGateway.yaml.tpl => httpGw.yaml.tpl} | 0 ...Gateway.yaml.tpl => httpSignedGw.yaml.tpl} | 0 .../terraform/airnode/gcp/main.tf | 40 +++++++++---------- .../terraform/airnode/gcp/outputs.tf | 4 +- .../{httpApiGateway.yaml.tpl => httpGw.tpl} | 0 ...taApiGateway.yaml.tpl => httpSignedGw.tpl} | 0 9 files changed, 46 insertions(+), 44 deletions(-) rename packages/airnode-deployer/terraform/airnode/aws/templates/{httpApiGateway.yaml.tpl => httpGw.yaml.tpl} (100%) rename packages/airnode-deployer/terraform/airnode/aws/templates/{httpSignedDataApiGateway.yaml.tpl => httpSignedGw.yaml.tpl} (100%) rename packages/airnode-deployer/terraform/airnode/gcp/templates/{httpApiGateway.yaml.tpl => httpGw.tpl} (100%) rename packages/airnode-deployer/terraform/airnode/gcp/templates/{httpSignedDataApiGateway.yaml.tpl => httpSignedGw.tpl} (100%) diff --git a/packages/airnode-deployer/src/entrypoint.ts b/packages/airnode-deployer/src/entrypoint.ts index e22db1ee62..c10276dff2 100644 --- a/packages/airnode-deployer/src/entrypoint.ts +++ b/packages/airnode-deployer/src/entrypoint.ts @@ -18,12 +18,14 @@ export async function run(...args: unknown[]) { return handler.run(...args); } -export async function processHttpRequest(...args: unknown[]) { +// We shorten function name to allow for shorter cloud resource names +export async function httpReq(...args: unknown[]) { const handler = await cloudHandler(); return handler.processHttpRequest(...args); } -export async function processHttpSignedDataRequest(...args: unknown[]) { +// We shorten function name to allow for shorter cloud resource names +export async function httpSignedReq(...args: unknown[]) { const handler = await cloudHandler(); return handler.processHttpSignedDataRequest(...args); } diff --git a/packages/airnode-deployer/terraform/airnode/aws/main.tf b/packages/airnode-deployer/terraform/airnode/aws/main.tf index 4d0db91e0d..7101727375 100644 --- a/packages/airnode-deployer/terraform/airnode/aws/main.tf +++ b/packages/airnode-deployer/terraform/airnode/aws/main.tf @@ -26,8 +26,8 @@ module "startCoordinator" { configuration_file = var.configuration_file secrets_file = var.secrets_file environment_variables = { - HTTP_GATEWAY_URL = var.http_api_key == null ? null : "${module.httpApiGateway[0].api_url}" - HTTP_SIGNED_DATA_GATEWAY_URL = var.http_signed_data_api_key == null ? null : "${module.httpSignedDataApiGateway[0].api_url}" + HTTP_GATEWAY_URL = var.http_api_key == null ? null : "${module.httpGw[0].api_url}" + HTTP_SIGNED_DATA_GATEWAY_URL = var.http_signed_data_api_key == null ? null : "${module.httpSignedGw[0].api_url}" } invoke_targets = [module.run.lambda_arn] @@ -37,12 +37,12 @@ module "startCoordinator" { depends_on = [module.run] } -module "processHttpRequest" { +module "httpReq" { source = "./modules/function" count = var.http_api_key == null ? 0 : 1 - name = "${local.name_prefix}-processHttpRequest" - handler = "index.processHttpRequest" + name = "${local.name_prefix}-httpReq" + handler = "index.httpReq" source_dir = var.handler_dir memory_size = 256 timeout = 15 @@ -51,29 +51,29 @@ module "processHttpRequest" { reserved_concurrent_executions = var.disable_concurrency_reservation ? null : var.http_max_concurrency } -module "httpApiGateway" { +module "httpGw" { source = "./modules/apigateway" count = var.http_api_key == null ? 0 : 1 - name = "${local.name_prefix}-httpApiGateway" + name = "${local.name_prefix}-httpGw" stage = "v1" - template_file = "./templates/httpApiGateway.yaml.tpl" + template_file = "./templates/httpGw.yaml.tpl" template_variables = { - proxy_lambda = module.processHttpRequest[0].lambda_arn + proxy_lambda = module.httpReq[0].lambda_arn region = var.aws_region } lambdas = [ - module.processHttpRequest[0].lambda_arn + module.httpReq[0].lambda_arn ] api_key = var.http_api_key } -module "processHttpSignedDataRequest" { +module "httpSignedReq" { source = "./modules/function" count = var.http_signed_data_api_key == null ? 0 : 1 - name = "${local.name_prefix}-processHttpSignedDataRequest" - handler = "index.processHttpSignedDataRequest" + name = "${local.name_prefix}-httpSignedReq" + handler = "index.httpSignedReq" source_dir = var.handler_dir memory_size = 256 timeout = 15 @@ -82,19 +82,19 @@ module "processHttpSignedDataRequest" { reserved_concurrent_executions = var.disable_concurrency_reservation ? null : var.http_signed_data_max_concurrency } -module "httpSignedDataApiGateway" { +module "httpSignedGw" { source = "./modules/apigateway" count = var.http_signed_data_api_key == null ? 0 : 1 - name = "${local.name_prefix}-httpSignedDataApiGateway" + name = "${local.name_prefix}-httpSignedGw" stage = "v1" - template_file = "./templates/httpSignedDataApiGateway.yaml.tpl" + template_file = "./templates/httpSignedGw.yaml.tpl" template_variables = { - proxy_lambda = module.processHttpSignedDataRequest[0].lambda_arn + proxy_lambda = module.httpSignedReq[0].lambda_arn region = var.aws_region } lambdas = [ - module.processHttpSignedDataRequest[0].lambda_arn + module.httpSignedReq[0].lambda_arn ] api_key = var.http_signed_data_api_key } diff --git a/packages/airnode-deployer/terraform/airnode/aws/outputs.tf b/packages/airnode-deployer/terraform/airnode/aws/outputs.tf index 0981b51f3f..b66383da2f 100644 --- a/packages/airnode-deployer/terraform/airnode/aws/outputs.tf +++ b/packages/airnode-deployer/terraform/airnode/aws/outputs.tf @@ -1,7 +1,7 @@ output "http_gateway_url" { - value = var.http_api_key == null ? null : "${module.httpApiGateway[0].api_url}" + value = var.http_api_key == null ? null : "${module.httpGw[0].api_url}" } output "http_signed_data_gateway_url" { - value = var.http_signed_data_api_key == null ? null : "${module.httpSignedDataApiGateway[0].api_url}" + value = var.http_signed_data_api_key == null ? null : "${module.httpSignedGw[0].api_url}" } diff --git a/packages/airnode-deployer/terraform/airnode/aws/templates/httpApiGateway.yaml.tpl b/packages/airnode-deployer/terraform/airnode/aws/templates/httpGw.yaml.tpl similarity index 100% rename from packages/airnode-deployer/terraform/airnode/aws/templates/httpApiGateway.yaml.tpl rename to packages/airnode-deployer/terraform/airnode/aws/templates/httpGw.yaml.tpl diff --git a/packages/airnode-deployer/terraform/airnode/aws/templates/httpSignedDataApiGateway.yaml.tpl b/packages/airnode-deployer/terraform/airnode/aws/templates/httpSignedGw.yaml.tpl similarity index 100% rename from packages/airnode-deployer/terraform/airnode/aws/templates/httpSignedDataApiGateway.yaml.tpl rename to packages/airnode-deployer/terraform/airnode/aws/templates/httpSignedGw.yaml.tpl diff --git a/packages/airnode-deployer/terraform/airnode/gcp/main.tf b/packages/airnode-deployer/terraform/airnode/gcp/main.tf index 0d50831bba..29c737373b 100644 --- a/packages/airnode-deployer/terraform/airnode/gcp/main.tf +++ b/packages/airnode-deployer/terraform/airnode/gcp/main.tf @@ -56,8 +56,8 @@ module "startCoordinator" { project = var.gcp_project environment_variables = { - HTTP_GATEWAY_URL = var.http_api_key == null ? null : "${module.httpApiGateway[0].api_url}" - HTTP_SIGNED_DATA_GATEWAY_URL = var.http_signed_data_api_key == null ? null : "${module.httpSignedDataApiGateway[0].api_url}" + HTTP_GATEWAY_URL = var.http_api_key == null ? null : "${module.httpGw[0].api_url}" + HTTP_SIGNED_DATA_GATEWAY_URL = var.http_signed_data_api_key == null ? null : "${module.httpSignedGw[0].api_url}" } schedule_interval = 1 @@ -96,12 +96,12 @@ resource "google_project_service" "servicecontrol_api" { ] } -module "processHttpRequest" { +module "httpReq" { source = "./modules/function" count = var.http_api_key == null ? 0 : 1 - name = "${local.name_prefix}-processHttpRequest" - entry_point = "processHttpRequest" + name = "${local.name_prefix}-httpReq" + entry_point = "httpReq" source_dir = var.handler_dir memory_size = 256 timeout = 15 @@ -121,36 +121,36 @@ module "processHttpRequest" { ] } -module "httpApiGateway" { +module "httpGw" { source = "./modules/apigateway" count = var.http_api_key == null ? 0 : 1 - name = "${local.name_prefix}-httpApiGateway" - template_file = "./templates/httpApiGateway.yaml.tpl" + name = "${local.name_prefix}-httpGw" + template_file = "./templates/httpGw.yaml.tpl" template_variables = { project = var.gcp_project region = var.gcp_region - cloud_function_name = module.processHttpRequest[0].function_name + cloud_function_name = module.httpReq[0].function_name } project = var.gcp_project invoke_targets = [ - module.processHttpRequest[0].function_name + module.httpReq[0].function_name ] depends_on = [ google_project_service.apigateway_api, google_project_service.servicecontrol_api, - module.processHttpRequest, + module.httpReq, ] } -module "processHttpSignedDataRequest" { +module "httpSignedReq" { source = "./modules/function" count = var.http_signed_data_api_key == null ? 0 : 1 - name = "${local.name_prefix}-processHttpSignedDataRequest" - entry_point = "processHttpSignedDataRequest" + name = "${local.name_prefix}-httpSignedReq" + entry_point = "httpSignedReq" source_dir = var.handler_dir memory_size = 256 timeout = 15 @@ -170,26 +170,26 @@ module "processHttpSignedDataRequest" { ] } -module "httpSignedDataApiGateway" { +module "httpSignedGw" { source = "./modules/apigateway" count = var.http_signed_data_api_key == null ? 0 : 1 - name = "${local.name_prefix}-httpSignedDataApiGateway" - template_file = "./templates/httpSignedDataApiGateway.yaml.tpl" + name = "${local.name_prefix}-httpSignedGw" + template_file = "./templates/httpSignedGw.yaml.tpl" template_variables = { project = var.gcp_project region = var.gcp_region - cloud_function_name = module.processHttpSignedDataRequest[0].function_name + cloud_function_name = module.httpSignedReq[0].function_name } project = var.gcp_project invoke_targets = [ - module.processHttpSignedDataRequest[0].function_name + module.httpSignedReq[0].function_name ] depends_on = [ google_project_service.apigateway_api, google_project_service.servicecontrol_api, - module.processHttpSignedDataRequest, + module.httpSignedReq, ] } diff --git a/packages/airnode-deployer/terraform/airnode/gcp/outputs.tf b/packages/airnode-deployer/terraform/airnode/gcp/outputs.tf index 74750fc90a..6941ad0b63 100644 --- a/packages/airnode-deployer/terraform/airnode/gcp/outputs.tf +++ b/packages/airnode-deployer/terraform/airnode/gcp/outputs.tf @@ -1,7 +1,7 @@ output "http_gateway_url" { - value = var.http_api_key == null ? null : "https://${module.httpApiGateway[0].api_url}" + value = var.http_api_key == null ? null : "https://${module.httpGw[0].api_url}" } output "http_signed_data_gateway_url" { - value = var.http_signed_data_api_key == null ? null : "https://${module.httpSignedDataApiGateway[0].api_url}" + value = var.http_signed_data_api_key == null ? null : "https://${module.httpSignedGw[0].api_url}" } diff --git a/packages/airnode-deployer/terraform/airnode/gcp/templates/httpApiGateway.yaml.tpl b/packages/airnode-deployer/terraform/airnode/gcp/templates/httpGw.tpl similarity index 100% rename from packages/airnode-deployer/terraform/airnode/gcp/templates/httpApiGateway.yaml.tpl rename to packages/airnode-deployer/terraform/airnode/gcp/templates/httpGw.tpl diff --git a/packages/airnode-deployer/terraform/airnode/gcp/templates/httpSignedDataApiGateway.yaml.tpl b/packages/airnode-deployer/terraform/airnode/gcp/templates/httpSignedGw.tpl similarity index 100% rename from packages/airnode-deployer/terraform/airnode/gcp/templates/httpSignedDataApiGateway.yaml.tpl rename to packages/airnode-deployer/terraform/airnode/gcp/templates/httpSignedGw.tpl From be44e07585baf8ea608ca670e72fd31751c21c2f Mon Sep 17 00:00:00 2001 From: Michal Kimle Date: Tue, 17 May 2022 10:14:52 +0200 Subject: [PATCH 3/4] Add changeset --- .changeset/dry-hats-juggle.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/dry-hats-juggle.md diff --git a/.changeset/dry-hats-juggle.md b/.changeset/dry-hats-juggle.md new file mode 100644 index 0000000000..596110c8c3 --- /dev/null +++ b/.changeset/dry-hats-juggle.md @@ -0,0 +1,5 @@ +--- +'@api3/airnode-deployer': patch +--- + +Fix length of cloud resource names From 89d9ec6cfb2791f2ce9a7c11b8c5ce086eef4ab5 Mon Sep 17 00:00:00 2001 From: Michal Kimle Date: Thu, 26 May 2022 08:48:52 +0200 Subject: [PATCH 4/4] Fix lerna version in Docker build --- docker/Dockerfile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index e5888af393..c7af74d43e 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -30,7 +30,8 @@ RUN git clone ${repository} ${buildDir} -b ${branch} # Production dependencies FROM source${build} AS deps -RUN yarn global add lerna && \ + # Install the same version of lerna we're using as dependency +RUN yarn global add lerna@$(grep '^\s*"lerna"' package.json | cut -d '^' -f 2 | cut -d '"' -f 1) && \ lerna bootstrap -- --production --no-optional --ignore-scripts && \ rm -rf node_modules/@api3 && \ mkdir -p ${dependenciesDir}/common && \