Skip to content

Fix inconsistent result error in aws_bedrockagent_agent_alias routing_configuration #43411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions .changelog/43411.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_bedrockagent_agent_alias: Fix inconsistent result error when `routing_configuration` is not specified but AWS returns default `agent_version`
```
40 changes: 38 additions & 2 deletions internal/service/bedrockagent/agent_alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,19 @@ func (r *agentAliasResource) Create(ctx context.Context, request resource.Create
return
}

// Handle the case where routing_configuration was not provided in the plan but AWS returns it with a default agent_version
// This prevents the "Provider produced inconsistent result" error
var planData agentAliasResourceModel
response.Diagnostics.Append(request.Plan.Get(ctx, &planData)...)
if response.Diagnostics.HasError() {
return
}

if planData.RoutingConfiguration.IsNull() {
// If routing_configuration was not specified in the plan, keep it as null in the state
data.RoutingConfiguration = planData.RoutingConfiguration
}

response.Diagnostics.Append(response.State.Set(ctx, &data)...)
}

Expand Down Expand Up @@ -206,11 +219,24 @@ func (r *agentAliasResource) Read(ctx context.Context, request resource.ReadRequ
return
}

// Store the original state to check if routing_configuration should remain null
var originalData agentAliasResourceModel
response.Diagnostics.Append(request.State.Get(ctx, &originalData)...)
if response.Diagnostics.HasError() {
return
}

response.Diagnostics.Append(fwflex.Flatten(ctx, output, &data)...)
if response.Diagnostics.HasError() {
return
}

// Handle the case where routing_configuration was null in the original state
// This maintains consistency when AWS returns a default agent_version
if originalData.RoutingConfiguration.IsNull() {
data.RoutingConfiguration = originalData.RoutingConfiguration
}

response.Diagnostics.Append(response.State.Set(ctx, &data)...)
}

Expand Down Expand Up @@ -266,8 +292,18 @@ func (r *agentAliasResource) Update(ctx context.Context, request resource.Update
}
}

// set unknowns if a tags only update
if new.RoutingConfiguration.IsUnknown() {
// Handle routing_configuration state consistency
var planData agentAliasResourceModel
response.Diagnostics.Append(request.Plan.Get(ctx, &planData)...)
if response.Diagnostics.HasError() {
return
}

if planData.RoutingConfiguration.IsNull() {
// If routing_configuration was not specified in the plan, keep it as null in the state
new.RoutingConfiguration = planData.RoutingConfiguration
} else if new.RoutingConfiguration.IsUnknown() {
// set unknowns if a tags only update
new.RoutingConfiguration = old.RoutingConfiguration
}

Expand Down
98 changes: 98 additions & 0 deletions internal/service/bedrockagent/agent_alias_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,76 @@ func TestAccBedrockAgentAgentAlias_routingUpdate(t *testing.T) {
})
}

func TestAccBedrockAgentAgentAlias_routingConfigurationNull(t *testing.T) {
ctx := acctest.Context(t)
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_bedrockagent_agent_alias.test"
var v awstypes.AgentAlias

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); acctest.PreCheckPartitionHasService(t, names.BedrockEndpointID) },
ErrorCheck: acctest.ErrorCheck(t, names.BedrockAgentServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckAgentAliasDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccAgentAliasConfig_routingConfigurationNull(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAgentAliasExists(ctx, resourceName, &v),
resource.TestCheckResourceAttr(resourceName, "agent_alias_name", rName),
resource.TestCheckResourceAttrSet(resourceName, "agent_alias_arn"),
resource.TestCheckResourceAttrSet(resourceName, "agent_alias_id"),
resource.TestCheckResourceAttrSet(resourceName, "agent_id"),
resource.TestCheckResourceAttr(resourceName, names.AttrDescription, "Test Alias"),
// The key assertion: routing_configuration should remain null even if AWS returns a default
resource.TestCheckResourceAttr(resourceName, "routing_configuration.#", "0"),
resource.TestCheckResourceAttr(resourceName, acctest.CtTagsPercent, "0"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccBedrockAgentAgentAlias_routingConfigurationExplicitNull(t *testing.T) {
ctx := acctest.Context(t)
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_bedrockagent_agent_alias.test"
var v awstypes.AgentAlias

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); acctest.PreCheckPartitionHasService(t, names.BedrockEndpointID) },
ErrorCheck: acctest.ErrorCheck(t, names.BedrockAgentServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckAgentAliasDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccAgentAliasConfig_routingConfigurationExplicitNull(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAgentAliasExists(ctx, resourceName, &v),
resource.TestCheckResourceAttr(resourceName, "agent_alias_name", rName),
resource.TestCheckResourceAttrSet(resourceName, "agent_alias_arn"),
resource.TestCheckResourceAttrSet(resourceName, "agent_alias_id"),
resource.TestCheckResourceAttrSet(resourceName, "agent_id"),
resource.TestCheckResourceAttr(resourceName, names.AttrDescription, "Test Alias"),
// The key assertion: routing_configuration should remain null even when explicitly set to null
resource.TestCheckResourceAttr(resourceName, "routing_configuration.#", "0"),
resource.TestCheckResourceAttr(resourceName, acctest.CtTagsPercent, "0"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccBedrockAgentAgentAlias_tags(t *testing.T) {
ctx := acctest.Context(t)
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
Expand Down Expand Up @@ -412,6 +482,34 @@ resource "aws_bedrockagent_agent_alias" "test" {
`, rName, tagKey1, tagValue1, tagKey2, tagValue2))
}

func testAccAgentAliasConfig_routingConfigurationNull(rName string) string {
return acctest.ConfigCompose(
testAccAgentConfig_basic(rName, "anthropic.claude-v2", "basic claude"),
fmt.Sprintf(`
resource "aws_bedrockagent_agent_alias" "test" {
agent_alias_name = %[1]q
agent_id = aws_bedrockagent_agent.test.agent_id
description = "Test Alias"
# routing_configuration is intentionally not specified to test null handling
}
`, rName))
}

func testAccAgentAliasConfig_routingConfigurationExplicitNull(rName string) string {
return acctest.ConfigCompose(
testAccAgentConfig_basic(rName, "anthropic.claude-v2", "basic claude"),
fmt.Sprintf(`
resource "aws_bedrockagent_agent_alias" "test" {
agent_alias_name = %[1]q
agent_id = aws_bedrockagent_agent.test.agent_id
description = "Test Alias"
routing_configuration {
agent_version = null
}
}
`, rName))
}

func testAccAgentAliasConfig_provisionedThroughout(rName, version string) string {
return acctest.ConfigCompose(
testAccAgentAliasConfig_provisionedModelThroughputBase(rName),
Expand Down
Loading