66# Maps to: backend-stack.ts createAgentCoreGateway()
77# =============================================================================
88
9- # -----------------------------------------------------------------------------
10- # CloudWatch Log Group for Lambda
11- # -----------------------------------------------------------------------------
12-
13- resource "aws_cloudwatch_log_group" "tool_lambda" {
14- name = " /aws/lambda/${ var . stack_name_base } -sample-tool"
15- retention_in_days = local. log_retention_days
16-
17- }
18-
19- # -----------------------------------------------------------------------------
20- # IAM Role for Lambda Function
21- # -----------------------------------------------------------------------------
22-
23- data "aws_iam_policy_document" "tool_lambda_assume_role" {
24- statement {
25- effect = " Allow"
26- actions = [" sts:AssumeRole" ]
27-
28- principals {
29- type = " Service"
30- identifiers = [" lambda.amazonaws.com" ]
31- }
32- }
33- }
34-
35- resource "aws_iam_role" "tool_lambda" {
36- name = " ${ var . stack_name_base } -sample-tool-lambda-role"
37- assume_role_policy = data. aws_iam_policy_document . tool_lambda_assume_role . json
38- description = " Execution role for sample tool Lambda"
39-
40- }
41-
42- data "aws_iam_policy_document" "tool_lambda_policy" {
43- statement {
44- effect = " Allow"
45- actions = [
46- " logs:CreateLogStream" ,
47- " logs:PutLogEvents"
48- ]
49- resources = [" ${ aws_cloudwatch_log_group . tool_lambda . arn } :*" ]
50- }
51- }
52-
53- resource "aws_iam_role_policy" "tool_lambda" {
54- name = " ${ var . stack_name_base } -sample-tool-lambda-policy"
55- role = aws_iam_role. tool_lambda . id
56- policy = data. aws_iam_policy_document . tool_lambda_policy . json
57- }
58-
59- # -----------------------------------------------------------------------------
60- # Lambda Function for Sample Tool
61- # -----------------------------------------------------------------------------
62-
63- data "archive_file" "tool_lambda" {
64- type = " zip"
65- source_dir = local. gateway_lambda_source_path
66- output_path = " ${ path . module } /artifacts/gateway_lambda.zip"
67- excludes = [" tool_spec.json" , " __pycache__" , " *.pyc" ]
68- }
69-
70- resource "aws_lambda_function" "sample_tool" {
71- function_name = " ${ var . stack_name_base } -sample-tool"
72- role = aws_iam_role. tool_lambda . arn
73- handler = " sample_tool_lambda.handler"
74- runtime = " python3.13"
75- timeout = 30
76-
77- filename = data. archive_file . tool_lambda . output_path
78- source_code_hash = data. archive_file . tool_lambda . output_base64sha256
79-
80- depends_on = [aws_cloudwatch_log_group . tool_lambda ]
81-
82- }
83-
849# -----------------------------------------------------------------------------
8510# IAM Role for Gateway
8611# -----------------------------------------------------------------------------
@@ -105,16 +30,6 @@ resource "aws_iam_role" "gateway" {
10530}
10631
10732data "aws_iam_policy_document" "gateway_policy" {
108- # Lambda invoke permission
109- statement {
110- sid = " LambdaInvoke"
111- effect = " Allow"
112- actions = [
113- " lambda:InvokeFunction"
114- ]
115- resources = [aws_lambda_function . sample_tool . arn ]
116- }
117-
11833 # Bedrock permissions (region-agnostic)
11934 statement {
12035 sid = " BedrockInvoke"
@@ -172,7 +87,6 @@ resource "aws_iam_role_policy" "gateway" {
17287
17388# -----------------------------------------------------------------------------
17489# Wait for IAM permission propagation
175- # Gateway Target creation can fail due to IAM role propagation delay
17690# -----------------------------------------------------------------------------
17791
17892resource "time_sleep" "gateway_iam_propagation" {
@@ -190,15 +104,13 @@ resource "aws_bedrockagentcore_gateway" "main" {
190104 role_arn = aws_iam_role. gateway . arn
191105 description = " AgentCore Gateway with MCP protocol and JWT authentication"
192106
193- # Protocol configuration
194107 protocol_type = " MCP"
195108 protocol_configuration {
196109 mcp {
197110 supported_versions = [" 2025-03-26" ]
198111 }
199112 }
200113
201- # JWT authorizer with Cognito - uses machine client from auth.tf
202114 authorizer_type = " CUSTOM_JWT"
203115 authorizer_configuration {
204116 custom_jwt_authorizer {
@@ -207,55 +119,5 @@ resource "aws_bedrockagentcore_gateway" "main" {
207119 }
208120 }
209121
210-
211122 depends_on = [time_sleep . gateway_iam_propagation ]
212123}
213-
214- # -----------------------------------------------------------------------------
215- # AgentCore Gateway Target
216- # -----------------------------------------------------------------------------
217-
218- resource "aws_bedrockagentcore_gateway_target" "sample_tool" {
219- name = " sample-tool-target"
220- gateway_identifier = aws_bedrockagentcore_gateway. main . gateway_id
221- description = " Sample tool Lambda target"
222-
223- credential_provider_configuration {
224- gateway_iam_role {}
225- }
226-
227- target_configuration {
228- mcp {
229- lambda {
230- lambda_arn = aws_lambda_function. sample_tool . arn
231-
232- tool_schema {
233- inline_payload {
234- name = " text_analysis_tool"
235- description = " A tool which analyzes an input block of text to count number of words and return the top N frequent characters."
236-
237- input_schema {
238- type = " object"
239- description = " Input parameters for text analysis"
240-
241- property {
242- name = " text"
243- type = " string"
244- description = " Input block of text to analyze"
245- required = true
246- }
247-
248- property {
249- name = " N"
250- type = " integer"
251- description = " The number of most frequent characters to return (optional, default = 5)"
252- }
253- }
254- }
255- }
256- }
257- }
258- }
259-
260- depends_on = [aws_bedrockagentcore_gateway . main ]
261- }
0 commit comments