Skip to content

Commit

Permalink
docker config
Browse files Browse the repository at this point in the history
  • Loading branch information
vggonzal committed Oct 19, 2023
1 parent adcbb34 commit 04f25e1
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
62 changes: 57 additions & 5 deletions terraform/hydrocron-main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,65 @@ resource "aws_api_gateway_deployment" "hydrocron-api-gateway-deployment-test" {
}
}

resource "aws_lambda_function" "hydrocron_api_lambda_timeseries_test" {
function_name = "${local.ec2_resources_name}-function-timeseries-test"
role = aws_iam_role.hydrocron-service-role-test.arn

data "aws_ecr_authorization_token" "token" {}

locals {
lambda_container_image_uri_split = split("/", var.lambda_container_image_uri)
ecr_image_name_and_tag = split(":", element(local.lambda_container_image_uri_split, length(local.lambda_container_image_uri_split) - 1))
ecr_image_name = "${local.environment}-${element(local.ecr_image_name_and_tag, 0)}"
ecr_image_tag = element(local.ecr_image_name_and_tag, 1)
}

resource aws_ecr_repository "lambda-image-repo" {
name = local.ecr_image_name
tags = var.default_tags
}


resource null_resource ecr_login {
triggers = {
image_uri = var.lambda_container_image_uri
}

provisioner "local-exec" {
interpreter = ["/bin/bash", "-e", "-c"]
command = <<EOF
docker login ${data.aws_ecr_authorization_token.token.proxy_endpoint} -u AWS -p ${data.aws_ecr_authorization_token.token.password}
EOF
}
}

resource null_resource upload_ecr_image {
depends_on = [null_resource.ecr_login]
triggers = {
image_uri = var.lambda_container_image_uri
}

provisioner "local-exec" {
interpreter = ["/bin/bash", "-e", "-c"]
command = <<EOF
docker pull ${var.lambda_container_image_uri}
docker tag ${var.lambda_container_image_uri} ${aws_ecr_repository.lambda-image-repo.repository_url}:${local.ecr_image_tag}
docker push ${aws_ecr_repository.lambda-image-repo.repository_url}:${local.ecr_image_tag}
EOF
}
}


resource "aws_lambda_function" "hydrocron_lambda_timeseries" {
depends_on = [
null_resource.upload_ecr_image
]

package_type = "Image"
image_uri = "${var.docker_tag}"
image_uri = "${aws_ecr_repository.lambda-image-repo.repository_url}:${local.ecr_image_tag}"
image_config {
command = ["hydrocron_api.controllers.timeseries.lambda_handler"]
}
function_name = "${local.lambda_resources_name}-hydrocron_lambda_timeseries"
role = aws_iam_role.hydrocron-service-role-test.arn
timeout = 5
handler = "hydrocron_api.controllers.timeseries.lambda_handler"
runtime = "python3.8"

vpc_config {
Expand Down
1 change: 1 addition & 0 deletions terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ locals {
name = regex("name = \"(\\S*)\"", data.local_file.pyproject_toml.content)[0]
version = regex("version = \"(\\S*)\"", data.local_file.pyproject_toml.content)[0]
environment = var.stage
lambda_resources_name = terraform.workspace == "default" ? "svc-${local.name}-${local.environment}" : "svc-${local.name}-${local.environment}-${terraform.workspace}"

app_prefix = "service-${var.app_name}-${local.environment}"
service_prefix = "service-${var.app_name}-${local.environment}-${var.service_name}"
Expand Down

0 comments on commit 04f25e1

Please sign in to comment.