Skip to content

Commit 500aa83

Browse files
committed
feat(tf): ECR repository and image deployment
Add container image repository creation and build+push to Terraform deployment. TODO: Does not include docs update.
1 parent f605673 commit 500aa83

File tree

8 files changed

+126
-7
lines changed

8 files changed

+126
-7
lines changed

infra/main.tf

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1+
# Agent Container Image
2+
module "container_image" {
3+
source = "./modules/container-image"
4+
5+
force_image_rebuild = var.force_image_rebuild
6+
image_build_tool = var.container_image_build_tool
7+
repository_name = "langgraph-cx-agent"
8+
}
9+
110
# Bedrock Agent Role
211
module "bedrock_role" {
3-
source = "./modules/agentcore-iam-role"
4-
role_name = var.bedrock_role_name
5-
knowledge_base_id = module.kb_stack.knowledge_base_id
6-
guardrail_id = module.guardrail.guardrail_id
12+
source = "./modules/agentcore-iam-role"
13+
container_repository_arn = module.container_image.ecr_repository_arn
14+
role_name = var.bedrock_role_name
15+
knowledge_base_id = module.kb_stack.knowledge_base_id
16+
guardrail_id = module.guardrail.guardrail_id
717
}
818

919
# Knowledge Base Stack

infra/modules/agentcore-iam-role/bedrock-agentcore-policy.tf

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,13 @@ resource "aws_iam_policy" "ecr_permissions" {
3434
"ecr:BatchGetImage",
3535
"ecr:GetDownloadUrlForLayer"
3636
]
37-
Resource = [
38-
"arn:aws:ecr:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:repository/*"
39-
]
37+
Resource = (
38+
var.container_repository_arn == "" ?
39+
[
40+
"arn:aws:ecr:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:repository/*"
41+
] :
42+
[var.container_repository_arn]
43+
)
4044
},
4145
{
4246
Sid = "ECRTokenAccess"

infra/modules/agentcore-iam-role/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ variable "role_name" {
33
type = string
44
}
55

6+
variable "container_repository_arn" {
7+
description = "ARN of specific Amazon ECR repository to grant access (default: all)"
8+
default = ""
9+
type = string
10+
}
11+
612
variable "knowledge_base_id" {
713
description = "Knowledge Base ID to restrict access to"
814
type = string
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
data "aws_caller_identity" "current" {}
2+
data "aws_region" "current" {}
3+
4+
locals {
5+
image_src_path = "${path.root}/${var.relative_image_src_path}"
6+
image_src_hash = sha512(
7+
join(
8+
"",
9+
# TODO: Find a way to exclude .venv, dist, and potentially other subfolders:
10+
[for f in fileset(".", "${local.image_src_path}/**") : filesha512(f)]
11+
)
12+
)
13+
14+
image_build_extra_args = "--platform linux/arm64"
15+
image_build_push_cmd = <<-EOT
16+
aws ecr get-login-password | ${var.image_build_tool} login --username AWS \
17+
--password-stdin ${aws_ecr_repository.ecr_repository.repository_url} &&
18+
${var.image_build_tool} build ${local.image_build_extra_args} \
19+
-t ${aws_ecr_repository.ecr_repository.repository_url}:${var.image_tag} \
20+
${local.image_src_path} &&
21+
${var.image_build_tool} push ${aws_ecr_repository.ecr_repository.repository_url}:${var.image_tag}
22+
EOT
23+
}
24+
25+
resource "aws_ecr_repository" "ecr_repository" {
26+
name = var.repository_name
27+
}
28+
29+
resource "terraform_data" "ecr_image" {
30+
triggers_replace = [
31+
aws_ecr_repository.ecr_repository.id,
32+
var.force_image_rebuild == true ? timestamp() : local.image_src_hash
33+
]
34+
35+
input = "${aws_ecr_repository.ecr_repository.repository_url}:${var.image_tag}"
36+
37+
provisioner "local-exec" {
38+
command = local.image_build_push_cmd
39+
}
40+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
output "ecr_repository_arn" {
2+
description = "ARN of the Amazon ECR repository for the agent container image"
3+
value = aws_ecr_repository.ecr_repository.arn
4+
}
5+
6+
output "ecr_repository_uri" {
7+
description = "URI of the Amazon ECR repository for the agent container image"
8+
value = aws_ecr_repository.ecr_repository.repository_url
9+
}
10+
11+
output "ecr_image_uri" {
12+
description = "URI of the Amazon ECR repository for the agent container image"
13+
value = terraform_data.ecr_image.output
14+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
variable "force_image_rebuild" {
2+
description = "Set true to force rebuild & push of image to ECR even if source appears unchanged"
3+
default = false
4+
type = bool
5+
}
6+
7+
variable "image_build_tool" {
8+
description = "Either 'docker' or a Docker-compatible alternative e.g. 'finch'"
9+
default = "docker"
10+
type = string
11+
}
12+
13+
variable "relative_image_src_path" {
14+
description = "Path to container image source folder, relative to Terraform root"
15+
default = "../cx-agent-backend"
16+
type = string
17+
}
18+
19+
variable "image_tag" {
20+
description = "Tag to apply to the pushed container image in Amazon ECR"
21+
default = "latest"
22+
type = string
23+
}
24+
25+
variable "repository_name" {
26+
description = "Name of the Amazon ECR repository to create and deploy the image to"
27+
type = string
28+
}

infra/terraform.tfvars.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Container Image Build Variables
2+
## Uncomment the below line if you use 'finch' instead of Docker:
3+
# container_image_build_tool = "finch"
4+
15
# Bedrock Role Variables
26
bedrock_role_name = "agentic-ai-bedrock-role"
37

infra/variables.tf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
# Container Image Variables
2+
variable "force_image_rebuild" {
3+
description = "Set true to force rebuild+push of container image even if source seems unchanged"
4+
default = false
5+
type = bool
6+
}
7+
8+
variable "container_image_build_tool" {
9+
description = "Either 'docker' or a Docker-compatible alternative e.g. 'finch'"
10+
default = "docker"
11+
type = string
12+
}
13+
114
# Bedrock Role Variables
215
variable "bedrock_role_name" {
316
description = "Name of the Bedrock agent role"

0 commit comments

Comments
 (0)