Skip to content

Commit b47b69d

Browse files
committed
Updated agentcore gateway execution role
1 parent 63bbafe commit b47b69d

File tree

2 files changed

+36
-45
lines changed

2 files changed

+36
-45
lines changed

src/lambda/agentcore_gateway_manager/index.py

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ def create_gateway(props, gateway_name, client):
9696
lambda_arn = props['LambdaArn']
9797
user_pool_id = props['UserPoolId']
9898
client_id = props['ClientId']
99+
execution_role_arn = props.get('ExecutionRoleArn')
99100

100101
# Create JWT authorizer config using existing Cognito resources
101102
authorizer_config = {
@@ -108,7 +109,7 @@ def create_gateway(props, gateway_name, client):
108109
# Create gateway
109110
gateway = client.create_mcp_gateway(
110111
name=gateway_name,
111-
role_arn=None,
112+
role_arn=execution_role_arn,
112113
authorizer_config=authorizer_config,
113114
enable_semantic_search=True,
114115
)
@@ -121,38 +122,6 @@ def create_gateway(props, gateway_name, client):
121122
logger.info("Waiting for IAM propagation...")
122123
time.sleep(30)
123124

124-
# Override trust policy to support all regions
125-
gateway_role_arn = gateway.get('executionRoleArn')
126-
if gateway_role_arn:
127-
role_name = gateway_role_arn.split('/')[-1]
128-
logger.info(f"Updating trust policy for role: {role_name}")
129-
130-
iam_client = boto3.client('iam')
131-
sts_client = boto3.client('sts')
132-
account_id = sts_client.get_caller_identity()['Account']
133-
134-
try:
135-
iam_client.update_assume_role_policy(
136-
RoleName=role_name,
137-
PolicyDocument=json.dumps({
138-
"Version": "2012-10-17",
139-
"Statement": [{
140-
"Effect": "Allow",
141-
"Principal": {"Service": "bedrock-agentcore.amazonaws.com"},
142-
"Action": "sts:AssumeRole",
143-
"Condition": {
144-
"StringEquals": {"aws:SourceAccount": account_id},
145-
"ArnLike": {"aws:SourceArn": f"arn:aws:bedrock-agentcore:*:{account_id}:*"}
146-
}
147-
}]
148-
})
149-
)
150-
logger.info("Trust policy updated successfully to support all regions")
151-
except Exception as e:
152-
logger.warning(f"Failed to update trust policy: {e}")
153-
else:
154-
logger.warning("Gateway executionRoleArn not found, skipping trust policy update")
155-
156125
# Add analytics Lambda target
157126
logger.info("Adding analytics Lambda target...")
158127
client.create_mcp_gateway_target(

template.yaml

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,13 +1162,46 @@ Resources:
11621162
KmsKeyId: !GetAtt CustomerManagedEncryptionKey.Arn
11631163
RetentionInDays: !Ref LogRetentionDays
11641164

1165+
AgentCoreGatewayExecutionRole:
1166+
Type: AWS::IAM::Role
1167+
Condition: CreateAgentCoreLambda
1168+
Properties:
1169+
RoleName: !Sub "${AWS::StackName}-AgentCoreGatewayExecutionRole"
1170+
Description: Execution role for AgentCore Gateway
1171+
AssumeRolePolicyDocument:
1172+
Version: "2012-10-17"
1173+
Statement:
1174+
- Effect: Allow
1175+
Principal:
1176+
Service: !Sub "bedrock-agentcore.${AWS::URLSuffix}"
1177+
Action: sts:AssumeRole
1178+
Condition:
1179+
StringEquals:
1180+
aws:SourceAccount: !Ref AWS::AccountId
1181+
ArnLike:
1182+
aws:SourceArn: !Sub "arn:${AWS::Partition}:bedrock-agentcore:${AWS::Region}:${AWS::AccountId}:*"
1183+
Policies:
1184+
- PolicyName: InvokeLambdaPolicy
1185+
PolicyDocument:
1186+
Version: "2012-10-17"
1187+
Statement:
1188+
- Effect: Allow
1189+
Action: lambda:InvokeFunction
1190+
Resource: !GetAtt AgentCoreAnalyticsLambdaFunction.Arn
1191+
ManagedPolicyArns:
1192+
- !Sub "arn:${AWS::Partition}:iam::aws:policy/CloudWatchLogsFullAccess"
1193+
Tags:
1194+
- Key: Name
1195+
Value: !Sub "${AWS::StackName}-AgentCoreGatewayExecutionRole"
1196+
11651197
AgentCoreGateway:
11661198
Type: Custom::AgentCoreGateway
11671199
Condition: CreateAgentCoreLambda
11681200
DependsOn:
11691201
- AgentCoreAnalyticsLambdaFunction
11701202
- ExternalAppClient
11711203
- UserPool
1204+
- AgentCoreGatewayExecutionRole
11721205
Properties:
11731206
ServiceToken: !GetAtt AgentCoreGatewayManagerFunction.Arn
11741207
StackName: !Ref AWS::StackName
@@ -1177,20 +1210,9 @@ Resources:
11771210
UserPoolId: !Ref UserPool
11781211
ClientId: !Ref ExternalAppClient
11791212
ClientSecret: !GetAtt ExternalAppClient.ClientSecret
1213+
ExecutionRoleArn: !GetAtt AgentCoreGatewayExecutionRole.Arn
11801214
SourceCodeHash: <LAMBDA_HASH_TOKEN>
11811215

1182-
AgentCoreAnalyticsLambdaInvokePermission:
1183-
Type: AWS::Lambda::Permission
1184-
Condition: CreateAgentCoreLambda
1185-
DependsOn:
1186-
- AgentCoreGateway
1187-
- AgentCoreAnalyticsLambdaFunction
1188-
Properties:
1189-
FunctionName: !Ref AgentCoreAnalyticsLambdaFunction
1190-
Action: lambda:InvokeFunction
1191-
Principal: !Sub "bedrock-agentcore.${AWS::URLSuffix}"
1192-
SourceArn: !GetAtt AgentCoreGateway.GatewayArn
1193-
11941216
##########################################################################
11951217
# Nested stack for selected pattern
11961218
##########################################################################

0 commit comments

Comments
 (0)