Skip to content
Open
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
18 changes: 18 additions & 0 deletions image-copy-ecr/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM golang:1.23.4-alpine3.20 AS builder

WORKDIR /app

COPY main.go .
COPY go.mod .
COPY go.sum .

RUN go build -tags lambda.norpc -o app main.go


FROM public.ecr.aws/lambda/provided:al2

# Copy function code
COPY --from=builder /app/app ./app

# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
ENTRYPOINT [ "./app" ]
42 changes: 32 additions & 10 deletions image-copy-ecr/iac/lambda.tf
Original file line number Diff line number Diff line change
Expand Up @@ -83,29 +83,51 @@ resource "aws_ecr_repository" "copier-repo" {
}
}

resource "ko_build" "image" {
repo = aws_ecr_repository.copier-repo.repository_url
importpath = "github.com/chainguard-dev/platform-examples/image-copy-ecr"
working_dir = path.module
// Disable SBOM generation due to
// https://github.com/ko-build/ko/issues/878
sbom = "none"
}

locals {
lambda_image_uri = "${aws_ecr_repository.copier_repo.repository_url}:${random_string.tag.result}"
// the sha1 hash value is used to detect changes to the source code so that
// the provider knows when to rebuild the lambda image with a new tag
source_hash = sha1(join("", [for f in fileset("${path.module}/..", "*") : filesha1("${path.module}/../${f}")]))
// Using a local for the lambda breaks a cyclic dependency between
// chainguard_identity.aws and aws_lambda_function.lambda
lambda_name = "image-copy"
}

resource "random_string" "tag" {
keepers = {
dir_sha1 = local.source_hash
}
length = 4
special = false
upper = false
}

resource "docker_registry_image" "cgr_ecr_mirror" {
name = docker_image.image.name
keep_remotely = true
}

resource "docker_image" "image" {
name = local.lambda_image_uri
build {
context = "${path.module}/.."
tag = ["latest", random_string.tag.result]
platform = "linux/amd64"
}
triggers = {
dir_sha1 = local.source_hash
}
}

data "aws_region" "current" {}

resource "aws_lambda_function" "lambda" {
function_name = local.lambda_name
role = aws_iam_role.lambda.arn

package_type = "Image"
image_uri = ko_build.image.image_ref
image_uri = local.lambda_image_uri
depends_on = [docker_registry_image.cgr_ecr_mirror]

timeout = 300

Expand Down
3 changes: 2 additions & 1 deletion image-copy-ecr/iac/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ terraform {
required_providers {
aws = { source = "hashicorp/aws" }
chainguard = { source = "chainguard-dev/chainguard" }
ko = { source = "ko-build/ko" }
docker = { source = "kreuzwerker/docker" }
random = { source = "hashicorp/random" }
}
}