@@ -6,10 +6,13 @@ resource "random_string" "solution_prefix" {
66 length = 4
77 special = false
88 upper = false
9+ numeric = false
910}
1011
1112locals {
1213 create_runtime = var. create_runtime
14+ # Sanitize runtime name to ensure it follows the regex pattern ^[a-zA-Z][a-zA-Z0-9_]{0,47}$
15+ sanitized_runtime_name = replace (var. runtime_name , " -" , " _" )
1316}
1417
1518# IAM Policy for creating the Service-Linked Role
@@ -38,9 +41,18 @@ data "aws_iam_policy_document" "service_linked_role" {
3841
3942resource "awscc_bedrockagentcore_runtime" "agent_runtime" {
4043 count = local. create_runtime ? 1 : 0
41- agent_runtime_name = " ${ random_string . solution_prefix . result } _${ var . runtime_name } "
44+ agent_runtime_name = " ${ random_string . solution_prefix . result } _${ local . sanitized_runtime_name } "
4245 description = var. runtime_description
4346 role_arn = var. runtime_role_arn != null ? var. runtime_role_arn : aws_iam_role. runtime_role [0 ]. arn
47+
48+ # Explicit dependency to avoid race conditions with IAM role creation
49+ # Include the time_sleep resource to ensure IAM role propagation
50+ depends_on = [
51+ aws_iam_role . runtime_role ,
52+ aws_iam_role_policy . runtime_role_policy ,
53+ aws_iam_role_policy . runtime_slr_policy ,
54+ time_sleep . iam_role_propagation
55+ ]
4456
4557 agent_runtime_artifact = {
4658 container_configuration = {
@@ -109,6 +121,13 @@ resource "aws_iam_role_policy" "runtime_slr_policy" {
109121 policy = data. aws_iam_policy_document . service_linked_role [0 ]. json
110122}
111123
124+ # Add a time delay to ensure IAM role propagation
125+ resource "time_sleep" "iam_role_propagation" {
126+ count = local. create_runtime && var. runtime_role_arn == null ? 1 : 0
127+ depends_on = [aws_iam_role . runtime_role , aws_iam_role_policy . runtime_role_policy , aws_iam_role_policy . runtime_slr_policy ]
128+ create_duration = " 20s"
129+ }
130+
112131resource "aws_iam_role_policy" "runtime_role_policy" {
113132 count = local. create_runtime && var. runtime_role_arn == null ? 1 : 0
114133 name = " ${ random_string . solution_prefix . result } -bedrock-agent-runtime-policy"
@@ -195,7 +214,7 @@ resource "aws_iam_role_policy" "runtime_role_policy" {
195214 ]
196215 Resource = [
197216 " arn:aws:bedrock-agentcore:${ data . aws_region . current . region } :${ data . aws_caller_identity . current . account_id } :workload-identity-directory/default" ,
198- " arn:aws:bedrock-agentcore:${ data . aws_region . current . region } :${ data . aws_caller_identity . current . account_id } :workload-identity-directory/default/workload-identity/${ random_string . solution_prefix . result } _${ var . runtime_name } -*"
217+ " arn:aws:bedrock-agentcore:${ data . aws_region . current . region } :${ data . aws_caller_identity . current . account_id } :workload-identity-directory/default/workload-identity/${ random_string . solution_prefix . result } _${ local . sanitized_runtime_name } -*"
199218 ]
200219 },
201220 {
@@ -218,11 +237,13 @@ resource "aws_iam_role_policy" "runtime_role_policy" {
218237
219238locals {
220239 create_runtime_endpoint = var. create_runtime_endpoint
240+ # Sanitize runtime endpoint name to ensure it follows the regex pattern ^[a-zA-Z][a-zA-Z0-9_]{0,47}$
241+ sanitized_runtime_endpoint_name = replace (var. runtime_endpoint_name , " -" , " _" )
221242}
222243
223244resource "awscc_bedrockagentcore_runtime_endpoint" "agent_runtime_endpoint" {
224245 count = local. create_runtime_endpoint ? 1 : 0
225- name = " ${ random_string . solution_prefix . result } _${ var . runtime_endpoint_name } "
246+ name = " ${ random_string . solution_prefix . result } _${ local . sanitized_runtime_endpoint_name } "
226247 description = var. runtime_endpoint_description
227248 agent_runtime_id = var. runtime_endpoint_agent_runtime_id != null ? var. runtime_endpoint_agent_runtime_id : try (awscc_bedrockagentcore_runtime. agent_runtime [0 ]. agent_runtime_id , null )
228249 tags = var. runtime_endpoint_tags
0 commit comments