|
1 | 1 | # Copyright (c) HashiCorp, Inc.
|
2 | 2 | # SPDX-License-Identifier: MPL-2.0
|
3 | 3 |
|
| 4 | +terraform { |
| 5 | + required_providers { |
| 6 | + docker = { |
| 7 | + source = "kreuzwerker/docker" |
| 8 | + version = "3.0.2" |
| 9 | + } |
| 10 | + } |
| 11 | +} |
4 | 12 | locals {
|
5 | 13 | on_vpc = length(var.subnet_ids) > 0 && length(var.security_group_ids) > 0
|
6 | 14 | vpc_config = local.on_vpc ? [{
|
7 | 15 | subnet_ids = var.subnet_ids
|
8 | 16 | security_group_ids = var.security_group_ids
|
9 | 17 | }] : []
|
10 |
| - cron_key = "${var.name}-cron" |
11 |
| - lambda_events_key = "${var.name}-lambda_events" |
| 18 | + cron_key = "${var.name}-cron" |
| 19 | + lambda_events_key = "${var.name}-lambda_events" |
| 20 | + image_parts = split(":", var.consul_lambda_registrator_image) |
| 21 | + image_tag = local.image_parts[1] |
| 22 | + image_path_parts = split("/", local.image_parts[0]) |
| 23 | + image_username = local.image_path_parts[1] |
| 24 | + image_name = local.image_path_parts[2] |
| 25 | + ecr_image_uri = "${data.aws_caller_identity.current.account_id}.dkr.ecr.${var.region}.amazonaws.com/${var.private_ecr_repo_name}:${local.image_tag}" |
| 26 | + ecr_image_uri_pull_through = "${data.aws_caller_identity.current.account_id}.dkr.ecr.${var.region}.amazonaws.com/${var.ecr_repository_prefix}/${local.image_username}/${local.image_name}:${local.image_tag}" |
| 27 | +} |
| 28 | + |
| 29 | +# Equivalent of aws ecr get-login |
| 30 | +data "aws_ecr_authorization_token" "ecr_auth" {} |
| 31 | + |
| 32 | +provider "docker" { |
| 33 | + host = var.docker_host |
| 34 | + registry_auth { |
| 35 | + username = data.aws_ecr_authorization_token.ecr_auth.user_name |
| 36 | + password = data.aws_ecr_authorization_token.ecr_auth.password |
| 37 | + address = "${data.aws_caller_identity.current.account_id}.dkr.ecr.${var.region}.amazonaws.com" |
| 38 | + } |
12 | 39 | }
|
13 | 40 |
|
| 41 | +data "aws_caller_identity" "current" {} |
| 42 | + |
14 | 43 | resource "aws_iam_role" "registration" {
|
15 | 44 | name = var.name
|
16 | 45 |
|
@@ -127,8 +156,51 @@ resource "aws_iam_role_policy_attachment" "lambda_logs" {
|
127 | 156 | policy_arn = aws_iam_policy.policy.arn
|
128 | 157 | }
|
129 | 158 |
|
| 159 | +resource "aws_ecr_repository" "lambda-registrator" { |
| 160 | + count = var.enable_pull_through_cache ? 0 : 1 |
| 161 | + name = var.private_ecr_repo_name |
| 162 | + force_delete = true |
| 163 | +} |
| 164 | + |
| 165 | + |
| 166 | +resource "aws_ecr_pull_through_cache_rule" "pull_through_cache_rule" { |
| 167 | + count = var.enable_pull_through_cache ? 1 : 0 |
| 168 | + ecr_repository_prefix = var.ecr_repository_prefix |
| 169 | + upstream_registry_url = var.upstream_registry_url |
| 170 | +} |
| 171 | + |
| 172 | +resource "docker_image" "lambda_registrator" { |
| 173 | + name = var.enable_pull_through_cache ? local.ecr_image_uri_pull_through : var.consul_lambda_registrator_image |
| 174 | + depends_on = [ |
| 175 | + aws_ecr_pull_through_cache_rule.pull_through_cache_rule |
| 176 | + ] |
| 177 | +} |
| 178 | + |
| 179 | +resource "docker_tag" "lambda_registrator_tag" { |
| 180 | + count = var.enable_pull_through_cache ? 0 : 1 |
| 181 | + source_image = docker_image.lambda_registrator.name |
| 182 | + target_image = local.ecr_image_uri |
| 183 | +} |
| 184 | + |
| 185 | +resource "null_resource" "push_image" { |
| 186 | + count = var.enable_pull_through_cache ? 0 : 1 |
| 187 | + |
| 188 | + provisioner "local-exec" { |
| 189 | + command = "docker push ${local.ecr_image_uri}" |
| 190 | + } |
| 191 | + |
| 192 | + depends_on = [ |
| 193 | + docker_tag.lambda_registrator_tag |
| 194 | + ] |
| 195 | +} |
| 196 | +resource "time_sleep" "wait_30_seconds" { |
| 197 | + count = var.enable_pull_through_cache ? 1 : 0 |
| 198 | + depends_on = [docker_image.lambda_registrator] |
| 199 | + |
| 200 | + create_duration = "30s" |
| 201 | +} |
130 | 202 | resource "aws_lambda_function" "registration" {
|
131 |
| - image_uri = var.ecr_image_uri |
| 203 | + image_uri = var.enable_pull_through_cache ? local.ecr_image_uri_pull_through : local.ecr_image_uri |
132 | 204 | package_type = "Image"
|
133 | 205 | function_name = var.name
|
134 | 206 | role = aws_iam_role.registration.arn
|
@@ -168,6 +240,11 @@ resource "aws_lambda_function" "registration" {
|
168 | 240 | security_group_ids = vpc_config.value["security_group_ids"]
|
169 | 241 | }
|
170 | 242 | }
|
| 243 | + depends_on = [ |
| 244 | + null_resource.push_image, |
| 245 | + time_sleep.wait_30_seconds, |
| 246 | + ] |
| 247 | + |
171 | 248 | }
|
172 | 249 |
|
173 | 250 | module "eventbridge" {
|
|
0 commit comments