Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cloud resource naming #1099

Merged
merged 4 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/dry-hats-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@api3/airnode-deployer': patch
---

Fix length of cloud resource names
9 changes: 6 additions & 3 deletions packages/airnode-deployer/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
coverage/

# Config files
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to mention that this is deliberate so the next me won't update this.

config.json
secrets.env
receipt.json
#
# We ignore all the files ending with given names to allow storing different config/secrets files
# in the config directory which is useful for development.
*config.json
*secrets.env
*receipt.json

# Packed package
*.tgz
Expand Down
6 changes: 4 additions & 2 deletions packages/airnode-deployer/src/entrypoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
36 changes: 18 additions & 18 deletions packages/airnode-deployer/terraform/airnode/aws/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -31,15 +31,15 @@ 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
}

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
}
Expand All @@ -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} * * * ? *)"
}

Expand Down
4 changes: 2 additions & 2 deletions packages/airnode-deployer/terraform/airnode/aws/outputs.tf
Original file line number Diff line number Diff line change
@@ -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}"
}
7 changes: 7 additions & 0 deletions packages/airnode-deployer/terraform/airnode/aws/variables.tf
Original file line number Diff line number Diff line change
@@ -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}"
}

Expand Down
40 changes: 20 additions & 20 deletions packages/airnode-deployer/terraform/airnode/gcp/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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,
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -35,17 +35,17 @@ 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" {
provider = google-beta

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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down Expand Up @@ -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" {
Expand All @@ -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"

Expand Down
4 changes: 2 additions & 2 deletions packages/airnode-deployer/terraform/airnode/gcp/outputs.tf
Original file line number Diff line number Diff line change
@@ -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}"
}
Loading